Add diagonal win checks

Adds diagonal win checking, and tests to go along with them
This commit is contained in:
Julia Lange 2024-07-16 01:46:42 -07:00
parent b3b369a076
commit 6ab989174b
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
2 changed files with 161 additions and 0 deletions

View file

@ -74,12 +74,76 @@ int check_for_win_vertical(struct Board *board, size_t pos_i, size_t pos_j)
return 0;
}
int check_for_win_lr_diagonal(struct Board *board, size_t pos_i, size_t pos_j)
{
enum Tile current_player = board->next_player * -1;
char in_a_row = 1;
// Down-right
size_t npos_i = pos_i + 1;
size_t npos_j = pos_j + 1;
while (npos_i < board->width && npos_j < board->height &&
IDX(npos_i, npos_j, board) == current_player) {
in_a_row++;
npos_i++;
npos_j++;
if (in_a_row >= 4)
return 1;
}
// Up-left
npos_i = pos_i - 1;
npos_j = pos_j - 1;
while (npos_i < board->width && npos_j < board->height &&
IDX(npos_i, npos_j, board) == current_player) {
in_a_row++;
npos_i--;
npos_j--;
if (in_a_row >= 4)
return 1;
}
return 0;
}
int check_for_win_rl_diagonal(struct Board *board, size_t pos_i, size_t pos_j)
{
enum Tile current_player = board->next_player * -1;
char in_a_row = 1;
// Down-left
size_t npos_i = pos_i - 1;
size_t npos_j = pos_j + 1;
while (npos_i < board->width && npos_j < board->height &&
IDX(npos_i, npos_j, board) == current_player) {
in_a_row++;
npos_i--;
npos_j++;
if (in_a_row >= 4)
return 1;
}
// Up-right
npos_i = pos_i + 1;
npos_j = pos_j - 1;
while (npos_i < board->width && npos_j < board->height &&
IDX(npos_i, npos_j, board) == current_player) {
in_a_row++;
npos_i++;
npos_j--;
if (in_a_row >= 4)
return 1;
}
return 0;
}
int check_for_win(struct Board *board, size_t pos_i, size_t pos_j)
{
if (check_for_win_horizontal(board, pos_i, pos_j))
return 1;
if (check_for_win_vertical(board, pos_i, pos_j))
return 1;
if (check_for_win_lr_diagonal(board, pos_i, pos_j))
return 1;
if (check_for_win_rl_diagonal(board, pos_i, pos_j))
return 1;
return 0;
}