[iagno] Precalculate possible moves.



commit 79000c1315e4700e2889cfbeb1f0bdd7fa7bd9ab
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Tue May 14 11:02:00 2019 +0200

    Precalculate possible moves.

 src/computer-reversi.vala |  2 +-
 src/game.vala             | 35 ++++++++++++++++++++++++++++++-----
 2 files changed, 31 insertions(+), 6 deletions(-)
---
diff --git a/src/computer-reversi.vala b/src/computer-reversi.vala
index 7d35702..b3d29f9 100644
--- a/src/computer-reversi.vala
+++ b/src/computer-reversi.vala
@@ -288,7 +288,7 @@ private abstract class ComputerReversi : ComputerPlayer
         int16 a = LESS_THAN_NEGATIVE_INFINITY;
 
         SList<PossibleMove?> moves;
-        g.get_possible_moves (out moves);
+        game.get_possible_moves (out moves);    // like g.get_possible_moves, but pre-calculated
         sort_moves (ref moves);
 
         /* Try each move using alpha-beta pruning to optimise finding the best branch */
diff --git a/src/game.vala b/src/game.vala
index 2192000..f14918d 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -178,16 +178,19 @@ private class GameState : Object
     {
         moves = new SList<PossibleMove?> ();
 
-        for (; x_saved < size; x_saved++)
+        // use local variables so we can launch the method again with similar results
+        uint8 x = x_saved;
+        uint8 y = y_saved;
+        for (; x < size; x++)
         {
-            for (; y_saved < size; y_saved++)
+            for (; y < size; y++)
             {
                 uint8 n_tiles;
-                place_tile (x_saved, y_saved, current_color, /* apply move */ false, out n_tiles);
+                place_tile (x, y, current_color, /* apply move */ false, out n_tiles);
                 if (n_tiles != 0)
-                    moves.prepend (PossibleMove (x_saved, y_saved, n_tiles));
+                    moves.prepend (PossibleMove (x, y, n_tiles));
             }
-            y_saved = 0;
+            y = 0;
         }
     }
 
@@ -441,6 +444,7 @@ private class Game : Object
     construct
     {
         undo_stack.append (current_state);
+        update_possible_moves ();
     }
 
     internal Game (bool _alternative_start = false, uint8 _size = 8)
@@ -572,6 +576,7 @@ private class Game : Object
 
     private void end_of_turn (bool undoing, bool no_draw)
     {
+        update_possible_moves ();
         completeness_updated (current_state.is_complete);
         turn_ended (undoing, no_draw);
     }
@@ -633,4 +638,24 @@ private class Game : Object
         neighbor_tiles [max, max] = 3;
         neighbor_tiles [max, 0  ] = 3;
     }
+
+    /*\
+    * * possible moves
+    \*/
+
+    private SList<PossibleMove?> possible_moves;
+
+    internal void get_possible_moves (out SList<PossibleMove?> moves)
+    {
+        moves = possible_moves.copy_deep ((a) => {
+                if (a == null)
+                    assert_not_reached ();
+                return (PossibleMove) a;
+            });
+    }
+
+    private inline void update_possible_moves ()
+    {
+        current_state.get_possible_moves (out possible_moves);
+    }
 }


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