From 2f5ede7841826397b872c79ee2624c07e45f60ea Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Mon, 26 Jul 2021 00:22:07 -0400 Subject: Made code DRYer by using macro. The macro checks for the validity of a move in each direction. --- src/valid_moves.c | 88 +++++++++++++++---------------------------------------- 1 file changed, 24 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/valid_moves.c b/src/valid_moves.c index 445f654..7dccd7f 100644 --- a/src/valid_moves.c +++ b/src/valid_moves.c @@ -21,73 +21,33 @@ bool is_valid_move(const enum player_color board[8][8], bool is_valid_left = false; bool is_valid_right = false; - if (!is_valid_up && !is_valid_down && !is_valid_left && !is_valid_right) { - // Check if valid up - bool would_flip_oppenent = false; - bool has_other_end = false; - for (int i = move.row - 1; i > 0 && !has_other_end; i--) { - if (board[i][move.col] == current_player) { - has_other_end = true; - } else if (board[i][move.col] == other_color) { - would_flip_oppenent = true; - } else { - // We have reached an empty tile - break; - } - } - is_valid_up = has_other_end && would_flip_oppenent; - } - - if (!is_valid_up && !is_valid_down && !is_valid_left && !is_valid_right) { - // Check if valid down - bool would_flip_oppenent = false; - bool has_other_end = false; - for (int i = move.row + 1; i < 8 && !has_other_end; i++) { - if (board[i][move.col] == current_player) { - has_other_end = true; - } else if (board[i][move.col] == other_color) { - would_flip_oppenent = true; - } else { - // We have reached an empty tile - break; - } - } - is_valid_down = has_other_end && would_flip_oppenent; +#define check_valid_direction(direction_bool, start, end_cond, step, cur_pos) \ + 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; \ } - if (!is_valid_up && !is_valid_down && !is_valid_left && !is_valid_right) { - // Check if valid left - bool would_flip_oppenent = false; - bool has_other_end = false; - for (int i = move.col - 1; i > 0 && !has_other_end; i--) { - if (board[move.row][i] == current_player) { - has_other_end = true; - } else if (board[move.row][i] == other_color) { - would_flip_oppenent = true; - } else { - // We have reached an empty tile - break; - } - } - is_valid_left = 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]); - if (!is_valid_up && !is_valid_down && !is_valid_left && !is_valid_right) { - // Check if valid right - bool would_flip_oppenent = false; - bool has_other_end = false; - for (int i = move.col + 1; i < 8 && !has_other_end; i++) { - if (board[move.row][i] == current_player) { - has_other_end = true; - } else if (board[move.row][i] == other_color) { - would_flip_oppenent = true; - } else { - // We have reached an empty tile - break; - } - } - is_valid_right = has_other_end && would_flip_oppenent; - } +#undef check_valid_direction return is_valid_up || is_valid_down || is_valid_left || is_valid_right; } -- cgit v1.2.3