summaryrefslogtreecommitdiff
path: root/src/valid_moves.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/valid_moves.c')
-rw-r--r--src/valid_moves.c69
1 files changed, 15 insertions, 54 deletions
diff --git a/src/valid_moves.c b/src/valid_moves.c
index 7c3e676..5913fb8 100644
--- a/src/valid_moves.c
+++ b/src/valid_moves.c
@@ -21,73 +21,34 @@
#include <stdbool.h>
#include "othello.h"
+#include "othello_move.h"
-bool is_valid_move(const enum player_color current_player,
+bool is_valid_move(enum player_color **board,
+ const enum player_color current_player,
const struct move move) {
- return true;
- enum player_color **board = get_board();
- // The move must be a positive position.
- if (move.row < 0 || move.col < 0) {
+ if (current_player == EMPTY) {
return false;
}
- // The move must be below the upper bounds of the board.
- if (move.row > 8 || move.col > 8) {
- return false;
- }
-
- // The move must be an empty spot to be valid.
- if (board[move.row][move.col] != EMPTY) {
- return false;
- }
-
- const enum player_color other_color = current_player == WHITE ? BLACK : WHITE;
-
- bool is_valid_up = false;
- bool is_valid_down = false;
- bool is_valid_left = false;
- bool is_valid_right = false;
-
-#define check_valid_direction(direction_bool, start, end_cond, step, cur_pos) \
- /* This if statement ensures that we don't check after one direction is \
- * already valid. \
- */ \
- if (!is_valid_up && !is_valid_down && !is_valid_left && !is_valid_right) { \
- bool would_flip_oppenent = false; \
- bool has_other_end = false; \
- for (int i = start; end_cond && !has_other_end; step) { \
- if (cur_pos == current_player) { \
- has_other_end = true; \
- } else if (cur_pos == other_color) { \
- would_flip_oppenent = true; \
- } else { \
- /* We have reached an empty tile*/ \
- break; \
- } \
- } \
- direction_bool = has_other_end && would_flip_oppenent; \
- }
-
- check_valid_direction(is_valid_up, move.row - 1, i > 0, i--,
- board[i][move.col]);
- check_valid_direction(is_valid_down, move.row + 1, i < 8, i++,
- board[i][move.col]);
- check_valid_direction(is_valid_left, move.col - 1, i > 0, i--,
- board[move.row][i]);
- check_valid_direction(is_valid_right, move.col + 1, i < 8, i++,
- board[move.row][i]);
+ // Make a copy of the input board. This is done because we lean on the logic
+ // that exists in apply_move to check if a move is valid. We don't want to
+ // necessarily mutate the board yet though.
+ enum player_color **b = copy_board(board);
+ // Apply the move to the copy of the board.
+ bool res = apply_move(b, current_player, move);
-#undef check_valid_direction
+ free_board(b);
- return is_valid_up || is_valid_down || is_valid_left || is_valid_right;
+ return res;
}
-bool has_valid_moves(const enum player_color current_player) {
+bool has_valid_moves(enum player_color **board,
+ const enum player_color current_player) {
bool result = false;
struct move move;
for (move.row = 0; move.row < 8 && !result; move.row++) {
for (move.col = 0; move.col < 8 && !result; move.col++) {
- result = is_valid_move(current_player, move);
+ result = is_valid_move(board, current_player, move);
}
}