Untitled

 avatar
unknown
plain_text
2 months ago
12 kB
2
Indexable

#include "game.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "snake_utils.h"

/* Helper function definitions */
static void set_board_at(game_t *game, unsigned int row, unsigned int col, char ch);
static bool is_tail(char c);
static bool is_head(char c);
static bool is_snake(char c);
static char body_to_tail(char c);
static char head_to_body(char c);
static unsigned int get_next_row(unsigned int cur_row, char c);
static unsigned int get_next_col(unsigned int cur_col, char c);
static void find_head(game_t *game, unsigned int snum);
static char next_square(game_t *game, unsigned int snum);
static void update_tail(game_t *game, unsigned int snum);
static void update_head(game_t *game, unsigned int snum);

/* Task 1 */
game_t *create_default_game() {
  // TODO: Implement this function.
    game_t* game = malloc(sizeof(game_t));
    game->num_rows = 18;
    game->num_snakes = 1;
    game->board = malloc(sizeof(char*) * game->num_rows);

    char* initial_board[18] = {
        "####################",
        "#                  #",
        "# d>D              #",
        "#                  #",
        "#                  #",
        "#                  #",
        "#                  #",
        "#                  #",
        "#                  #",
        "#                  #",
        "#                  #",
        "#                  #",
        "#                  #",
        "#                  #",
        "#                  #",
        "#                  #",
        "#                  #",
        "####################"
    };

    for (int i = 0; i < game->num_rows; i++) {
        game->board[i] = malloc(sizeof(char) * 21);
        strcpy(game->board[i], initial_board[i]);
    }
    game->snakes = malloc(sizeof(snake_t));
    game->snakes[0].live = true;
    game->snakes[0].head_row = 2;
    game->snakes[0].head_col = 4;
    game->snakes[0].tail_row = 2;
    game->snakes[0].tail_col = 2;
    return game;

}

/* Task 2 */
void free_game(game_t *game) {
  
    if (game == NULL) return;  

    
    for (int i = 0; i < game->num_rows; i++) {
        if (game->board[i] != NULL) {
            free(game->board[i]);  
        }
    }

    
    if (game->board != NULL) {
        free(game->board);
    }

    
    if (game->snakes != NULL) {
        free(game->snakes);
    }

    
    free(game);
}

/* Task 3 */
void print_board(game_t *game, FILE *fp) {
  // TODO: Implement this function.
  for (int i = 0; i < game->num_rows; i++){
        fprintf(fp, "%s\n", game->board[i]);  
    }
}

/*
  Saves the current game into filename. Does not modify the game object.
  (already implemented for you).
*/
void save_board(game_t *game, char *filename) {
  FILE *f = fopen(filename, "w");
  print_board(game, f);
  fclose(f);
}

/* Task 4.1 */

/*
  Helper function to get a character from the board
  (already implemented for you).
*/
char get_board_at(game_t *game, unsigned int row, unsigned int col) { return game->board[row][col]; }

/*
  Helper function to set a character on the board
  (already implemented for you).
*/
static void set_board_at(game_t *game, unsigned int row, unsigned int col, char ch) {
  game->board[row][col] = ch;
}

/*
  Returns true if c is part of the snake's tail.
  The snake consists of these characters: "wasd"
  Returns false otherwise.
*/
static bool is_tail(char c) {
  // TODO: Implement this function.
  char *ch = "wasd";
  for(int i = 0; i < strlen(ch); i ++ ) {
    if (c == ch[i]) {
      return true;
    }
  }
  return false;
}

/*
  Returns true if c is part of the snake's head.
  The snake consists of these characters: "WASDx"
  Returns false otherwise.
*/
static bool is_head(char c) {
  // TODO: Implement this function.
  char *j = "WASDx";
  for(int i = 0; i < strlen(j); i ++ ) {
    if (c == j[i]) {
      return true;
    }
  }
  return false;
}

/*
  Returns true if c is part of the snake.
  The snake consists of these characters: "wasd^<v>WASDx"
*/
static bool is_snake(char c) {
  // TODO: Implement this function.
  char *j = "wasd^<v>WASDx";
  for(int i = 0; i < strlen(j); i ++ ) {
    if (c == j[i]) {
      return true;
    }
  }
  return false;
}

/*
  Converts a character in the snake's body ("^<v>")
  to the matching character representing the snake's
  tail ("wasd").
*/
static char body_to_tail(char c) {
  // TODO: Implement this function.
  char *body = "^<v>";
  char *tail = "wasd";
  for (int i = 0; i < strlen(body); i ++ ) {
    if(c == body[i]) {
      c = tail[i];
      break;
    }
  }
  return c;
}

/*
  Converts a character in the snake's head ("WASD")
  to the matching character representing the snake's
  body ("^<v>").
*/
static char head_to_body(char c) {
  // TODO: Implement this function.
  char *body = "^<v>";
  char *head = "WASD";
  for (int i = 0; i < strlen(head); i ++ ) {
    if(c == head[i]) {
      c = body[i];
      break;
    }
  }
  return c;
}

/*
  Returns cur_row + 1 if c is 'v' or 's' or 'S'.
  Returns cur_row - 1 if c is '^' or 'w' or 'W'.
  Returns cur_row otherwise.
*/
static unsigned int get_next_row(unsigned int cur_row, char c) {
  // TODO: Implement this function.
  char *increase = "vsS";
  char *decrease = "^wW";
  
  for(int i = 0; i < strlen(increase); i ++ ) {
    if(c == increase[i]) {
      cur_row ++ ;
      return cur_row;
    }
  }

  for(int i = 0; i < strlen(decrease); i ++ ) {
    if(c == decrease[i]) {
      cur_row -- ;
      break;
    }
  }
  
  return cur_row;
}

/*
  Returns cur_col + 1 if c is '>' or 'd' or 'D'.
  Returns cur_col - 1 if c is '<' or 'a' or 'A'.
  Returns cur_col otherwise.
*/
static unsigned int get_next_col(unsigned int cur_col, char c) {
  // TODO: Implement this function.
  char *increase = ">dD";
  char *decrease = "<aA";

  for(int i = 0; i < strlen(increase); i ++ ) {
    if(c == increase[i]) {
      cur_col ++ ;
      return cur_col;
    }
  }

  for(int i = 0; i < strlen(decrease); i ++ ) {
    if(c == decrease[i]) {
      cur_col -- ;
      break;
    }
  }

  return cur_col;
}

/*
  Task 4.2

  Helper function for update_game. Return the character in the cell the snake is moving into.

  This function should not modify anything.
*/
static char next_square(game_t *game, unsigned int snum) {
  // TODO: Implement this function.
    if (game == NULL || snum >= game->num_snakes || !game->snakes[snum].live) {
        return '\0';  
    }

    snake_t snake = game->snakes[snum];
    unsigned int head_row = snake.head_row;
    unsigned int head_col = snake.head_col;
    char head = game->board[head_row][head_col];
    unsigned int next_row = get_next_row(head_row, head);
    unsigned int next_col = get_next_col(head_col, head);

    if (next_row >= game->num_rows || next_col >= strlen(game->board[next_row])) {
        return '\0';  
    }

    return game->board[next_row][next_col];
}

/*
  Task 4.3

  Helper function for update_game. Update the head...

  ...on the board: add a character where the snake is moving

  ...in the snake struct: update the row and col of the head

  Note that this function ignores food, walls, and snake bodies when moving the head.
*/
static void update_head(game_t *game, unsigned int snum) {
  // TODO: Implement this function.
    snake_t *snake = &game->snakes[snum];
    unsigned int new_row = get_next_row(snake->head_row, game->board[snake->head_row][snake->head_col]);
    unsigned int new_col = get_next_col(snake->head_col, game->board[snake->head_row][snake->head_col]);
    char head_char = game->board[snake->head_row][snake->head_col];
    game->board[snake->head_row][snake->head_col] = head_to_body(head_char);
    game->board[new_row][new_col] = head_char;

    snake->head_row = new_row;
    snake->head_col = new_col;
}

