From df33147bd53dcb1590828421b1ac9a157902d4ec Mon Sep 17 00:00:00 2001
From: "zabiulla.ahmadi" <zabiullah.ahmadi@etu.hesge.ch>
Date: Tue, 14 Jun 2022 20:02:44 +0200
Subject: [PATCH] alpha beta pruning

---
 play/play.c | 109 ++++++++++++++++++++++++++++++++++++++--------------
 play/play.h |   2 +-
 2 files changed, 81 insertions(+), 30 deletions(-)

diff --git a/play/play.c b/play/play.c
index 2df0ae2..fbadbb4 100755
--- a/play/play.c
+++ b/play/play.c
@@ -300,16 +300,16 @@ int max(int a, int b)
     return (a > b) ? a : b;
 }
 
-int mini_max(board brd, bool is_max, int depth)
+int mini_max(board brd, bool is_max, int depth, int *alpha, int *beta)
 {
 
-    if (brd->row >= 4)
-    {
-        if (depth >= 7)
-        {
-            return 0;
-        }
-    }
+    // if (brd->row >= 4)
+    // {
+    //     if (depth >= 10)
+    //     {
+    //         return 0;
+    //     }
+    // }
 
     int score = evaluate(brd);
 
@@ -339,9 +339,26 @@ int mini_max(board brd, bool is_max, int depth)
                     add_to_board(brd, i + 1, j + 1, PC);
 
                     // calculate score of this move
-                    best_score = max(best_score, mini_max(brd, false, depth + 1));
+                    // best_score = max(best_score, mini_max(brd, false, depth + 1, alpha, beta));
+                    best_score = mini_max(brd, !is_max, depth + 1, alpha, beta);
                     // undo move
                     add_to_board(brd, i + 1, j + 1, (char)0);
+
+                    // alpha Pruning
+
+                    if (best_score >= *beta)
+                    {
+                        i = brd->row;
+                        j = brd->col;
+                    }
+                    *alpha = max(*alpha, best_score);
+                    // *alpha = max(*alpha, best_score);
+                    // if (*alpha >= *beta)
+                    // {
+                    //     // custom break;
+                    //     // i = brd->row;
+                    //     j = brd->col;
+                    // }
                 }
             }
         }
@@ -361,11 +378,26 @@ int mini_max(board brd, bool is_max, int depth)
                     // make a move
                     add_to_board(brd, i + 1, j + 1, PLAYER);
 
-                    // calculate score of this move
-                    best_score = min(best_score, mini_max(brd, true, depth + 1));
-
+                    // best_score = min(best_score, mini_max(brd, true, depth + 1, alpha, beta));
+                    best_score = mini_max(brd, !is_max, depth + 1, alpha, beta);
                     // undo move
                     add_to_board(brd, i + 1, j + 1, (char)0);
+
+                    if (best_score <= *alpha)
+                    {
+                        i = brd->row;
+                        j = brd->col;
+                    }
+                    *beta = min(*beta, best_score);
+                    // beta cut-of Pruning
+
+                    // *beta = min(*beta, best_score);
+                    // if (*alpha >= *beta)
+                    // {
+                    //     // custom break;
+                    //     // i = brd->row;
+                    //     j = brd->col;
+                    // }
                 }
             }
         }
@@ -409,6 +441,9 @@ void best_move(board brd)
     bst_move.row = -1;
     bst_move.col = -1;
 
+    int alpha = _MIN_INF_;
+    int beta = _MAX_INF_;
+
     // for available case add and undo -> to see best score
     for (int i = 0; i < brd->row; i++)
     {
@@ -421,8 +456,8 @@ void best_move(board brd)
                 add_to_board(brd, i + 1, j + 1, PC);
 
                 // find max score
-                int score = mini_max(brd, false, 0);
-                printf("row: %d col: %d score : %d\n", i, j, score);
+                int score = mini_max(brd, false, 0, &alpha, &beta);
+                printf("row: %d col: %d score : %d , alpha %d, beta %d\n", i, j, score, alpha, beta);
                 // undo move
                 add_to_board(brd, i + 1, j + 1, (char)0);
                 // find the case with max probab
@@ -684,21 +719,19 @@ void player_vs_smart_ai_mini_max_player_start(board brd)
     }
 }
 
