[iagno] Disallow undo if only computer has moved



commit 9aaac95c94da4025003625e15414aab25f26fdd2
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sun Aug 3 11:07:50 2014 -0500

    Disallow undo if only computer has moved
    
    You can move first as Light if you undo the Dark computer's first move,
    which causes a crash.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=733708

 src/game.vala       |   11 ++++++++---
 src/iagno.vala      |    7 +++++--
 src/test-iagno.vala |   14 ++++++++++++++
 3 files changed, 27 insertions(+), 5 deletions(-)
---
diff --git a/src/game.vala b/src/game.vala
index 153e7f0..5f58f07 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -228,15 +228,20 @@ public class Game : Object
         return enemy_count;
     }
 
-    public bool can_undo ()
+    public bool can_undo (int count = 1)
+        requires (count == 1 || count == 2)
     {
-        return undo_index > 0;
+        /* Can undo one move if a move has been played */
+        if (count == 1)
+            return undo_index > 0;
+        else /* Can undo two moves only after the second ply; after the first ply, undo_index == 3 */
+            return undo_index > 3;
     }
 
     public void undo (int count = 1)
         requires (count == 1 || count == 2)
     {
-        if (!can_undo ())
+        if (!can_undo (count))
             return;
 
         for (var i = 0; i < count; i++)
diff --git a/src/iagno.vala b/src/iagno.vala
index 3f04658..d2ca6fd 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -306,10 +306,13 @@ public class Iagno : Gtk.Application
         headerbar.set_subtitle (null);
 
         var undo_action = (SimpleAction) lookup_action ("undo-move");
-        undo_action.set_enabled (game.can_undo ());
+        if (player_one == Player.DARK || computer == null)
+            undo_action.set_enabled (game.can_undo (1));
+        else
+            undo_action.set_enabled (game.can_undo (2));
 
         var new_game_action = (SimpleAction) lookup_action ("new-game");
-        new_game_action.set_enabled (game.can_undo ());
+        new_game_action.set_enabled (game.can_undo (1));
 
         if (game.current_color == Player.DARK)
         {
diff --git a/src/test-iagno.vala b/src/test-iagno.vala
index f1c79c0..f625869 100644
--- a/src/test-iagno.vala
+++ b/src/test-iagno.vala
@@ -45,6 +45,19 @@ public class TestIagno : Object
         assert (game.to_string ().strip () == string.joinv ("\n", board).strip ());
     }
 
+    private static void test_undo_at_start ()
+    {
+        Game game = new Game ();
+        assert (!game.can_undo (1));
+        assert (!game.can_undo (2));
+        game.place_tile (2, 3);
+        assert (game.can_undo (1));
+        assert (!game.can_undo (2));
+        game.place_tile (2, 2);
+        assert (game.can_undo (1));
+        assert (game.can_undo (2));
+    }
+
     private static void test_current_color_after_pass ()
     {
         string[] board = {"L LLLLLL",
@@ -147,6 +160,7 @@ public class TestIagno : Object
     public static int main (string[] args) {
         Test.init (ref args);
         Test.add_func ("/Iagno/Pass then Undo", test_undo_after_pass);
+        Test.add_func ("/Iagno/Undo at Start", test_undo_at_start);
         Test.add_func ("/Iagno/Current Color after Pass", test_current_color_after_pass);
         Test.add_func ("/Iagno/AI Search 1", test_ai_search_1);
         Test.add_func ("/Iagno/AI Search 2", test_ai_search_2);


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