Browse Source

Fix + Frequency-dependant method alternative

Vsevolod Levitan 4 tháng trước cách đây
mục cha
commit
c223f099ca
1 tập tin đã thay đổi với 104 bổ sung29 xóa
  1. 104 29
      MinMaxABFourInARowBoard.cs

+ 104 - 29
MinMaxABFourInARowBoard.cs

@@ -6,7 +6,7 @@ public class MinMaxABFourInARowBoard
 {
     protected static int EvaluateScoreDiff(int player, int computer)
     {
-        if (player == 0 && computer == 0) return 0;
+        if ((player == 0 && computer == 0) || (computer > 0 && player > 0)) return 0;
 
         if (computer > 0)
         {
@@ -40,44 +40,119 @@ public class MinMaxABFourInARowBoard
         [ -1, 1 ]
     ];
 
+    // public int CountSequences(GameBoard board)
+    // {
+    //     int score = 0;
+
+    //     for (int row = 0; row < board.dimensions.Rows; row++)
+    //     {
+    //         for (int col = 0; col < board.dimensions.Columns; col++)
+    //         {
+    //             for (int dir = 0; dir < 4; dir++)
+    //             {
+    //                 int rowDir = directions[dir][0];
+    //                 int colDir = directions[dir][1];
+
+    //                 int endRow = row + (3 * rowDir);
+    //                 int endCol = col + (3 * colDir);
+
+    //                 if (endRow >= 0 && endRow < board.dimensions.Rows &&
+    //                     endCol >= 0 && endCol < board.dimensions.Columns)
+    //                 {
+    //                     int aiCount = 0;
+    //                     int humanCount = 0;
+
+    //                     for (int k = 0; k < 4; k++)
+    //                     {
+    //                         int currentRow = row + (k * rowDir);
+    //                         int currentCol = col + (k * colDir);
+
+    //                         if (board.cells[currentCol][currentRow] == Player.Computer)
+    //                             aiCount++;
+    //                         else if (board.cells[currentCol][currentRow] != Player.None)
+    //                             humanCount++;
+    //                     }
+
+    //                     score += EvaluateScoreDiff(humanCount, aiCount);
+    //                 }
+    //             }
+    //         }
+    //     }
+
+    //     return score;
+    // }
+
+
+    // БЫСТРЕЕ ПРИ ВЫСОКОЙ ЧАСТОТЕ ПРОЦА
     public int CountSequences(GameBoard board)
     {
         int score = 0;
 
+        for (int col = 0; col < board.dimensions.Columns; col++)
+        {
+            for (int row = 0; row < board.dimensions.Rows - 3; row++)
+            {
+                int c = 0;
+                int p = 0;
 
+                for (int k = 0; k < 4; k++)
+                {
+                    if (board.cells[col][row + k] == Player.Computer) c++;
+                    else if (board.cells[col][row + k] != Player.None) p++;
+                }
+
+                score += EvaluateScoreDiff(p, c);
+            }
+        }
 
-        for (int row = 0; row < board.dimensions.Rows; row++)
+        for (int col = 0; col < board.dimensions.Columns - 3; col++)
         {
-            for (int col = 0; col < board.dimensions.Columns; col++)
+            for (int row = 0; row < board.dimensions.Rows; row++)
             {
-                for (int dir = 0; dir < 4; dir++)
+                int c = 0;
+                int p = 0;
+
+                for (int k = 0; k < 4; k++)
                 {
-                    int rowDir = directions[dir][0];
-                    int colDir = directions[dir][1];
-
-                    int endRow = row + (3 * rowDir);
-                    int endCol = col + (3 * colDir);
-
-                    if (endRow >= 0 && endRow < board.dimensions.Rows &&
-                        endCol >= 0 && endCol < board.dimensions.Columns)
-                    {
-                        int aiCount = 0;
-                        int humanCount = 0;
-
-                        for (int k = 0; k < 4; k++)
-                        {
-                            int currentRow = row + (k * rowDir);
-                            int currentCol = col + (k * colDir);
-
-                            if (board.cells[currentCol][currentRow] == Player.Computer)
-                                aiCount++;
-                            else if (board.cells[currentCol][currentRow] != Player.None)
-                                humanCount++;
-                        }
-
-                        score += EvaluateScoreDiff(humanCount, aiCount);
-                    }
+                    if (board.cells[col + k][row] == Player.Computer) c++;
+                    else if (board.cells[col + k][row] != Player.None) p++;
                 }
+
+                score += EvaluateScoreDiff(p, c);
+            }
+        }
+
+        for (int col = 0; col < board.dimensions.Columns - 3; col++)
+        {
+            for (int row = 0; row < board.dimensions.Rows - 3; row++)
+            {
+                int c = 0;
+                int p = 0;
+
+                for (int k = 0; k < 4; k++)
+                {
+                    if (board.cells[col + k][row + k] == Player.Computer) c++;
+                    else if (board.cells[col + k][row + k] != Player.None) p++;
+                }
+
+                score += EvaluateScoreDiff(p, c);
+            }
+        }
+
+        for (int col = 3; col < board.dimensions.Columns; col++)
+        {
+            for (int row = 0; row < board.dimensions.Rows - 3; row++)
+            {
+                int c = 0;
+                int p = 0;
+
+                for (int k = 0; k < 4; k++)
+                {
+                    if (board.cells[col - k][row + k] == Player.Computer) c++;
+                    else if (board.cells[col - k][row + k] != Player.None) p++;
+                }
+
+                score += EvaluateScoreDiff(p, c);
             }
         }