[iagno] game: add to_string and from_string



commit 41e4dec31c5d80fab40758a4da6384c1e17d41c6
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Fri Sep 27 22:46:53 2013 -0500

    game: add to_string and from_string

 src/game.vala |   29 +++++++++++++++++++++++++++++
 1 files changed, 29 insertions(+), 0 deletions(-)
---
diff --git a/src/game.vala b/src/game.vala
index a4a2bca..e80b4f5 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -120,6 +120,21 @@ public class Game : Object
         move ();
     }
 
+    public Game.from_strings (string[] setup, Player to_move, int width = 8, int height = 8)
+        requires (setup.length == height)
+        requires (setup[0].length == width)
+    {
+        tiles = new Player[width, height];
+
+        for (int y = 0; y < height; y++)
+            for (int x = 0; x < width; x++)
+                tiles[x, y] = Player.from_char (setup[y][x]);
+
+        current_color = to_move;
+
+        warn_if_fail (string.joinv ("\n", setup).strip () == to_string ().strip ());
+    }
+
     public Game.copy (Game game)
     {
         tiles = new Player[game.width, game.height];
@@ -292,6 +307,20 @@ public class Game : Object
         square_changed (x, y);
     }
 
+    public string to_string ()
+    {
+        string s = "\n";
+
+        for (int y = 0; y < height; y++)
+        {
+            for (int x = 0; x < width; x++)
+                s += tiles[x, y].to_string ();
+            s += "\n";
+        }
+
+        return s;
+    }
+
     private void flip_current_color ()
     {
         if (current_color == Player.LIGHT)


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