From f694e733d5a799c7ec9f440af13118b2bfa44891 Mon Sep 17 00:00:00 2001
From: "dario.genga" <dario.genga@etu.hesge.ch>
Date: Tue, 21 Dec 2021 16:48:30 +0100
Subject: [PATCH] Add diagonal verification

Also fixed a typo in the readme.
---
 README.md   |  2 +-
 main.c      | 16 +++++++++++-----
 puissance.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 6 deletions(-)

diff --git a/README.md b/README.md
index 29e3b2e..26496aa 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 73c5683..ede0dfe 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 7109115..501b57f 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;
 }
-- 
GitLab