[gnome-games/glchess-vala] Support PGN games with FEN setup



commit 911588c86db69f76cad0dd2ada025dbbe831cabf
Author: Robert Ancell <robert ancell canonical com>
Date:   Mon Dec 27 12:47:46 2010 +1100

    Support PGN games with FEN setup

 glchess/src/chess-game.vala |   14 +++++++++-----
 glchess/src/chess-pgn.vala  |   15 +++++++++++++++
 glchess/src/glchess.vala    |   25 ++++++++++++++++++++-----
 3 files changed, 44 insertions(+), 10 deletions(-)
---
diff --git a/glchess/src/chess-game.vala b/glchess/src/chess-game.vala
index 873f15a..548b399 100644
--- a/glchess/src/chess-game.vala
+++ b/glchess/src/chess-game.vala
@@ -125,7 +125,7 @@ public class ChessState
     /* Bitmap of all the pieces */
     private int64 piece_masks[2];
 
-    public ChessState (string state = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
+    public ChessState (string? state = null)
     {
         players[Color.WHITE] = new ChessPlayer (Color.WHITE);
         players[Color.BLACK] = new ChessPlayer (Color.BLACK);
@@ -133,6 +133,9 @@ public class ChessState
         for (int i = 0; i < 64; i++)
             board[i] = null;
 
+        if (state == null)
+            return;
+
         string[] fields = state.split (" ");
         //if (fields.length != 6)
         //    throw new Error ("Invalid FEN string");
@@ -143,9 +146,10 @@ public class ChessState
         //    throw new Error ("Invalid piece placement");
         for (int rank = 0; rank < 8; rank++)
         {
-            for (int file = 0; file < 8;)
+            var rank_string = ranks[7 - rank];
+            for (int file = 0, offset = 0; file < 8 && offset < rank_string.length; offset++)
             {
-                var c = ranks[7 - rank][file];
+                var c = rank_string[offset];
                 if (c >= '1' && c <= '8')
                 {
                     file += c - '0';
@@ -625,10 +629,10 @@ public class ChessGame
         get { return move_stack.data.current_player; }
     }
 
-    public ChessGame ()
+    public ChessGame (string state = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
     {
         is_started = false;
-        move_stack.prepend (new ChessState ());
+        move_stack.prepend (new ChessState (state));
         result = ChessResult.IN_PROGRESS;
 
         white.do_move.connect (move_cb);
diff --git a/glchess/src/chess-pgn.vala b/glchess/src/chess-pgn.vala
index d805dc5..197bcf9 100644
--- a/glchess/src/chess-pgn.vala
+++ b/glchess/src/chess-pgn.vala
@@ -70,6 +70,21 @@ public class PGNGame
         get { return tags.lookup ("Result"); }
         set { tags.insert ("Result", value); }
     }
+    public bool set_up
+    {
+        get { string? v = tags.lookup ("SetUp"); return v != null && v == "1" ? true : false; }
+        set { tags.insert ("SetUp", value ? "1" : "0"); }
+    }
+    public string? fen
+    {
+        get { return tags.lookup ("FEN"); }
+        set { tags.insert ("FEN", value); }       
+    }
+    public string? termination
+    {
+        get { return tags.lookup ("Termination"); }
+        set { tags.insert ("Termination", value); }
+    }   
 
     public PGNGame ()
     {
diff --git a/glchess/src/glchess.vala b/glchess/src/glchess.vala
index bd40e3e..5387d48 100644
--- a/glchess/src/glchess.vala
+++ b/glchess/src/glchess.vala
@@ -151,9 +151,9 @@ public class Application
         update_history_panel ();
     }
 
-    private void start_game (bool use_ai = false)
+    private void start_game (ChessGame game)
     {
-        game = new ChessGame ();
+        this.game = game;
         game.started.connect (game_start_cb);
         game.turn_started.connect (game_turn_cb);
         game.moved.connect (game_move_cb);
@@ -235,7 +235,7 @@ public class Application
         if (game != null)
             load_game (game);
         else
-            start_game ();
+            start_game (new ChessGame ());
 
         if (settings.get_boolean ("fullscreen"))
             window.fullscreen ();
@@ -512,7 +512,7 @@ public class Application
     [CCode (cname = "G_MODULE_EXPORT new_game_cb", instance_pos = -1)]
     public void new_game_cb (Gtk.Widget widget)
     {
-        start_game ();
+        start_game (new ChessGame ());
     }
 
     [CCode (cname = "G_MODULE_EXPORT resign_cb", instance_pos = -1)]
@@ -1102,7 +1102,22 @@ public class Application
     {
         var pgn = new PGN.from_file (file);
         var pgn_game = pgn.games.nth_data (0);
-        start_game ();
+        
+        ChessGame game;
+        if (pgn_game.set_up)
+        {
+            if (pgn_game.fen != null)
+                game = new ChessGame (pgn_game.fen);
+            else
+            {
+                GLib.warning ("Chess game has SetUp tag but no FEN tag");
+                game = new ChessGame ();            
+            }
+        }
+        else
+            game = new ChessGame ();
+
+        start_game (game);
         foreach (var move in pgn_game.moves)
         {
             GLib.debug ("Move: %s", move);



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