[gnome-games] chess: Correctly restore AI games



commit 8217c666d4f8d84b4330d7d24ca031b3d8b59449
Author: Robert Ancell <robert ancell canonical com>
Date:   Wed Jan 19 09:45:59 2011 +1100

    chess: Correctly restore AI games

 glchess/src/chess-game.vala |   57 ++++++++++++++++++-------------------------
 glchess/src/glchess.vala    |   51 ++++++++++++++++++++------------------
 2 files changed, 51 insertions(+), 57 deletions(-)
---
diff --git a/glchess/src/chess-game.vala b/glchess/src/chess-game.vala
index 2d924f6..331f78c 100644
--- a/glchess/src/chess-game.vala
+++ b/glchess/src/chess-game.vala
@@ -9,7 +9,6 @@ public class ChessPlayer : Object
     public Color color;
     public signal void start_turn ();
     public signal bool do_move (string move, bool apply);
-    public signal bool do_move_with_coords (int r0, int f0, int r1, int f1, bool apply);
     public signal bool do_resign ();
     public signal bool do_claim_draw ();
 
@@ -25,7 +24,8 @@ public class ChessPlayer : Object
 
     public bool move_with_coords (int r0, int f0, int r1, int f1, bool apply = true)
     {
-        return do_move_with_coords (r0, f0, r1, f1, apply);
+        string move = "%c%d%c%d".printf ('a' + f0, r0 + 1, 'a' + f1, r1 + 1);    
+        return do_move (move, apply);
     }
 
     public bool resign ()
@@ -878,6 +878,8 @@ public class ChessGame
     public ChessRule rule;
     public List<ChessState> move_stack;
 
+    public const string STANDARD_SETUP = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
+
     public signal void started ();
     public signal void turn_started (ChessPlayer player);
     public signal void moved (ChessMove move);
@@ -907,51 +909,46 @@ public class ChessGame
         }
     }
 
