[iagno] Better import/export.



commit 07fef79737b4a50bff46acc45c9f4c458be440f3
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Sep 20 19:44:09 2014 +0200

    Better import/export.

 src/game.vala       |   36 +++++++++--------
 src/player.vala     |   13 ++----
 src/test-iagno.vala |  112 +++++++++++++++++++++++++-------------------------
 3 files changed, 80 insertions(+), 81 deletions(-)
---
diff --git a/src/game.vala b/src/game.vala
index f97a29e..1a0f723 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -107,21 +107,37 @@ public class Game : Object
     public Game.from_strings (string[] setup, Player to_move, int tmp_size = 8)
         requires (tmp_size >= 4)
         requires (setup.length == tmp_size)
-        /* warning, only testing the first string */
-        requires (setup[0].length == tmp_size)
     {
         size = tmp_size;
         tiles = new Player[size, size];
 
         for (int y = 0; y < size; y++)
+        {
+            if (setup[y].length != size * 2)
+                warn_if_reached ();
             for (int x = 0; x < size; x++)
-                tiles[x, y] = Player.from_char (setup[y][x]);
+                tiles[x, y] = Player.from_char (setup[y][x * 2 + 1]);
+        }
 
         current_color = to_move;
 
         warn_if_fail (string.joinv ("\n", setup).strip () == to_string ().strip ());
     }
 
+    public string to_string ()
+    {
+        string s = "\n";
+
+        for (int y = 0; y < size; y++)
+        {
+            for (int x = 0; x < size; x++)
+                s += " " + tiles[x, y].to_string ();
+            s += "\n";
+        }
+
+        return s;
+    }
+
     public Game.copy (Game game)
     {
         size = game.size;
@@ -136,20 +152,6 @@ public class Game : Object
         /* don't copy history */
     }
 
-    public string to_string ()
-    {
-        string s = "\n";
-
-        for (int y = 0; y < size; y++)
-        {
-            for (int x = 0; x < size; x++)
-                s += tiles[x, y].to_string ();
-            s += "\n";
-        }
-
-        return s;
-    }
-
     /*\
     * * Public information
     \*/
diff --git a/src/player.vala b/src/player.vala
index 4a3bb83..53d46bd 100644
--- a/src/player.vala
+++ b/src/player.vala
@@ -34,12 +34,12 @@ public enum Player
             return "D";
         default:
             warn_if_fail (this == NONE);
-            return " ";
+            return ".";
         }
     }
 
     public static Player from_char (char c)
-        requires (c == 'L' || c == 'D' || c == ' ')
+        requires (c == 'L' || c == 'D' || c == '.')
     {
         switch (c)
         {
@@ -47,7 +47,7 @@ public enum Player
             return LIGHT;
         case 'D':
             return DARK;
-        case ' ':
+        case '.':
             return NONE;
         default:
             warn_if_reached ();
@@ -56,12 +56,9 @@ public enum Player
     }
 
     public static Player flip_color (Player p)
-        requires (p == Player.LIGHT || p == Player.DARK)
+        requires (p != Player.NONE)
     {
-        if (p == Player.LIGHT)
-            return Player.DARK;
-        else
-            return Player.LIGHT;
+        return p == Player.LIGHT ? Player.DARK : Player.LIGHT;
     }
 }
 
