diff options
-rw-r--r-- | include/othello.h | 2 | ||||
-rw-r--r-- | include/othello_move.h | 4 | ||||
-rw-r--r-- | src/board.c | 59 | ||||
-rw-r--r-- | src/game_loop.c | 5 | ||||
-rw-r--r-- | src/move.c | 116 | ||||
-rw-r--r-- | src/print_board.c | 2 | ||||
-rw-r--r-- | src/valid_moves.c | 3 |
7 files changed, 118 insertions, 73 deletions
diff --git a/include/othello.h b/include/othello.h index 56745bb..de4f846 100644 --- a/include/othello.h +++ b/include/othello.h @@ -52,6 +52,6 @@ void print_board(void); enum player_color get_current_player(void); // Returns the current board. -const enum player_color **get_board(void); +enum player_color **get_board(void); enum player_color get_winner(void); diff --git a/include/othello_move.h b/include/othello_move.h index 0bab641..377a27f 100644 --- a/include/othello_move.h +++ b/include/othello_move.h @@ -23,6 +23,7 @@ #include "othello.h" struct move prompt_get_move(enum player_color current_player); +struct move get_scm_move(FILE *strategy); // True if move is valid for current_player. // Otherwise false. @@ -34,4 +35,5 @@ bool is_valid_move(const enum player_color current_player, bool has_valid_moves(const enum player_color current_player); /* Returns true if the move was valid */ -bool apply_move(enum player_color current_player, struct move move); +bool apply_move(enum player_color **board, enum player_color current_player, + struct move move); diff --git a/src/board.c b/src/board.c index 12b5384..6d4a35c 100644 --- a/src/board.c +++ b/src/board.c @@ -25,14 +25,14 @@ static enum player_color **board; -const enum player_color **get_board(void) { - return (const enum player_color **)board; +enum player_color **get_board(void) { + return board; } void initialize_board() { board = malloc(8 * sizeof(enum player_color *)); for (int i = 0; i < 8; i++) { - board[i] = malloc(8 * sizeof(enum player_color)); + board[i] = malloc(8 * sizeof(enum player_color)); } // Set all the positions to empty @@ -49,59 +49,6 @@ void initialize_board() { board[4][4] = WHITE; } -/* Returns true if the move was valid */ -bool apply_move(enum player_color current_player, struct move move) { - - // Flip in up direction - bool flipped_up = false; - for (int i = move.row - 1; - i > 0 && board[i][move.col] != EMPTY && !flipped_up; i--) { - if (board[i][move.col] == current_player) { - for (int j = move.row; j > i; j--) { - board[j][move.col] = current_player; - } - flipped_up = true; - } - } - - // Flip in down direction - bool flipped_down = false; - for (int i = move.row + 1; - i < 8 && board[i][move.col] != EMPTY && !flipped_down; i++) { - if (board[i][move.col] == current_player) { - for (int j = move.row; j < i; j++) { - board[j][move.col] = current_player; - } - flipped_down = true; - } - } - - // Flip in left direction - bool flipped_left = false; - for (int i = move.col - 1; - i > 0 && board[move.row][i] != EMPTY && !flipped_left; i--) { - if (board[move.row][i] == current_player) { - for (int j = move.col; j > i; j--) { - board[move.row][j] = current_player; - } - flipped_left = true; - } - } - - // Flip in right direction - bool flipped_right = false; - for (int i = move.col + 1; - i < 8 && board[move.row][i] != EMPTY && !flipped_right; i++) { - if (board[move.row][i] == current_player) { - for (int j = move.col; j < i; j++) { - board[move.row][j] = current_player; - } - flipped_right = true; - } - } - - return flipped_up || flipped_down || flipped_left || flipped_right; -} enum player_color get_winner() { int white_score = 0, black_score = 0; diff --git a/src/game_loop.c b/src/game_loop.c index 22c624e..26cb988 100644 --- a/src/game_loop.c +++ b/src/game_loop.c @@ -68,8 +68,9 @@ enum player_color game_loop(FILE *player_one_strategy, #define other_player (current_player == WHITE ? BLACK : WHITE) while (has_valid_moves(current_player)) { - struct move move = prompt_get_move(current_player); - if (apply_move(current_player, move)) { + struct move move = current_player_move(current_player, player_one_strategy, + player_two_strategy); + if (apply_move(get_board(), current_player, move)) { current_player = other_player; } } @@ -53,10 +53,10 @@ static char *prompt_player(enum player_color current_player) { } } -static void inner_main(void *closure, int argc, char **argv) { - (void)closure; - scm_shell(argc, argv); -} +/*static void inner_main(void *closure, int argc, char **argv) {*/ + /*(void)closure;*/ + /*scm_shell(argc, argv);*/ +/*}*/ struct move prompt_get_move(enum player_color current_player) { // Initialize move to an invalid move. @@ -72,13 +72,13 @@ struct move prompt_get_move(enum player_color current_player) { print_help(); } else if (STREQ(input, "p")) { print_board(); - } else if (STREQ(input, "g")) { - int argc = 1; - char **argv = calloc(1, sizeof(char *)); - argv[0] = "othello"; - /*scm_with_guile(void *(*func)(void *), void *data)*/ - scm_boot_guile(argc, argv, inner_main, NULL); - free(argv); + /*} else if (STREQ(input, "g")) {*/ + /*int argc = 1;*/ + /*char **argv = calloc(1, sizeof(char *));*/ + /*argv[0] = "othello";*/ + /*[>scm_with_guile(void *(*func)(void *), void *data)<]*/ + /*scm_boot_guile(argc, argv, inner_main, NULL);*/ + /*free(argv);*/ } else { sscanf(input, "%d %d", &move.row, &move.col); } @@ -93,3 +93,97 @@ struct move prompt_get_move(enum player_color current_player) { return move; } + +/* Returns true if the move was valid */ +bool apply_move(enum player_color **board, enum player_color current_player, struct move move) { + + // The move must be a positive position. + if (move.row < 0 || move.col < 0) { + return false; + } + + // The move must be below the upper bounds of teh board. + if (move.row > 8 || move.col > 8) { + return false; + } + + // The move must be an empty spot. + if (board[move.row][move.col] != EMPTY) { + return false; + } + + // Flip in up direction + bool flipped_up = false; + for (int i = move.row - 1; + i > 0 && board[i][move.col] != EMPTY && !flipped_up; i--) { + if (board[i][move.col] == current_player) { + if (i < move.row - 1) { + for (int j = move.row; j > i; j--) { + board[j][move.col] = current_player; + } + flipped_up = true; + } else { + break; + } + } + } + + // Flip in down direction + bool flipped_down = false; + for (int i = move.row + 1; + i < 8 && board[i][move.col] != EMPTY && !flipped_down; i++) { + if (board[i][move.col] == current_player) { + if (i > move.row + 1) { + for (int j = move.row; j < i; j++) { + board[j][move.col] = current_player; + } + flipped_down = true; + } else { + break; + } + } + } + + // Flip in left direction + bool flipped_left = false; + for (int i = move.col - 1; + i > 0 && board[move.row][i] != EMPTY && !flipped_left; i--) { + if (board[move.row][i] == current_player) { + if (i < move.col - 1) { + for (int j = move.col; j > i; j--) { + board[move.row][j] = current_player; + } + flipped_left = true; + } else { + break; + } + } + } + + // Flip in right direction + bool flipped_right = false; + for (int i = move.col + 1; + i < 8 && board[move.row][i] != EMPTY && !flipped_right; i++) { + if (board[move.row][i] == current_player) { + if (i > move.col + 1) { + for (int j = move.col; j < i; j++) { + board[move.row][j] = current_player; + } + flipped_right = true; + } else { + break; + } + } + } + + return flipped_up || flipped_down || flipped_left || flipped_right; +} + +struct move get_scm_move(FILE *strategy) { + // Initialize move to an invalid move. + struct move move = {-1, -1}; + return move; + (void)strategy; +} + + diff --git a/src/print_board.c b/src/print_board.c index 98280fd..b0ce650 100644 --- a/src/print_board.c +++ b/src/print_board.c @@ -23,7 +23,7 @@ #include "othello.h" void print_board() { - const enum player_color **board = get_board(); + enum player_color **board = get_board(); puts(" 0 1 2 3 4 5 6 7"); diff --git a/src/valid_moves.c b/src/valid_moves.c index e8e72f6..7c3e676 100644 --- a/src/valid_moves.c +++ b/src/valid_moves.c @@ -24,7 +24,8 @@ bool is_valid_move(const enum player_color current_player, const struct move move) { - const enum player_color **board = get_board(); + return true; + enum player_color **board = get_board(); // The move must be a positive position. if (move.row < 0 || move.col < 0) { return false; |