-    public ChessGame (string fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
+    public ChessGame (string fen = STANDARD_SETUP, string[]? moves = null)
     {
         is_started = false;
         move_stack.prepend (new ChessState (fen));
         result = ChessResult.IN_PROGRESS;
 
+        if (moves != null)
+        {
+            for (var i = 0; i < moves.length; i++)
+            {
+                if (!do_move (current_player, moves[i], true))
+                    warning ("Invalid move %s", moves[i]);
+            }
+        }
+
         white.do_move.connect (move_cb);
-        white.do_move_with_coords.connect (move_with_coords_cb);
         white.do_resign.connect (resign_cb);
         white.do_claim_draw.connect (claim_draw_cb);
         black.do_move.connect (move_cb);
-        black.do_move_with_coords.connect (move_with_coords_cb);
         black.do_resign.connect (resign_cb);
         black.do_claim_draw.connect (claim_draw_cb);
     }
 
     private bool move_cb (ChessPlayer player, string move, bool apply)
     {
-        return do_move (player, move, -1, -1, -1, -1, apply);
-    }
+        if (!is_started)
+            return false;
 
-    private bool move_with_coords_cb (ChessPlayer player, int r0, int f0, int r1, int f1, bool apply)
-    {
-        return do_move (player, null, r0, f0, r1, f1, apply);
+        return do_move (player, move, apply);
     }
 
-    private bool do_move (ChessPlayer player, string? move, int r0, int f0, int r1, int f1, bool apply)
+    private bool do_move (ChessPlayer player, string? move, bool apply)
     {
-        if (!is_started)
-            return false;
         if (player != current_player)
             return false;
 
         var state = move_stack.data.copy ();
         state.number++;
-        if (move != null)
-        {
-            if (!state.move (move, apply))
-                return false;
-        }
-        else
-        {
-            if (!state.move_with_coords (player, r0, f0, r1, f1, PieceType.QUEEN, apply))
-                return false;
-        }
+        if (!state.move (move, apply))
+            return false;
 
         if (!apply)
             return true;
@@ -966,6 +963,7 @@ public class ChessGame
 
         if (_clock != null)
             _clock.active_color = current_player.color;
+
         current_player.start_turn ();
         turn_started (current_player);
 
@@ -999,12 +997,13 @@ public class ChessGame
 
     public void start ()
     {
+        if (result != ChessResult.IN_PROGRESS)
+            return;
+
         if (is_started)
             return;
         is_started = true;
 
-        reset ();
-
         if (_clock != null)
         {
             _clock.expired.connect (clock_expired_cb);
@@ -1047,14 +1046,6 @@ public class ChessGame
         get { return move_stack.length() - 1; }
     }
 
-    private void reset ()
-    {
-        result = ChessResult.IN_PROGRESS;
-        var state = move_stack.data;
-        move_stack = null;
-        move_stack.prepend (state);
-    }
-
     private void stop (ChessResult result, ChessRule rule)
     {
         this.result = result;
diff --git a/glchess/src/glchess.vala b/glchess/src/glchess.vala
index 6a4f0f7..250eb78 100644
--- a/glchess/src/glchess.vala
+++ b/glchess/src/glchess.vala
@@ -201,18 +201,29 @@ public class Application
                            _("%1$s (%2$s) - Chess").printf (Path.get_basename (path), Path.get_dirname (path));
         }
 
+        var model = (Gtk.ListStore) history_combo.model;
+        model.clear ();
+        Gtk.TreeIter iter;
+        model.append (out iter);
+        model.set (iter, 0,
+                   /* Move History Combo: Go to the start of the game */
+                   _("Game Start"), 1, 0, -1);
+        history_combo.set_active_iter (iter);
+
+        string fen = ChessGame.STANDARD_SETUP;
+        string[] moves = new string[pgn_game.moves.length ()];
+        var i = 0;
+        foreach (var move in pgn_game.moves)
+            moves[i++] = move;
+
         if (pgn_game.set_up)
         {
             if (pgn_game.fen != null)
-                game = new ChessGame (pgn_game.fen);
+                fen = pgn_game.fen;
             else
-            {
                 warning ("Chess game has SetUp tag but no FEN tag");
-                game = new ChessGame ();            
-            }
         }
-        else
-            game = new ChessGame ();
+        game = new ChessGame (fen, moves);
 
         if (pgn_game.time_control != null)
         {
@@ -233,15 +244,6 @@ public class Application
         if (game.clock != null)
             game.clock.tick.connect (game_clock_tick_cb);
 
-        var model = (Gtk.ListStore) history_combo.model;
-        model.clear ();
-        Gtk.TreeIter iter;
-        model.append (out iter);
-        model.set (iter, 0,
-                   /* Move History Combo: Go to the start of the game */
-                   _("Game Start"), 1, 0, -1);
-        history_combo.set_active_iter (iter);
-
         view_options.game = game;
         info_bar.hide ();
         save_menu.sensitive = false;
@@ -279,17 +281,18 @@ public class Application
             opponent_engine.start ();
         }
 
-        game.start ();
-        foreach (var move in pgn_game.moves)
+        /* Replay current moves */
+        for (var j = (int) game.move_stack.length () - 2; j >= 0; j--)
         {
-            debug ("Move: %s", move);
-            if (!game.current_player.move (move))
-            {
-                warning ("Invalid move: %s", move);
-                break;
-            }
+            var state = game.move_stack.nth_data (j);
+            game_move_cb (game, state.last_move);
         }
 
+        game.start ();
+
+        if (game.result != ChessResult.IN_PROGRESS)
+            game_end_cb (game);
+
         white_time_label.queue_draw ();
         black_time_label.queue_draw ();
     }
@@ -815,7 +818,7 @@ public class Application
             return;
         int move_number;
         combo.model.get (iter, 1, out move_number, -1);
-        if (move_number == game.n_moves)
+        if (game == null || move_number == game.n_moves)
             move_number = -1;
         view_options.move_number = move_number;
     }



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