diff --git a/README.md b/README.md index 29e3b2e423012d5d775b814db7e32132b8450671..26496aa32f4225bcb284c4beb5e1f742c2fb8bd3 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/main.c b/main.c index 73c56830d18871b324522fed7ee306a2f47bcf3c..ede0dfe0247b93909169e9062c9b7ff902012a40 100644 --- a/main.c +++ b/main.c @@ -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); diff --git a/puissance.c b/puissance.c index 710911558d2ce8574efe6a9ce2643e1e6c197835..501b57fa9783dc1f2d3446e33ff28eff9bd477c0 100644 --- a/puissance.c +++ b/puissance.c @@ -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; }