From 284af628eee7e8661f117ca4cab698c24abf600f Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 25 Jul 2021 23:57:54 -0400 Subject: Replaced player_color type with equivalent player_color enum --- cmd/main.c | 4 ++-- include/othello.h | 24 +++++++++++++----------- src/game_loop.c | 21 +++++++++++---------- src/initialize_board.c | 2 +- src/print_board.c | 2 +- src/valid_moves.c | 11 ++++++----- 6 files changed, 34 insertions(+), 30 deletions(-) diff --git a/cmd/main.c b/cmd/main.c index 30ddd15..2c5a6e2 100644 --- a/cmd/main.c +++ b/cmd/main.c @@ -6,10 +6,10 @@ #include "othello.h" int main(int argc, char **argv) { - player_color board[8][8]; + enum player_color board[8][8]; initialize_board(board); - player_color winner = game_loop(board); + enum player_color winner = game_loop(board); switch (winner) { case WHITE: diff --git a/include/othello.h b/include/othello.h index ed25b6d..b9a8cf5 100644 --- a/include/othello.h +++ b/include/othello.h @@ -1,19 +1,21 @@ #include -typedef char player_color; -#define EMPTY (player_color)'*' -#define WHITE (player_color)'w' -#define BLACK (player_color)'b' +enum player_color { + EMPTY = '*', + WHITE = 'w', + BLACK = 'b', +}; struct move { int row; int col; }; -void initialize_board(player_color board[8][8]); -bool is_valid_move(const player_color board[8][8], - const player_color current_player, const struct move move); -bool has_valid_moves(const player_color board[8][8], - const player_color current_player); -player_color game_loop(player_color board[8][8]); -void print_board(const player_color board[8][8]); +void initialize_board(enum player_color board[8][8]); +bool is_valid_move(const enum player_color board[8][8], + const enum player_color current_player, + const struct move move); +bool has_valid_moves(const enum player_color board[8][8], + const enum player_color current_player); +enum player_color game_loop(enum player_color board[8][8]); +void print_board(const enum player_color board[8][8]); diff --git a/src/game_loop.c b/src/game_loop.c index 91350ec..964ba3f 100644 --- a/src/game_loop.c +++ b/src/game_loop.c @@ -9,7 +9,7 @@ #define STREQ(a, b) (strcmp(a, b) == 0) -static player_color other_player(player_color current_player) { +static enum player_color other_player(enum player_color current_player) { switch (current_player) { case WHITE: return BLACK; @@ -20,7 +20,7 @@ static player_color other_player(player_color current_player) { } } -static int player_number_from_color(player_color player) { +static int player_number_from_color(enum player_color player) { switch (player) { case WHITE: return 1; @@ -31,7 +31,7 @@ static int player_number_from_color(player_color player) { } } -static void prompt_player(player_color current_player) { +static void prompt_player(enum player_color current_player) { printf("Player %d [h for help] > ", player_number_from_color(current_player)); } @@ -40,13 +40,14 @@ static void print_help() { "For example `0 0` will select the top left corner.\n\n" "To print the board again, enter `p`\n\n" "To drop into a guile repl enter `g`.\n\n" - "In the guile repl you can define an AI to use for the current player.\n\n" + "In the guile repl you can define an AI to use for the current " + "player.\n\n" "After you define an AI for that player, it will be used for the rest " "of the game instead of prompting for input for that user."); } -static struct move get_move(player_color board[8][8], - player_color current_player) { +static struct move get_move(enum player_color board[8][8], + enum player_color current_player) { // Initialize move to an invalid move. struct move move = {-1, -1}; @@ -79,8 +80,8 @@ static struct move get_move(player_color board[8][8], return move; } -static void apply_move(player_color board[8][8], player_color current_player, - struct move move) { +static void apply_move(enum player_color board[8][8], + enum player_color current_player, struct move move) { if (!is_valid_move(board, current_player, move)) { // We should have checked for the validity of the move before we got here. @@ -151,8 +152,8 @@ static void apply_move(player_color board[8][8], player_color current_player, } } -player_color game_loop(player_color board[8][8]) { - player_color current_player = WHITE; +enum player_color game_loop(enum player_color board[8][8]) { + enum player_color current_player = WHITE; while (has_valid_moves(board, current_player)) { struct move move = get_move(board, current_player); diff --git a/src/initialize_board.c b/src/initialize_board.c index 0172bfb..cfa92c1 100644 --- a/src/initialize_board.c +++ b/src/initialize_board.c @@ -1,6 +1,6 @@ #include "othello.h" -void initialize_board(player_color board[8][8]) { +void initialize_board(enum player_color board[8][8]) { // Set all the positions to empty for (int i = 0; i < 8; i++) { diff --git a/src/print_board.c b/src/print_board.c index cea9b8e..9608fe4 100644 --- a/src/print_board.c +++ b/src/print_board.c @@ -2,7 +2,7 @@ #include "othello.h" -void print_board(const player_color board[8][8]) { +void print_board(const enum player_color board[8][8]) { puts(" 0 1 2 3 4 5 6 7"); for (int row = 0; row < 8; row++) { diff --git a/src/valid_moves.c b/src/valid_moves.c index bcbbd46..445f654 100644 --- a/src/valid_moves.c +++ b/src/valid_moves.c @@ -2,8 +2,9 @@ #include "othello.h" -bool is_valid_move(const player_color board[8][8], - const player_color current_player, const struct move move) { +bool is_valid_move(const enum player_color board[8][8], + const enum player_color current_player, + const struct move move) { // The move must be a positive position if (move.row < 0 || move.col < 0) { return false; @@ -13,7 +14,7 @@ bool is_valid_move(const player_color board[8][8], return false; } - const player_color other_color = current_player == WHITE ? BLACK : WHITE; + const enum player_color other_color = current_player == WHITE ? BLACK : WHITE; bool is_valid_up = false; bool is_valid_down = false; @@ -91,8 +92,8 @@ bool is_valid_move(const player_color board[8][8], return is_valid_up || is_valid_down || is_valid_left || is_valid_right; } -bool has_valid_moves(const player_color board[8][8], - const player_color current_player) { +bool has_valid_moves(const enum player_color board[8][8], + const enum player_color current_player) { bool result = false; struct move move; for (move.row = 0; move.row < 8 && !result; move.row++) { -- cgit v1.2.3