From 4602e787c041fd2322fbd1f648d063a84652b5ff Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Tue, 31 Aug 2021 20:58:33 -0400 Subject: 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. --- src/valid_moves.c | 13 +++++++++++-- 1 file 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; \ -- cgit v1.2.3