[gnome-games] chess: Get undo working:



commit 1cd49e4ffee4026274c4416f9785f88cc4211c56
Author: Robert Ancell <robert ancell canonical com>
Date:   Tue Jan 25 22:10:00 2011 +1000

    chess: Get undo working:

 glchess/data/glchess.ui           |    4 ++++
 glchess/src/chess-engine-uci.vala |    9 +++++++--
 glchess/src/chess-game.vala       |    5 +----
 glchess/src/glchess.vala          |   34 +++++++++++++++++++++++++++++++++-
 4 files changed, 45 insertions(+), 7 deletions(-)
---
diff --git a/glchess/data/glchess.ui b/glchess/data/glchess.ui
index 4978305..b85883f 100644
--- a/glchess/data/glchess.ui
+++ b/glchess/data/glchess.ui
@@ -73,6 +73,7 @@
                       <object class="GtkImageMenuItem" id="undo_move_item">
                         <property name="label" translatable="yes" comments="Undo move menu item">_Undo Move</property>
                         <property name="visible">True</property>
+                        <property name="sensitive">False</property>
                         <property name="use_underline">True</property>
                         <property name="image">undo_move_image</property>
                         <property name="use_stock">False</property>
@@ -85,6 +86,7 @@
                       <object class="GtkImageMenuItem" id="resign_item">
                         <property name="label" translatable="yes" comments="Save menu item">_Resign</property>
                         <property name="visible">True</property>
+                        <property name="sensitive">False</property>
                         <property name="use_underline">True</property>
                         <property name="image">warning_image</property>
                         <property name="use_stock">False</property>
@@ -212,6 +214,7 @@
             <child>
               <object class="GtkToolButton" id="undo_move_button">
                 <property name="visible">True</property>
+                <property name="sensitive">False</property>
                 <property name="is_important">True</property>
                 <property name="label" translatable="yes" comments="The undo move toolbar button">Undo Move</property>
                 <property name="use_underline">True</property>
@@ -226,6 +229,7 @@
             <child>
               <object class="GtkToolButton" id="resign_button">
                 <property name="visible">True</property>
+                <property name="sensitive">False</property>
                 <property name="is_important">True</property>
                 <property name="label" translatable="yes" comments="The tooltip for the Resign toolbar button">Resign</property>
                 <property name="use_underline">True</property>
diff --git a/glchess/src/chess-engine-uci.vala b/glchess/src/chess-engine-uci.vala
index a7a9c02..d50a996 100644
--- a/glchess/src/chess-engine-uci.vala
+++ b/glchess/src/chess-engine-uci.vala
@@ -3,7 +3,8 @@ public class ChessEngineUCI : ChessEngine
     private char[] buffer;
     private string moves;
     private string[] options;
-    
+    private bool waiting_for_move;
+
     public ChessEngineUCI (string[] options)
     {
         this.options = options;
@@ -28,6 +29,7 @@ public class ChessEngineUCI : ChessEngine
             write_line ("position startpos moves" + moves);
         else
             write_line ("position startpos");
+        waiting_for_move = true;
         write_line ("go wtime 30000 btime 30000");
     }
 
@@ -38,7 +40,9 @@ public class ChessEngineUCI : ChessEngine
 
     public override void undo ()
     {
-        write_line ("stop");
+        if (waiting_for_move)
+            write_line ("stop");
+        waiting_for_move = false;
         moves = moves.slice (0, moves.last_index_of (" "));
     }
 
@@ -90,6 +94,7 @@ public class ChessEngineUCI : ChessEngine
                     if (tokens.length < 2)
                         warning ("No move with bestmove: %s", line);
                     debug ("Engine moves %s", tokens[1]);
+                    waiting_for_move = false;
                     moved (tokens[1]);
                     break;
 
