[iagno] Add --size option for testing.



commit a2e40724a1b8d11a54a0edeaf1d7c2cd8e923a75
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Sep 20 17:43:20 2014 +0200

    Add --size option for testing.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=737004

 data/iagno.6             |    4 ++
 src/computer-player.vala |   30 +++++++++++--------
 src/game-view.vala       |   27 ++++++++++--------
 src/game.vala            |   70 +++++++++++++++++++++++----------------------
 src/iagno.vala           |   15 +++++++--
 5 files changed, 83 insertions(+), 63 deletions(-)
---
diff --git a/data/iagno.6 b/data/iagno.6
index cd41062..93928c4 100644
--- a/data/iagno.6
+++ b/data/iagno.6
@@ -64,6 +64,10 @@ Turn off/on the sound, when launched or during a play.
 If the two options are given, the game is muted.
 .RE
 .TP
+.B \-s, \-\-size=<size>
+Changes the size of the board. The size must be even and at least 4.
+This option is for debugging only. The AI may not perform optimally.
+.TP
 .B \-v, \-\-version
 Prints the program version and exits.
 .P
diff --git a/src/computer-player.vala b/src/computer-player.vala
index 172a99f..389c2ca 100644
--- a/src/computer-player.vala
+++ b/src/computer-player.vala
@@ -160,9 +160,9 @@ public class ComputerPlayer : Object
 
         /* Find all possible moves and sort from most new tiles to least new tiles */
         List<PossibleMove?> moves = null;
