Week 5 Code Examples

#include <stdio.h>

#define MAP_ROWS 8
#define MAP_COLUMNS 8

#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'

// --- Enums and Structs ---

enum tile {
    EMPTY,
    WALL,
    CHURRO,
    TRAP,
    PLAYER
};

struct location {
    enum tile tile;
};

struct player {
    int x;
    int y;
};

// --- Starter Function Prototypes ---

void initialise_map(struct location map[MAP_ROWS][MAP_COLUMNS]);
void print_map(struct location map[MAP_ROWS][MAP_COLUMNS]);
void print_welcome(void);

void place_walls(struct location map[MAP_ROWS][MAP_COLUMNS]);
int check_validity(int row, int col, struct location map[MAP_ROWS][MAP_COLUMNS]);
void place_churros(struct location map[MAP_ROWS][MAP_COLUMNS]);
void play_game(struct location map[MAP_ROWS][MAP_COLUMNS], struct player hero);


int main(void) {
    struct location map[MAP_ROWS][MAP_COLUMNS];
    initialise_map(map);

    //int player_x;
    //int player_y;
    struct player hero;

    printf("Enter the player's starting location: ");
    scanf("%d %d", &hero.x, &hero.y);
    map[hero.x][hero.y].tile = PLAYER;

    // Function here to place some walls!
    place_walls(map);
    // Function here to place some churros!
    place_churros(map);
    
    print_welcome();

    print_map(map);
    printf("\n\n");

    play_game(map, hero);
    return 0;
}

// Play the game function - yay!
// INPUT: map and hero
// OUTPUT: void
void play_game(struct location map[MAP_ROWS][MAP_COLUMNS], struct player hero){
    char move;
    int row = hero.x;
    int col = hero.y;
    printf("Move: ");
    while (scanf(" %c", &move) == 1) {
        map[row][col].tile = EMPTY;
        if (move == UP){
            row--;
        } else if (move == DOWN) {
            row++;
        } else if (move == LEFT) {
            col--;
        } else if (move == RIGHT) {
            col++;
        }

        if(!check_validity(row, col, map)) {
            printf("Not valid..")
        }

        map[row][col].tile = PLAYER;
        
        print_map(map);
        printf("Move: ");
    }
}

// ADD YOUR OWN FUNctions HERE
// Function to place some walls
// INPUTS: map
// OUTPUT: void
void place_walls(struct location map[MAP_ROWS][MAP_COLUMNS]) {
    int num;
    printf("How many walls would you like to place?");
    scanf("%d", &num);

    for (int i = 0; i < num; i++) {
        int row;
        int col;
        printf("Where would you like to place wall? ");
        scanf("%d %d", &row, &col);
        while (!check_validity(row, col, map)){
            printf("Invalid spot, try again ");
            scanf("%d %d", &row, &col);
        }
        map[row][col].tile = WALL;
    }
}

// Function to place some churros
// INPUTS: map
// OUTPUT: void
void place_churros(struct location map[MAP_ROWS][MAP_COLUMNS]) {
    int num;
    printf("How many churros would you like to place?");
    scanf("%d", &num);

    for (int i = 0; i < num; i++) {
        int row;
        int col;
        printf("Where would you like to place a churro? ");
        scanf("%d %d", &row, &col);
        while (!check_validity(row, col, map)){
            printf("Invalid spot, try again ");
            scanf("%d %d", &row, &col);
        }
        map[row][col].tile = CHURRO;
    }
}

// Function to check valid input
// INPUT int row, int col, map 
// OUTPUT int (1 or 0 depending if it is a valid move or not)

int check_validity(int row, int col, struct location map[MAP_ROWS][MAP_COLUMNS]) {
    if (row < 0 || row >= MAP_ROWS || col < 0 || col >= MAP_COLUMNS) {
        return 0;
    } else if (map[row][col].tile != EMPTY) {
        return 0;
    }
    return 1;
}




// STARTER FUNCTIONS - DO NOT CHANGE

void initialise_map(struct location map[MAP_ROWS][MAP_COLUMNS]) {
    for (int r = 0; r < MAP_ROWS; r++) {
        for (int c = 0; c < MAP_COLUMNS; c++) {
            map[r][c].tile = EMPTY;
        }
    }
}

void print_map(struct location map[MAP_ROWS][MAP_COLUMNS]) {
    for (int r = 0; r < MAP_ROWS; r++) {
        for (int c = 0; c < MAP_COLUMNS; c++) {
            if (map[r][c].tile == PLAYER) {
                printf(" P ");
            } else if (map[r][c].tile == WALL) {
                printf(" # ");
            } else if (map[r][c].tile == CHURRO) {
                printf(" C ");
            } else if (map[r][c].tile == TRAP) {
                printf("(!)");
            } else {
                printf(" . ");
            }
        }
        printf("\n");
    }
}

void print_welcome(void) {
    printf("____________________________\n\n");
    printf("Welcome to the Churro Quest!\n\n");
    printf("____________________________\n\n");
}