-int min_max_double(board brd, bool is_max, int depth, bool turn)
+int min_max_double(board brd, bool is_max, int depth, bool turn, int *alpha, int *beta)
 {
 
-    if (brd->row >= 4)
-    {
-        if (depth > 8)
-        {
-            return 0;
-        }
-    }
+    // if (brd->row >= 4)
+    // {
+    //     if (depth > 8)
+    //     {
+    //         return 0;
+    //     }
+    // }
 
     int score = evaluate(brd);
 
-    // if the board is full
-
     // if maximizer has won
     if (score == 1)
         return score * (remaining_case(brd) + 1);
@@ -723,9 +756,18 @@ int min_max_double(board brd, bool is_max, int depth, bool turn)
                     add_to_board(brd, i + 1, j + 1, (turn) ? PC : PLAYER);
 
                     // calculate score of this move
-                    best_score = max(best_score, mini_max(brd, false, depth + 1));
+                    // best_score = max(best_score, min_max_double(brd, false, depth + 1, turn, alpha, beta));
+                    best_score = mini_max(brd, true, depth + 1, alpha, beta);
                     // undo move
                     add_to_board(brd, i + 1, j + 1, (char)0);
+
+                    // alpha Pruning
+                    *alpha = max(*alpha, best_score);
+                    if (*alpha <= *beta)
+                    {
+                        i = brd->row;
+                        j = brd->col;
+                    }
                 }
             }
         }
@@ -746,12 +788,18 @@ int min_max_double(board brd, bool is_max, int depth, bool turn)
                     add_to_board(brd, i + 1, j + 1, (turn) ? PC : PLAYER);
 
                     // calculate score of this move
-                    best_score = min(best_score, mini_max(brd, true, depth + 1));
-
+                    // best_score = min(best_score, min_max_double(brd, true, depth + 1, turn, alpha, beta));
+                    best_score = mini_max(brd, true, depth + 1, alpha, beta);
                     // undo move
                     add_to_board(brd, i + 1, j + 1, (char)0);
 
-                    // min(score, best_score_score);
+                    // beta Pruning
+                    *beta = min(*beta, best_score);
+                    if (*beta <= *alpha)
+                    {
+                        i = brd->row;
+                        j = brd->col;
+                    }
                 }
             }
         }
@@ -766,6 +814,9 @@ void best_move_o(board brd, bool turn)
     bst_move.row = -1;
     bst_move.col = -1;
 
+    int alpha = _MIN_INF_;
+    int beta = _MAX_INF_;
+
     // for available case add and undo -> to see best score
     for (int i = 0; i < brd->row; i++)
     {
@@ -778,7 +829,7 @@ void best_move_o(board brd, bool turn)
                 add_to_board(brd, i + 1, j + 1, (turn) ? PC : PLAYER);
 
                 // find max score
-                int score = min_max_double(brd, false, 0, turn);
+                int score = min_max_double(brd, false, 0, turn, &alpha, &beta);
                 printf("row: %d col: %d score : %d\n", i, j, score);
                 // undo move
                 add_to_board(brd, i + 1, j + 1, (char)0);
diff --git a/play/play.h b/play/play.h
index 5c7b928..a2f53ac 100755
--- a/play/play.h
+++ b/play/play.h
@@ -31,7 +31,7 @@ int check_win(board brd);
 bool case_is_available(char chr);
 void case_to_coordinates(board brd, int cas, int *i, int *j);
 bool board_is_full(board brd);
-int mini_max(board brd, bool is_max, int depth);
+int mini_max(board brd, bool is_max, int depth, int *alpha, int *beta);
 void best_move(board brd);
 
 void two_player(board brd);
-- 
GitLab