Skip to content
Snippets Groups Projects
Commit f694e733 authored by dario.genga's avatar dario.genga
Browse files

Add diagonal verification

Also fixed a typo in the readme.
parent 543897ff
Branches
No related tags found
No related merge requests found
......@@ -17,7 +17,7 @@ Use this command to compile the project.
Use this command to clean the project.
### Run the tests
> `make test`
> `make tests`
>
> `./test`
......
......@@ -13,16 +13,22 @@ int main() {
puissance game;
game_init(&game, RAND_AI, DEFAULT_ROW, DEFAULT_COL);
manual_play(&game, 4);
print_game(game);
manual_play(&game, 0);
manual_play(&game, 1);
print_game(game);
manual_play(&game, 1);
manual_play(&game, 2);
print_game(game);
manual_play(&game, 3);
manual_play(&game, 0);
manual_play(&game, 2);
print_game(game);
manual_play(&game, 6);
manual_play(&game, 2);
manual_play(&game, 3);
print_game(game);
manual_play(&game, 1);
manual_play(&game, 4);
manual_play(&game, 3);
print_game(game);
manual_play(&game, 3);
print_game(game);
game_destroy(&game);
......
......@@ -197,11 +197,63 @@ GameResult horizontal_game_check(puissance *p, int last_col_index_played) {
return ONGOING;
}
bool diagonal_parse(puissance *p, int x_r, int y_c, int starting_row, int starting_col) {
bool four_aligned = true;
int last_played_value = p->data[starting_row][starting_col];
for (int i = 1; i <= NB_VERIFICATION_FOR_WIN; i++) {
int r = x_r * i + starting_row;
int c = y_c * i + starting_col;
if (r < 0 || r >= p->row || c < 0 || c >= p->col) {
return false;
}
if (p->data[r][c] != last_played_value) {
four_aligned = false;
break;
}
}
return four_aligned;
}
GameResult diagonal_game_check(puissance *p, int last_col_index_played) {
if (p->state != ONGOING) {
return p->state;
}
bool four_aligned = false;
// Get the row index
int last_row_index_played = get_available_row_index(p, last_col_index_played) + 1;
// down right
if (diagonal_parse(p, 1, 1, last_row_index_played, last_col_index_played)) {
return get_winning_player(p);
}
// up left
if (diagonal_parse(p, -1, -1, last_row_index_played, last_col_index_played)) {
return get_winning_player(p);
}
// up right
if (diagonal_parse(p, -1, 1, last_row_index_played, last_col_index_played)) {
return get_winning_player(p);
}
// down left
if (diagonal_parse(p, 1, -1, last_row_index_played, last_col_index_played)) {
return get_winning_player(p);
}
return ONGOING;
}
GameResult verify_game(puissance *p, int last_col_index_played) {
// Vertical check
vertical_game_check(p, last_col_index_played);
// Horizontal check
horizontal_game_check(p, last_col_index_played);
// Diagonal check
diagonal_game_check(p, last_col_index_played);
return p->state;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment