summaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/board.c59
-rw-r--r--src/game_loop.c5
-rw-r--r--src/move.c116
-rw-r--r--src/print_board.c2
-rw-r--r--src/valid_moves.c3
5 files changed, 114 insertions, 71 deletions
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;
}
}
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;
+}
+
+
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;