summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2021-07-26 00:22:07 -0400
committerRobby Zambito <contact@robbyzambito.me>2021-07-26 00:22:07 -0400
commit2f5ede7841826397b872c79ee2624c07e45f60ea (patch)
tree4cc84469879bc7a8af6250059d6f657a76b4ac6d /src
parent9960fa968af7d862a88eacc7b44a3c727ad0e3e8 (diff)
Made code DRYer by using macro.
The macro checks for the validity of a move in each direction.
Diffstat (limited to 'src')
-rw-r--r--src/valid_moves.c88
1 files changed, 24 insertions, 64 deletions
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;
}