summaryrefslogtreecommitdiff
path: root/src/move.c
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2022-01-05 22:50:17 -0500
committerRobby Zambito <contact@robbyzambito.me>2022-01-05 22:50:17 -0500
commit75607010bb4b095bc362c576927f7a7293514722 (patch)
tree8a92ca99137c7483fd15c3e7586b0d749d8eb45b /src/move.c
parent58324035132009869c406d486605be98725c83f1 (diff)
Make apply_move accept a board as a parameter.
This was how it originally was, but I changed it to use a global variable instead. I want to be able to use the apply_move function in the is_valid_move implementation, to have all the logic for checking if a move is valid in one place. Moved the apply_move function out of board.c, since it no longer needs a direct reference to the board.
Diffstat (limited to 'src/move.c')
-rw-r--r--src/move.c116
1 files changed, 105 insertions, 11 deletions
diff --git a/src/move.c b/src/move.c
index a22cb59..968dcfa 100644
--- a/src/move.c
+++ b/src/move.c
@@ -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;
+}
+
+