diff --git a/glchess/src/chess-game.vala b/glchess/src/chess-game.vala
index 02e2e34..6e928b1 100644
--- a/glchess/src/chess-game.vala
+++ b/glchess/src/chess-game.vala
@@ -1266,12 +1266,9 @@ public class ChessGame
 
     private void undo_cb (ChessPlayer player)
     {
-        /* If this players turn undo their opponents move then their last move */
+        /* If this players turn undo their opponents move first */
         if (player == current_player)
-        {
             undo_cb (opponent);
-            return;
-        }
 
         /* Don't pop off starting move */
         if (move_stack.next == null)
diff --git a/glchess/src/glchess.vala b/glchess/src/glchess.vala
index 76cc26d..7c67fbf 100644
--- a/glchess/src/glchess.vala
+++ b/glchess/src/glchess.vala
@@ -14,6 +14,10 @@ public class Application
     private Gtk.Container view_container;
     private ChessViewOptions view_options;
     private ChessView view;
+    private Gtk.Widget undo_menu;
+    private Gtk.Widget undo_button;
+    private Gtk.Widget resign_menu;
+    private Gtk.Widget resign_button;
     private Gtk.Widget first_move_button;
     private Gtk.Widget prev_move_button;
     private Gtk.Widget next_move_button;
@@ -42,6 +46,7 @@ public class Application
     private File game_file;
     private List<AIProfile> ai_profiles;
     private ChessPlayer? opponent = null;
+    private ChessPlayer? human_player = null;
     private ChessEngine? opponent_engine = null;
 
     public Application ()
@@ -66,6 +71,10 @@ public class Application
         save_menu = (Gtk.Widget) builder.get_object ("menu_save_item");
         save_as_menu = (Gtk.Widget) builder.get_object ("menu_save_as_item");
         fullscreen_menu = (Gtk.MenuItem) builder.get_object ("menu_fullscreen");
+        undo_menu = (Gtk.Widget) builder.get_object ("undo_move_item");
+        undo_button = (Gtk.Widget) builder.get_object ("undo_move_button");
+        resign_menu = (Gtk.Widget) builder.get_object ("resign_item");
+        resign_button = (Gtk.Widget) builder.get_object ("resign_button");
         first_move_button = (Gtk.Widget) builder.get_object ("first_move_button");
         prev_move_button = (Gtk.Widget) builder.get_object ("prev_move_button");
         next_move_button = (Gtk.Widget) builder.get_object ("next_move_button");
@@ -266,6 +275,7 @@ public class Application
         save_menu.sensitive = false;
         save_as_menu.sensitive = false;
         update_history_panel ();
+        update_control_buttons ();
 
         // TODO: Could both be engines
         var white_engine = pgn_game.tags.lookup ("WhiteAI");
@@ -283,11 +293,13 @@ public class Application
         if (white_engine != null)
         {
             opponent = game.white;
+            human_player = game.black;
             opponent_engine = get_engine (white_engine, white_level);
         }
         else if (black_engine != null)
         {
             opponent = game.black;
+            human_player = game.white;
             opponent_engine = get_engine (black_engine, black_level);
         }
 
@@ -627,6 +639,7 @@ public class Application
         save_menu.sensitive = true;
         save_as_menu.sensitive = true;
         update_history_panel ();
+        update_control_buttons ();
 
         if (opponent_engine != null)
             opponent_engine.report_move (move);
@@ -655,6 +668,22 @@ public class Application
             history_combo.set_active_iter (iter);
             view.queue_draw ();
         }
+
+        update_history_panel ();
+        update_control_buttons ();
+    }
+    
+    private void update_control_buttons ()
+    {
+        var can_resign = game.n_moves > 0;
+        resign_menu.sensitive = resign_button.sensitive = can_resign;
+
+        /* Can undo once the human player has made a move */
+        var can_undo = game.n_moves > 0;
+        if (opponent != null && opponent.color == Color.WHITE)
+            can_undo = game.n_moves > 1;
+
+        undo_menu.sensitive = undo_button.sensitive = can_undo;
     }
 
     private void game_end_cb (ChessGame game)
@@ -786,7 +815,10 @@ public class Application
     [CCode (cname = "G_MODULE_EXPORT undo_move_cb", instance_pos = -1)]
     public void undo_move_cb (Gtk.Widget widget)
     {
-        game.current_player.undo ();
+        if (opponent != null)
+            human_player.undo ();
+        else
+            game.opponent.undo ();
     }
 
     [CCode (cname = "G_MODULE_EXPORT quit_cb", instance_pos = -1)]



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