-        for (var x = 0; x < 8; x++)
+        for (var x = 0; x < g.size; x++)
         {
-            for (var y = 0; y < 8; y++)
+            for (var y = 0; y < g.size; y++)
             {
                 var n_tiles = g.place_tile (x, y);
                 if (n_tiles <= 0)
@@ -255,11 +255,15 @@ public class ComputerPlayer : Object
     private static int eval_heuristic (Game g)
     {
         var count = 0;
-        for (var x = 0; x < 8; x++)
+
+        if (g.size != 8)     // TODO
+            return 0;
+
+        for (var x = 0; x < g.size; x++)
         {
-            for (var y = 0; y < 8; y++)
+            for (var y = 0; y < g.size; y++)
             {
-                var h = heuristic[y * 8 + x];
+                var h = heuristic[y * g.size + x];
                 if (g.get_owner (x, y) != g.current_color)
                     h = -h;
                 count += h;
@@ -272,9 +276,9 @@ public class ComputerPlayer : Object
     private static int around (Game g)
     {
         var count = 0;
-        for (var x = 0; x < 8; x++)
+        for (var x = 0; x < g.size; x++)
         {
-            for (var y = 0; y < 8; y++)
+            for (var y = 0; y < g.size; y++)
             {
                 var a = 0;
                 a -= is_empty (g, x + 1, y);
@@ -299,7 +303,7 @@ public class ComputerPlayer : Object
 
     private static int is_empty (Game g, int x, int y)
     {
-        if (x < 0 || x >= 8 || y < 0 || y >= 8 || g.get_owner (x, y) != Player.NONE)
+        if (x < 0 || x >= g.size || y < 0 || y >= g.size || g.get_owner (x, y) != Player.NONE)
             return 0;
 
         return 1;
@@ -308,17 +312,17 @@ public class ComputerPlayer : Object
     private static void random_select (Game g, out int move_x, out int move_y)
     {
         List<int> moves = null;
-        for (var x = 0; x < 8; x++)
-            for (var y = 0; y < 8; y++)
+        for (var x = 0; x < g.size; x++)
+            for (var y = 0; y < g.size; y++)
                 if (g.can_place (x, y, g.current_color))
-                    moves.append (x * 8 + y);
+                    moves.append (x * g.size + y);
 
         if (moves == null)
             assert_not_reached ();
 
         var i = Random.int_range (0, (int) moves.length ());
         var xy = moves.nth_data (i);
-        move_x = xy / 8;
-        move_y = xy % 8;
+        move_x = xy / g.size;
+        move_y = xy % g.size;
     }
 }
diff --git a/src/game-view.vala b/src/game-view.vala
index 14c85af..0cd6cc4 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -48,11 +48,12 @@ public class GameView : Gtk.DrawingArea
             if (_game != null)
                 SignalHandler.disconnect_by_func (_game, null, this);
             _game = value;
+            pixmaps = new int[game.size,game.size];
             if (_game != null)
             {
                 _game.square_changed.connect (square_changed_cb);
-                for (var x = 0; x < 8; x++)
-                    for (var y = 0; y < 8; y++)
+                for (var x = 0; x < game.size; x++)
+                    for (var y = 0; y < game.size; y++)
                         pixmaps[x, y] = get_pixmap (_game.get_owner (x, y));
             }
             redraw ();
@@ -70,16 +71,15 @@ public class GameView : Gtk.DrawingArea
     {
         set_events (Gdk.EventMask.EXPOSURE_MASK | Gdk.EventMask.BUTTON_PRESS_MASK);
         set_size_request (350, 350);
-        pixmaps = new int[8,8];
     }
 
     private void calculate ()
     {
         var size = int.min (get_allocated_width (), get_allocated_height ());
         /* tile_size includes a grid spacing */
-        tile_size = (size - 2 * GRID_BORDER + GRID_SPACING) / 8;
+        tile_size = (size - 2 * GRID_BORDER + GRID_SPACING) / game.size;
         /* board_size includes its borders */
-        board_size = tile_size * 8 - GRID_SPACING + 2 * GRID_BORDER;
+        board_size = tile_size * game.size - GRID_SPACING + 2 * GRID_BORDER;
     }
 
     public override bool draw (Cairo.Context cr)
@@ -110,7 +110,7 @@ public class GameView : Gtk.DrawingArea
 
         /* draw lines */
         cr.set_line_width (GRID_SPACING);
-        for (var i = 1; i < 8; i++)
+        for (var i = 1; i < game.size; i++)
         {
             cr.move_to (i * tile_size - GRID_SPACING / 2.0, 0);
             cr.rel_line_to (0, board_size - GRID_BORDER);
@@ -122,10 +122,13 @@ public class GameView : Gtk.DrawingArea
 
         /* draw pieces */
         cr.translate (-GRID_SPACING / 2, -GRID_SPACING / 2);
-        for (var x = 0; x < 8; x++)
+        for (var x = 0; x < game.size; x++)
         {
-            for (var y = 0; y < 8; y++)
+            for (var y = 0; y < game.size; y++)
             {
+                if (pixmaps[x, y] == 0)
+                    continue;
+
                 var tile_x = x * tile_size;
                 var tile_y = y * tile_size;
                 var texture_x = (pixmaps[x, y] % 8) * tile_size;
@@ -187,7 +190,7 @@ public class GameView : Gtk.DrawingArea
         /* Show the result by laying the tiles with winning color first */
         if (flip_final_result_now && game.is_complete ())
         {
-            var n = y * game.width + x;
+            var n = y * game.size + x;
             var winning_color = Player.LIGHT;
             var losing_color = Player.DARK;
             var n_winning_tiles = game.n_light_tiles;
@@ -254,9 +257,9 @@ public class GameView : Gtk.DrawingArea
     {
         var animating = false;
 
-        for (var x = 0; x < 8; x++)
+        for (var x = 0; x < game.size; x++)
         {
-            for (var y = 0; y < 8; y++)
+            for (var y = 0; y < game.size; y++)
             {
                 var old = pixmaps[x, y];
                 square_changed_cb (x, y);
@@ -294,7 +297,7 @@ public class GameView : Gtk.DrawingArea
         {
             var x = (int) (event.x - x_offset) / tile_size;
             var y = (int) (event.y - y_offset) / tile_size;
-            if (x >= 0 && x < 8 && y >= 0 && y < 8)
+            if (x >= 0 && x < game.size && y >= 0 && y < game.size)
                 move (x, y);
         }
 
diff --git a/src/game.vala b/src/game.vala
index 3c343a3..db2167c 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -14,14 +14,11 @@ public class Game : Object
     /* Tiles on the board */
     private Player[,] tiles;
 
-    public int width
+    private int _size;
+    public int size
     {
-        get { return tiles.length[0]; }
-    }
-
-    public int height
-    {
-        get { return tiles.length[1]; }
+        get { return _size; }
+        private set { _size = value; }
     }
 
     /* undoing */
@@ -85,33 +82,39 @@ public class Game : Object
     * * Creation / exporting
     \*/
 
-    public Game (int width = 8, int height = 8)
+    public Game (int tmp_size = 8)
+        requires (tmp_size >= 4)
+        requires (tmp_size % 2 == 0)
     {
-        tiles = new Player[width, height];
-        for (var x = 0; x < width; x++)
-            for (var y = 0; y < height; y++)
+        size = tmp_size;
+        tiles = new Player[size, size];
+        for (var x = 0; x < size; x++)
+            for (var y = 0; y < size; y++)
                 tiles[x, y] = Player.NONE;
 
-        /* Black plays first */
+        /* Dark plays first */
         current_color = Player.DARK;
 
         /* Setup board with four tiles by default */
-        set_tile (3, 3, Player.LIGHT, false);
-        set_tile (4, 4, Player.LIGHT, false);
-        set_tile (3, 4, Player.DARK, false);
-        set_tile (4, 3, Player.DARK, false);
+        set_tile (size / 2 - 1, size / 2 - 1, Player.LIGHT, false);
+        set_tile (size / 2 - 1, size / 2, Player.DARK, false);
+        set_tile (size / 2, size / 2 - 1, Player.DARK, false);
+        set_tile (size / 2, size / 2, Player.LIGHT, false);
         n_current_tiles = 2;
         n_opponent_tiles = 2;
     }
 
-    public Game.from_strings (string[] setup, Player to_move, int width = 8, int height = 8)
-        requires (setup.length == height)
-        requires (setup[0].length == width)
+    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)
     {
-        tiles = new Player[width, height];
+        size = tmp_size;
+        tiles = new Player[size, size];
 
-        for (int y = 0; y < height; y++)
-            for (int x = 0; x < width; x++)
+        for (int y = 0; y < size; y++)
+            for (int x = 0; x < size; x++)
                 tiles[x, y] = Player.from_char (setup[y][x]);
 
         current_color = to_move;
@@ -121,9 +124,10 @@ public class Game : Object
 
     public Game.copy (Game game)
     {
-        tiles = new Player[game.width, game.height];
-        for (var x = 0; x < width; x++)
-            for (var y = 0; y < height; y++)
+        size = game.size;
+        tiles = new Player[size, size];
+        for (var x = 0; x < size; x++)
+            for (var y = 0; y < size; y++)
                 tiles[x, y] = game.tiles[x, y];
         number_of_moves = game.number_of_moves;
         current_color = game.current_color;
@@ -136,9 +140,9 @@ public class Game : Object
     {
         string s = "\n";
 
-        for (int y = 0; y < height; y++)
+        for (int y = 0; y < size; y++)
         {
-            for (int x = 0; x < width; x++)
+            for (int x = 0; x < size; x++)
                 s += tiles[x, y].to_string ();
             s += "\n";
         }
@@ -157,15 +161,15 @@ public class Game : Object
     }
 
     public bool is_complete ()
-        ensures (result || n_tiles < width * height)
+        ensures (result || n_tiles < size * size)
     {
         return !can_move (Player.LIGHT) && !can_move (Player.DARK);
     }
 
     public bool can_move (Player color)
     {
-        for (var x = 0; x < width; x++)
-            for (var y = 0; y < height; y++)
+        for (var x = 0; x < size; x++)
+            for (var y = 0; y < size; y++)
                 if (can_place (x, y, color))
                     return true;
         return false;
@@ -211,7 +215,7 @@ public class Game : Object
 
     private bool is_valid_location (int x, int y)
     {
-        return x >= 0 && x < width && y >= 0 && y < height;
+        return x >= 0 && x < size && y >= 0 && y < size;
     }
 
     private int place (int x, int y, Player color, bool apply)
@@ -238,9 +242,7 @@ public class Game : Object
 
     private int flip_tiles (int x, int y, int x_step, int y_step, Player color, bool apply)
     {
-        var enemy = Player.LIGHT;
-        if (color == Player.LIGHT)
-            enemy = Player.DARK;
+        var enemy = Player.flip_color (color);
 
         /* Count number of enemy pieces we are beside */
         var enemy_count = 0;
diff --git a/src/iagno.vala b/src/iagno.vala
index b7adc94..a02652b 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -17,6 +17,7 @@ public class Iagno : Gtk.Application
     private bool is_maximized;
     private static bool fast_mode;
     private static int computer_level = 0;
+    private static int size = 8;
 
     /* Seconds */
     private static const double QUICK_MOVE_DELAY = 0.4;
@@ -51,6 +52,7 @@ public class Iagno : Gtk.Application
         { "level", 'l', 0, OptionArg.INT, ref computer_level, N_("Set the level of the computer's AI"), 
null},
         { "mute", 0, 0, OptionArg.NONE, null, N_("Turn off the sound"), null},
         { "second", 0, 0, OptionArg.NONE, null, N_("Play second"), null},
+        { "size", 's', 0, OptionArg.INT, ref size, N_("Size of the board (debug only)"), null},
         { "two-players", 0, 0, OptionArg.NONE, null, N_("Two-players mode"), null},
         { "unmute", 0, 0, OptionArg.NONE, null, N_("Turn on the sound"), null},
         { "version", 'v', 0, OptionArg.NONE, null, N_("Print release version and exit"), null},
@@ -97,6 +99,13 @@ public class Iagno : Gtk.Application
             return Posix.EXIT_SUCCESS;
         }
 
+        if (size < 4 || size % 2 != 0)
+        {
+            /* Console message displayed for an incorrect size */
+            stderr.printf ("%s\n", _("Size must be even and at least 4."));
+            return Posix.EXIT_FAILURE;
+        }
+
         /* WARNING: Don't forget that changing at this moment settings
         could interfere badly with a running instance of the game. */
         settings = new Settings ("org.gnome.iagno");
@@ -113,7 +122,7 @@ public class Iagno : Gtk.Application
             if (computer_level <= 3)
                 settings.set_int ("computer-level", computer_level);
             else
-                stderr.printf ("%1$s\n", _("Level should be between 1 (easy) and 3 (hard). Settings 
unchanged."));
+                stderr.printf ("%s\n", _("Level should be between 1 (easy) and 3 (hard). Settings 
unchanged."));
         }
 
         /* The game mode is set for the next game. */
@@ -143,9 +152,7 @@ public class Iagno : Gtk.Application
         if (settings.get_boolean ("window-is-maximized"))
             window.maximize ();
         add_window (window);
-
         view = new GameView ();
-        view.game = game;
         view.move.connect (player_move_cb);
         var tile_set = settings.get_string ("tileset");
         view.theme = Path.build_filename (DATA_DIRECTORY, "themes", tile_set);
@@ -217,7 +224,7 @@ public class Iagno : Gtk.Application
         if (computer != null)
             computer.cancel_move ();
 
-        game = new Game ();
+        game = new Game (size);
         game.move.connect (game_move_cb);
         game.complete.connect (game_complete_cb);
         view.game = game;


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