[iagno/gnome-3-12] Disallow undo if only computer has moved
- From: Michael Catanzaro <mcatanzaro src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [iagno/gnome-3-12] Disallow undo if only computer has moved
- Date: Wed, 13 Aug 2014 02:01:16 +0000 (UTC)
commit d5eda2e4447352612f4632df25b0b29c52151ba6
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 | 4 +++-
src/test-iagno.vala | 14 ++++++++++++++
3 files changed, 25 insertions(+), 4 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 fe5b076..eb631a7 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -253,8 +253,10 @@ public class Iagno : Gtk.Application
/* Can't undo when running two computer players */
if (light_computer != null && dark_computer != null)
undo_action.set_enabled (false);
+ else if (light_computer != null)
+ undo_action.set_enabled (game.can_undo (1));
else
- undo_action.set_enabled (game.can_undo ());
+ undo_action.set_enabled (game.can_undo (2));
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]