diff options
author | Robby Zambito <contact@robbyzambito.me> | 2021-08-31 20:58:33 -0400 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2021-08-31 20:58:33 -0400 |
commit | 4602e787c041fd2322fbd1f648d063a84652b5ff (patch) | |
tree | dfeebcbf9a00c2f41edb7226f7165e138c8deb5f /src | |
parent | a3d620344709d17a713fbc6b1576e0467bccf974 (diff) |
Add check for upper bounds to see if a move is valid.
This makes sure we don't accidentally overrun the board.
Also added a comment as to why we have the if statement in the
check_valid_direction macro.
Diffstat (limited to 'src')
-rw-r--r-- | src/valid_moves.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/valid_moves.c b/src/valid_moves.c index 7f7658c..dca1a0f 100644 --- a/src/valid_moves.c +++ b/src/valid_moves.c @@ -23,11 +23,17 @@ 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 + // The move must be a positive position. if (move.row < 0 || move.col < 0) { return false; } - // The move must be an empty spot to be valid + + // 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; } @@ -40,6 +46,9 @@ bool is_valid_move(const enum player_color board[8][8], 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; \ |