[iagno] Introduce a new AI.



commit 74bcbe56a46a6d63ef938f46ad2727afd98afde2
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri May 24 16:16:35 2019 +0200

    Introduce a new AI.
    
    A depth 4 should be
    enough with all the
    things we calculate
    and, in fact, feels
    a bit hard for now.

 src/computer-reversi.vala | 70 +++++++++++++++++++++--------------------------
 src/iagno.vala            | 11 +++++---
 2 files changed, 38 insertions(+), 43 deletions(-)
---
diff --git a/src/computer-reversi.vala b/src/computer-reversi.vala
index bf03d18..c7eb9ee 100644
--- a/src/computer-reversi.vala
+++ b/src/computer-reversi.vala
@@ -81,11 +81,8 @@ private class ComputerReversiEasy : ComputerReversi
 
 private class ComputerReversiHard : ComputerReversi
 {
-    private uint8 end_start;
-
     construct
     {
-        end_start = (size * size) - 10;
         init_heuristic (size, out heuristic);
     }
 
@@ -107,10 +104,10 @@ private class ComputerReversiHard : ComputerReversi
      // requires (a != null)
      // requires (b != null)
     {
-        if (((!) a).n_tiles >= ((!) b).n_tiles)
-            return -1;
-        else
-            return 1;
+        return (int) (heuristic [ ((!) b).x, ((!) b).y ]
+             -        heuristic [ ((!) a).x, ((!) a).y ])
+             + 16 * ((int) ((!) b).n_tiles
+             -       (int) ((!) a).n_tiles);
     }
 
     /*\
@@ -119,12 +116,7 @@ private class ComputerReversiHard : ComputerReversi
 
     protected override int16 calculate_heuristic (GameStateStruct g)
     {
-        /* End of the game: just maximize the number of tokens */
-        if (g.n_tiles >= end_start)
-            return (int16) g.n_current_tiles - (int16) g.n_opponent_tiles;
-
-        /* Normal strategy: try to evaluate the position */
-        return (int16) g.n_current_tiles - (int16) g.n_opponent_tiles + eval_heuristic (g, ref heuristic);
+        return eval_heuristic (g, ref heuristic);
     }
 
     private static inline int16 eval_heuristic (GameStateStruct g, ref int16 [,] heuristic)
@@ -146,8 +138,8 @@ private class ComputerReversiHard : ComputerReversi
                 // around
                 int16 a = (int16) g.get_empty_neighbors (x, y);
                 if (a == 0) // completely surrounded
-                    a = -2;
-                count += is_current_color ? -a : a;
+                    a = -7;
+                count += 4 * (is_current_color ? -a : a);
             }
         }
         return count;
@@ -161,14 +153,14 @@ private class ComputerReversiHard : ComputerReversi
 
     private const int16 [,] heuristic_8 =
     {
-        { 65,  -3, 6, 4, 4, 6,  -3, 65 },
-        { -3, -29, 3, 1, 1, 3, -29, -3 },
-        {  6,   3, 5, 3, 3, 5,   3,  6 },
-        {  4,   1, 3, 1, 1, 3,   1,  4 },
-        {  4,   1, 3, 1, 1, 3,   1,  4 },
-        {  6,   3, 5, 3, 3, 5,   3,  6 },
-        { -3, -29, 3, 1, 1, 3, -29, -3 },
-        { 65,  -3, 6, 4, 4, 6,  -3, 65 }
+        { 110,  35,  15,   5,   5,  15,  35, 110 },
+        {  35,  15,   5, -20, -20,   5,  15,  35 },
+        {  15,   5,  26,   7,   7,  26,   5,  15 },
+        {   5, -20,   7, -27, -27,   7, -20,   5 },
+        {   5, -20,   7, -27, -27,   7, -20,   5 },
+        {  15,   5,  26,   7,   7,  26,   5,  15 },
+        {  35,  15,   5, -20, -20,   5,  15,  35 },
+        { 110,  35,  15,   5,   5,  15,  35, 110 }
     };
 
     private static void init_heuristic (uint8 size, out int16 [,] heuristic)
@@ -190,29 +182,29 @@ private class ComputerReversiHard : ComputerReversi
 
         // corners
         uint8 tmp1 = size - 1;
-        heuristic [0   , 0   ] = 65;
-        heuristic [0   , tmp1] = 65;
-        heuristic [tmp1, tmp1] = 65;
-        heuristic [tmp1, 0   ] = 65;
+        heuristic [0   , 0   ] = 110;
+        heuristic [0   , tmp1] = 110;
+        heuristic [tmp1, tmp1] = 110;
+        heuristic [tmp1, 0   ] = 110;
 
         if (size >= 6)
         {
             // corners neighbors
             uint8 tmp2 = size - 2;
-            heuristic [0   , 1   ] = -3;
-            heuristic [0   , tmp2] = -3;
-            heuristic [tmp1, 1   ] = -3;
-            heuristic [tmp1, tmp2] = -3;
-            heuristic [1   , 0   ] = -3;
-            heuristic [1   , tmp1] = -3;
-            heuristic [tmp2, 0   ] = -3;
-            heuristic [tmp2, tmp1] = -3;
+            heuristic [0   , 1   ] = 35;
+            heuristic [0   , tmp2] = 35;
+            heuristic [tmp1, 1   ] = 35;
+            heuristic [tmp1, tmp2] = 35;
+            heuristic [1   , 0   ] = 35;
+            heuristic [1   , tmp1] = 35;
+            heuristic [tmp2, 0   ] = 35;
+            heuristic [tmp2, tmp1] = 35;
 
             // corners diagonal neighbors
-            heuristic [1   , 1   ] = -29;
-            heuristic [1   , tmp2] = -29;
-            heuristic [tmp2, tmp2] = -29;
-            heuristic [tmp2, 1   ] = -29;
+            heuristic [1   , 1   ] = 15;
+            heuristic [1   , tmp2] = 15;
+            heuristic [tmp2, tmp2] = 15;
+            heuristic [tmp2, 1   ] = 15;
         }
     }
 }
diff --git a/src/iagno.vala b/src/iagno.vala
index 4e1f72e..e38f080 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -376,10 +376,13 @@ private class Iagno : Gtk.Application
         else
         {
             uint8 computer_level = (uint8) settings.get_int ("computer-level");
-            if (computer_level == 1)
-                computer = new ComputerReversiEasy (game);
-            else
-                computer = new ComputerReversiHard (game, /* depth */ computer_level * 2);
+            switch (computer_level)
+            {
+                case 1 : computer = new ComputerReversiEasy (game);                break;
+                case 2 : computer = new ComputerReversiHard (game, /* depth */ 2); break;
+                case 3 : computer = new ComputerReversiHard (game, /* depth */ 4); break;
+                default: assert_not_reached ();
+            }
         }
 
         if (settings.get_enum ("color") == 1)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]