diff options
author | Robby Zambito <contact@robbyzambito.me> | 2022-01-22 13:38:01 -0500 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2022-01-22 13:38:01 -0500 |
commit | 9a5125eae7c105b02043ea327ce060884f9858f5 (patch) | |
tree | bb469ebe35ef978280ad2e8cb5a88e75120fba09 | |
parent | 14be7c446c2114ee14939ae7811d2018caee4c70 (diff) |
Change how move is applied
Use one loop for walking away from the move in each direction. Before
there was one loop for each direction, one after another. The other way
is probably slightly more efficient, but this seems quite a bit more
readable.
Also started working on applying the move diagonally, since both reversi
and othello seem to allow diagonal moves.
-rw-r--r-- | src/move.c | 143 |
1 files changed, 94 insertions, 49 deletions
@@ -112,68 +112,112 @@ int apply_move(enum player_color **board, enum player_color current_player, return 0; } - // Flip in up direction - int num_flipped_up = 0; - for (int i = move.row - 1; - i >= 0 && board[i][move.col] != EMPTY && num_flipped_up == 0; i--) { - if (board[i][move.col] == current_player) { - if (i < move.row - 1) { - for (int j = move.row; j > i; j--) { - board[j][move.col] = current_player; - num_flipped_up++; + int num_flipped_up = 0, num_flipped_down = 0, num_flipped_left = 0, + num_flipped_right = 0, num_flipped_up_left = 0, num_flipped_up_right = 0, + num_flipped_down_left = 0, num_flipped_down_right = 0; + + bool done_flipping_up = false, done_flipping_down = false, + done_flipping_left = false, done_flipping_right = false, + done_flipping_up_left = false, done_flipping_up_right = false, + done_flipping_down_left = false, done_flipping_down_right = false; + + for (int i = 1; i < 8; i++) { + if (!done_flipping_up && (move.row - i >= 0) && + board[move.row - i][move.col] == current_player) { + + if (i > 1) { + bool flippable = true; + for (int j = 1; flippable && j < i; j++) { + if (board[move.row - j][move.col] == EMPTY) { + flippable = false; + } + } + + if (flippable) { + for (int j = 0; j < i; j++) { + board[move.row - j][move.col] = current_player; + num_flipped_up++; + } } - } else { - break; } + + done_flipping_up = true; } - } + if (!done_flipping_down && (move.row + i < 8) && + board[move.row + i][move.col] == current_player) { + + if (i > 1) { + bool flippable = true; + for (int j = 1; flippable && j < i; j++) { + if (board[move.row + j][move.col] == EMPTY) { + flippable = false; + } + } - // Flip in down direction - int num_flipped_down = 0; - for (int i = move.row + 1; - i < 8 && board[i][move.col] != EMPTY && num_flipped_down == 0; i++) { - if (board[i][move.col] == current_player) { - if (i > move.row + 1) { - for (int j = move.row; j < i; j++) { - board[j][move.col] = current_player; - num_flipped_down++; + if (flippable) { + for (int j = 0; j < i; j++) { + board[move.row + j][move.col] = current_player; + num_flipped_down++; + } } - } else { - break; } + + done_flipping_down = true; } - } + if (!done_flipping_left && (move.col - i >= 0) && + board[move.row][move.col - i] == current_player) { + + if (i > 1) { + bool flippable = true; + for (int j = 1; flippable && j < i; j++) { + if (board[move.row][move.col - j] == EMPTY) { + flippable = false; + } + } - // Flip in left direction - int num_flipped_left = 0; - for (int i = move.col - 1; - i >= 0 && board[move.row][i] != EMPTY && num_flipped_left == 0; i--) { - if (board[move.row][i] == current_player) { - if (i < move.col - 1) { - for (int j = move.col; j > i; j--) { - board[move.row][j] = current_player; - num_flipped_left++; + if (flippable) { + for (int j = 0; j < i; j++) { + board[move.row][move.col - j] = current_player; + num_flipped_left++; + } } - } else { - break; } + + done_flipping_left = true; } - } + if (!done_flipping_right && (move.col + i < 8) && + board[move.row][move.col + i] == current_player) { + + if (i > 1) { + bool flippable = true; + for (int j = 1; flippable && j < i; j++) { + if (board[move.row][move.col + j] == EMPTY) { + flippable = false; + } + } - // Flip in right direction - int num_flipped_right = 0; - for (int i = move.col + 1; - i < 8 && board[move.row][i] != EMPTY && num_flipped_right == 0; i++) { - if (board[move.row][i] == current_player) { - if (i > move.col + 1) { - for (int j = move.col; j < i; j++) { - board[move.row][j] = current_player; - num_flipped_right++; + if (flippable) { + for (int j = 0; j < i; j++) { + board[move.row][move.col + j] = current_player; + num_flipped_right++; + } } - } else { - break; } + + done_flipping_right = true; } + /*if (!done_flipping_up_left &&) {*/ + /*done_flipping_up_left = true;*/ + /*}*/ + /*if (!done_flipping_up_right &&) {*/ + /*done_flipping_up_right = true;*/ + /*}*/ + /*if (!done_flipping_down_left &&) {*/ + /*done_flipping_down_left = true;*/ + /*}*/ + /*if (!done_flipping_down_right &&) {*/ + /*done_flipping_down_right = true;*/ + /*}*/ } return num_flipped_up + num_flipped_down + num_flipped_left + @@ -183,8 +227,9 @@ int apply_move(enum player_color **board, enum player_color current_player, SCM scm_apply_move(SCM scm_move, SCM scm_board, SCM scm_player) { struct move move = scm_move_to_c_move(scm_move); - enum player_color **board = - SCM_UNBNDP(scm_board) ? copy_board(get_board()) : scm_board_to_c_board(scm_board); + enum player_color **board = SCM_UNBNDP(scm_board) + ? copy_board(get_board()) + : scm_board_to_c_board(scm_board); enum player_color player = SCM_UNBNDP(scm_player) ? get_current_player() |