diff --git a/src/test-iagno.vala b/src/test-iagno.vala
index f625869..95e08b0 100644
--- a/src/test-iagno.vala
+++ b/src/test-iagno.vala
@@ -22,14 +22,14 @@ public class TestIagno : Object
 {
     private static void test_undo_after_pass ()
     {
-        string[] board = {"    LLLL",
-                          "   LLLLD",
-                          "  LLLLD ",
-                          "  LLLDLL",
-                          " LLLDLLL",
-                          " LLDDLLL",
-                          "LLLLLLLL",
-                          "LLLLLLLL"};
+        string[] board = {" . . . . L L L L",
+                          " . . . L L L L D",
+                          " . . L L L L D .",
+                          " . . L L L D L L",
+                          " . L L L D L L L",
+                          " . L L D D L L L",
+                          " L L L L L L L L",
+                          " L L L L L L L L"};
         Game game = new Game.from_strings (board, Player.DARK);
         assert (game.place_tile (7, 2) > 0);
         assert (!game.can_move (Player.LIGHT));
@@ -60,14 +60,14 @@ public class TestIagno : Object
 
     private static void test_current_color_after_pass ()
     {
-        string[] board = {"L LLLLLL",
-                          "LLLLLLLL",
-                          "L LLLLLL",
-                          "LDLLLLLL",
-                          "LDDLDLLL",
-                          "LDLDLLLL",
-                          "DDDDDLLL",
-                          "DDDDDDDD"};
+        string[] board = {" L . L L L L L L",
+                          " L L L L L L L L",
+                          " L . L L L L L L",
+                          " L D L L L L L L",
+                          " L D D L D L L L",
+                          " L D L D L L L L",
+                          " D D D D D L L L",
+                          " D D D D D D D D"};
         Game game = new Game.from_strings (board, Player.DARK);
         assert (game.current_color == Player.DARK);
         assert (game.place_tile (1, 2) > 0);
@@ -79,14 +79,14 @@ public class TestIagno : Object
 
     private static void test_ai_search_1 ()
     {
-        string[] board = {"L  LLLLL",
-                          "LLDDDDDD",
-                          "DDDDDLDD",
-                          "LDLLLLLL",
-                          "LLDLDDLL",
-                          "LLDDLLLL",
-                          "LLLLLLLL",
-                          "LLLLLLLL"};
+        string[] board = {" L . . L L L L L",
+                          " L L D D D D D D",
+                          " D D D D D L D D",
+                          " L D L L L L L L",
+                          " L L D L D D L L",
+                          " L L D D L L L L",
+                          " L L L L L L L L",
+                          " L L L L L L L L"};
         Game game = new Game.from_strings (board, Player.LIGHT);
         ComputerPlayer ai = new ComputerPlayer (game);
         ai.move ();
@@ -95,14 +95,14 @@ public class TestIagno : Object
 
     private static void test_ai_search_2 ()
     {
-        string[] board = {"        ",
-                          "        ",
-                          "   D    ",
-                          "   DD   ",
-                          "   DLL  ",
-                          "  DDD   ",
-                          "   D    ",
-                          "  D     "};
+        string[] board = {" . . . . . . . .",
+                          " . . . . . . . .",
+                          " . . . D . . . .",
+                          " . . . D D . . .",
+                          " . . . D L L . .",
+                          " . . D D D . . .",
+                          " . . . D . . . .",
+                          " . . D . . . . ."};
         Game game = new Game.from_strings (board, Player.LIGHT);
         ComputerPlayer ai = new ComputerPlayer (game);
         ai.move ();
@@ -111,14 +111,14 @@ public class TestIagno : Object
 
     private static void test_ai_search_3 ()
     {
-        string[] board = {"DL DDDDD",
-                          "DLDDDDDD",
-                          "DDLDLDDD",
-                          "DLDDLDDD",
-                          "DDLDLLLD",
-                          "DLDDLDLD",
-                          "LLLLLDDD",
-                          "DDDDDDDD"};
+        string[] board = {" D L . D D D D D",
+                          " D L D D D D D D",
+                          " D D L D L D D D",
+                          " D L D D L D D D",
+                          " D D L D L L L D",
+                          " D L D D L D L D",
+                          " L L L L L D D D",
+                          " D D D D D D D D"};
         Game game = new Game.from_strings (board, Player.LIGHT);
         ComputerPlayer ai = new ComputerPlayer (game);
         ai.move ();
@@ -127,14 +127,14 @@ public class TestIagno : Object
 
     private static void test_ai_search_4 ()
     {
-        string[] board = {"  LDDDDD",
-                          "DLDLDLDD",
-                          "DDDLLDLD",
-                          "DDLLDLDD",
-                          "DLLLDDDD",
-                          "DLDLDLDD",
-                          "DDDLLDDD",
-                          "DDDLDDDD"};
+        string[] board = {" . . L D D D D D",
+                          " D L D L D L D D",
+                          " D D D L L D L D",
+                          " D D L L D L D D",
+                          " D L L L D D D D",
+                          " D L D L D L D D",
+                          " D D D L L D D D",
+                          " D D D L D D D D"};
         Game game = new Game.from_strings (board, Player.LIGHT);
         ComputerPlayer ai = new ComputerPlayer (game);
         ai.move ();
@@ -143,14 +143,14 @@ public class TestIagno : Object
 
     private static void test_ai_search_5 ()
     {
-        string[] board = {"     L  ",
-                          "    LL  ",
-                          "  LLLL  ",
-                          "  LLLL  ",
-                          " DDDLLLL",
-                          " L DLLLL",
-                          " LLLDLLL",
-                          "  LLLLLL"};
+        string[] board = {" . . . . . L . .",
+                          " . . . . L L . .",
+                          " . . L L L L . .",
+                          " . . L L L L . .",
+                          " . D D D L L L L",
+                          " . L . D L L L L",
+                          " . L L L D L L L",
+                          " . . L L L L L L"};
         Game game = new Game.from_strings (board, Player.LIGHT);
         ComputerPlayer ai = new ComputerPlayer (game);
         ai.move ();


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