[iagno] Use less undo.



commit 88a5dd702443e17abbd5c8f8a342605d6c03e6a8
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Tue Feb 26 19:07:37 2019 +0100

    Use less undo.
    
    It is hard to estimate what strategy
    will feel better, from a performance
    point of view. But, if we want (and,
    we do) to save the calculations done
    between two computer moves, to avoid
    always recalculating all the things,
    this should probably help. So, done.

 src/computer-player.vala | 28 ++++++++++++++--------------
 src/game.vala            |  9 +++++----
 2 files changed, 19 insertions(+), 18 deletions(-)
---
diff --git a/src/computer-player.vala b/src/computer-player.vala
index 3e70c16..df6b51c 100644
--- a/src/computer-player.vala
+++ b/src/computer-player.vala
@@ -230,21 +230,21 @@ private class ComputerPlayer : Object
             if (move == null)
                 assert_not_reached ();
 
-            if (g.place_tile (((!) move).x, ((!) move).y, true) == 0)
+            Game _g = new Game.copy (g);
+
+            if (_g.place_tile (((!) move).x, ((!) move).y, true) == 0)
             {
-                critical ("Computer marked move (depth %d, %d,%d, %d flips) as valid, but is invalid when 
checking.\n%s", depth, ((!) move).x, ((!) move).y, ((!) move).n_tiles, g.to_string ());
+                critical ("Computer marked move (depth %d, %d,%d, %d flips) as valid, but is invalid when 
checking.\n%s", depth, ((!) move).x, ((!) move).y, ((!) move).n_tiles, _g.to_string ());
                 assert_not_reached ();
             }
 
-            int a_new = -1 * search (g, depth, NEGATIVE_INFINITY, -a);
+            int a_new = -1 * search (_g, depth, NEGATIVE_INFINITY, -a);
             if (a_new > a)
             {
                 a = a_new;
                 x = ((!) move).x;
                 y = ((!) move).y;
             }
-
-            g.undo ();
         }
     }
 
@@ -276,18 +276,18 @@ private class ComputerPlayer : Object
                 if (move == null)
                     assert_not_reached ();
 
-                if (g.place_tile (((!) move).x, ((!) move).y) == 0)
+                Game _g = new Game.copy (g);
+
+                if (_g.place_tile (((!) move).x, ((!) move).y) == 0)
                 {
-                    critical ("Computer marked move (depth %d, %d,%d, %d flips) as valid, but is invalid 
when checking.\n%s", depth, ((!) move).x, ((!) move).y, ((!) move).n_tiles, g.to_string ());
+                    critical ("Computer marked move (depth %d, %d,%d, %d flips) as valid, but is invalid 
when checking.\n%s", depth, ((!) move).x, ((!) move).y, ((!) move).n_tiles, _g.to_string ());
                     assert_not_reached ();
                 }
 
-                int a_new = -1 * search (g, depth - 1, -b, -a);
+                int a_new = -1 * search (_g, depth - 1, -b, -a);
                 if (a_new > a)
                     a = a_new;
 
-                g.undo ();
-
                 /* This branch has worse values, so ignore it */
                 if (b <= a)
                     break;
@@ -295,13 +295,13 @@ private class ComputerPlayer : Object
         }
         else
         {
-            g.pass ();
+            Game _g = new Game.copy (g);
+
+            _g.pass ();
 
-            int a_new = -1 * search (g, depth - 1, -b, -a);
+            int a_new = -1 * search (_g, depth - 1, -b, -a);
             if (a_new > a)
                 a = a_new;
-
-            g.undo ();
         }
 
         return a;
diff --git a/src/game.vala b/src/game.vala
index f5f5573..4e7436e 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -193,10 +193,11 @@ private class Game : Object
             for (uint8 y = 0; y < size; y++)
                 tiles [x, y] = game.tiles [x, y];
 
-        number_of_moves  = game.number_of_moves;
-        current_color    = game.current_color;
-        n_current_tiles  = game.n_current_tiles;
-        n_opponent_tiles = game.n_opponent_tiles;
+        number_of_moves         = game.number_of_moves;
+        current_color           = game.current_color;
+        n_current_tiles         = game.n_current_tiles;
+        n_opponent_tiles        = game.n_opponent_tiles;
+        current_player_can_move = game.current_player_can_move;
 
         init_undo_stack (_size, out undo_stack);
         /* warning: history not copied */


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