/*
  Task 4.4

  Helper function for update_game. Update the tail...

  ...on the board: blank out the current tail, and change the new
  tail from a body character (^<v>) into a tail character (wasd)

  ...in the snake struct: update the row and col of the tail
*/
static void update_tail(game_t *game, unsigned int snum) {
  // TODO: Implement this function.
    snake_t *snake = &game->snakes[snum];
    unsigned int new_row = get_next_row(snake->tail_row, game->board[snake->tail_row][snake->tail_col]);
    unsigned int new_col = get_next_col(snake->tail_col, game->board[snake->tail_row][snake->tail_col]);
    game->board[new_row][new_col] = body_to_tail(game->board[new_row][new_col]);
    game->board[new_row][new_col] = body_to_tail(game->board[new_row][new_col]);
    snake->tail_row = new_row;
    snake->tail_col = new_col;
}

/* Task 4.5 */
void update_game(game_t *game, int (*add_food)(game_t *game)) {
  // TODO: Implement this function.
    for (int i = 0; i < game->num_snakes; i++) {
        snake_t *snake = &game->snakes[i];
        if (!snake->live) {
            continue;
        }
        char next_cell = next_square(game, i);
        
        if (is_snake(next_cell) || next_cell == '#') {
            game->board[snake->head_row][snake->head_col] = 'x';
            snake->live = false;
        } else if (next_cell == '*') {
                update_head(game, i);
                add_food(game);
            }
            else {
                update_head(game, i);
                update_tail(game, i);
            }
        }
    
}


/* Task 5.1 */
char *read_line(FILE *fp) {
  // TODO: Implement this function.
    char buf[1024];
    if (fgets(buf, sizeof(buf), fp) == NULL) {
        return NULL;
    }
    size_t len = strlen(buf);
    if (buf[len - 1] == '\n') {
        buf[len - 1] = '\0';
        len--;
    }
    char* l = malloc(len + 1);
    if (l) {
        strcpy(l, buf);
    }
    return l;
}

/* Task 5.2 */
game_t *load_board(FILE *fp) {
  // TODO: Implement this function.
    game_t *game = malloc(sizeof(game_t));
    if (!game){ 
        return NULL;
    }
    game->num_rows = 0;
    game->board = NULL;
    char *l;
    while((l = read_line(fp)) != NULL){
        char **new_board = realloc(game->board, sizeof(char*) * (game->num_rows + 1));
        if (!new_board) {
            free(l);
            return NULL;
        }
        game->board = new_board;
        game->board[game->num_rows++] = l;
    }

    return game;
}

/*
  Task 6.1

  Helper function for initialize_snakes.
  Given a snake struct with the tail row and col filled in,
  trace through the board to find the head row and col, and
  fill in the head row and col in the struct.
*/
static void find_head(game_t *game, unsigned int snum) {
  // TODO: Implement this function.
  snake_t* snake = &game->snakes[snum];
  int cur_row = snake->tail_row;
  int cur_col = snake->tail_col;
  char direction = game->board[cur_row][cur_col];
  while (!is_head(direction)) {
        cur_row = get_next_row(cur_row, direction);
        cur_col = get_next_col(cur_col, direction);
        direction = game->board[cur_row][cur_col];
    }
  snake->head_row = cur_row;
  snake->head_col = cur_col;
}
    
/* Task 6.2 */
game_t *initialize_snakes(game_t *game) {
  // TODO: Implement this function.
    int snake_count = 0;
    for (int i = 0; i < game->num_rows; i++) {
        for (int j = 0; j < strlen(game->board[i]); j++) {
            if (is_tail(game->board[i][j])) {
                snake_count++;
            }
        }
    }
    game->snakes = malloc(sizeof(snake_t) * snake_count);
    game->num_snakes = snake_count;
    int index = 0;

    for (int i = 0; i < game->num_rows; i++) {
        for (int j = 0; j < strlen(game->board[i]); j++) {
            if (is_tail(game->board[i][j])) {
                game->snakes[index].tail_row = i;
                game->snakes[index].tail_col = j;
                game->snakes[index].live = true;
                find_head(game, index);
                index++;

            }
        }
    }
    return game;
}
Editor is loading...
Leave a Comment