[gnome-games] iagno: Add back 'Flip final results' option



commit d0b353f756dde9fac496439fb3c166d0eea6f858
Author: Robert Ancell <robert ancell canonical com>
Date:   Mon Feb 6 17:25:30 2012 +1100

    iagno: Add back 'Flip final results' option

 iagno/data/org.gnome.iagno.gschema.xml.in |    3 --
 iagno/src/game-view.vala                  |   54 ++++++++++++++++++++++++++---
 iagno/src/game.vala                       |   44 ++++++++++++++---------
 iagno/src/iagno.vala                      |   16 +++++++-
 4 files changed, 90 insertions(+), 27 deletions(-)
---
diff --git a/iagno/data/org.gnome.iagno.gschema.xml.in b/iagno/data/org.gnome.iagno.gschema.xml.in
index 13a674b..d48cea8 100644
--- a/iagno/data/org.gnome.iagno.gschema.xml.in
+++ b/iagno/data/org.gnome.iagno.gschema.xml.in
@@ -8,9 +8,6 @@
       <default>1</default>
       <range min="0" max="3" />
     </key>
-    <key name="quick-moves" type="b">
-      <default>false</default>
-    </key>
     <key name="tileset" type="s">
       <default>'classic.png'</default>
     </key>
diff --git a/iagno/src/game-view.vala b/iagno/src/game-view.vala
index 7466bdb..6aefbb8 100644
--- a/iagno/src/game-view.vala
+++ b/iagno/src/game-view.vala
@@ -84,6 +84,21 @@ public class GameView : Gtk.DrawingArea
         set { _show_grid = value; redraw (); }
     }
 
+    private bool _flip_final_result;
+    public bool flip_final_result
+    {
+        get { return _flip_final_result; }
+        set
+        {
+            _flip_final_result = value;
+            if (game == null)
+                return;
+            for (var x = 0; x < game.width; x++)
+                for (var y = 0; y < game.height; y++)
+                    square_changed_cb (x, y);
+        }
+    }
+
     public override void get_preferred_width (out int minimum, out int natural)
     {
         minimum = natural = (int) (8 * (20 + GRID_WIDTH));
@@ -165,16 +180,45 @@ public class GameView : Gtk.DrawingArea
 
     private void square_changed_cb (int x, int y)
     {
-        var target = get_pixmap (game.get_owner (x, y));
+        var pixmap = get_pixmap (game.get_owner (x, y));
 
-        if (pixmaps[x, y] == target)
+        /* If requested show the result by laying the tiles with winning color first */
+        if (game.is_complete && flip_final_result)
+        {
+            var n = y * game.width + x;
+            var winning_color = Player.LIGHT;
+            var losing_color = Player.DARK;
+            var n_winning_tiles = game.n_light_tiles;
+            var n_losing_tiles = game.n_dark_tiles;
+            if (n_losing_tiles > n_winning_tiles)
+            {
+                winning_color = Player.DARK;
+                winning_color = Player.LIGHT;
+                var t = n_winning_tiles;
+                n_winning_tiles = n_losing_tiles;
+                n_losing_tiles = t;
+            }
+            if (n < n_winning_tiles)
+                pixmap = get_pixmap (winning_color);
+            else if (n < n_winning_tiles + n_losing_tiles)
+                pixmap = get_pixmap (losing_color);
+            else
+                pixmap = get_pixmap (Player.NONE);
+        }
+
+        set_square (x, y, pixmap);
+    }
+    
+    private void set_square (int x, int y, int pixmap)
+    {
+        if (pixmaps[x, y] == pixmap)
             return;
 
-        if (target == 0 || pixmaps[x, y] == 0)
-            pixmaps[x, y] = target;
+        if (pixmap == 0 || pixmaps[x, y] == 0)
+            pixmaps[x, y] = pixmap;
         else
         {
-            if (target > pixmaps[x, y])
+            if (pixmap > pixmaps[x, y])
                 pixmaps[x, y]++;
             else
                 pixmaps[x, y]--;
diff --git a/iagno/src/game.vala b/iagno/src/game.vala
index b38a6e1..f0eff3c 100644
--- a/iagno/src/game.vala
+++ b/iagno/src/game.vala
@@ -9,6 +9,16 @@ public class Game
 {
     /* Tiles on the board */
     public Player[,] tiles;
+    
+    public int width
+    {
+        get { return tiles.length[0]; }
+    }
+
+    public int height
+    {
+        get { return tiles.length[1]; }
+    }
 
     /* Undo stack.  This is a record of all the tile changes since the start of the game
      * in the binary form ccxxxyyy where cc is the color (0-2), xxx is the x location (0-7)
@@ -36,9 +46,9 @@ public class Game
         get
         {
             var count = 0;
-            for (var x = 0; x < 8; x++)
+            for (var x = 0; x < width; x++)
             {
-                for (var y = 0; y < 8; y++)
+                for (var y = 0; y < height; y++)
                 {
                     if (tiles[x, y] != Player.NONE)
                         count++;
@@ -62,8 +72,8 @@ public class Game
     {
         get
         {
-            for (var x = 0; x < 8; x++)
-                for (var y = 0; y < 8; y++)
+            for (var x = 0; x < width; x++)
+                for (var y = 0; y < height; y++)
                     if (can_place (x, y))
                         return true;
             return false;
@@ -72,15 +82,15 @@ public class Game
 
     public bool is_complete
     {
-        get { return n_tiles == 64 || n_light_tiles == 0 || n_dark_tiles == 0; }
+        get { return n_tiles == width * height || n_light_tiles == 0 || n_dark_tiles == 0; }
     }
 
-    public Game ()
+    public Game (int width = 8, int height = 8)
     {
         /* Setup board with four tiles by default */
-        tiles = new Player[8, 8];
-        for (var x = 0; x < 8; x++)
-            for (var y = 0; y < 8; y++)
+        tiles = new Player[width, height];
+        for (var x = 0; x < width; x++)
+            for (var y = 0; y < height; y++)
                 tiles[x, y] = Player.NONE;
         set_tile (3, 3, Player.LIGHT, false);
         set_tile (3, 4, Player.DARK, false);
@@ -101,9 +111,9 @@ public class Game
 
     public Game.copy (Game game)
     {
-        tiles = new Player[8, 8];
-        for (var x = 0; x < 8; x++)
-            for (var y = 0; y < 8; y++)
+        tiles = new Player[width, height];
+        for (var x = 0; x < width; x++)
+            for (var y = 0; y < height; y++)
                 tiles[x, y] = game.tiles[x, y];
         for (var i = 0; i < game.undo_index; i++)
             undo_history[i] = game.undo_history[i];
@@ -186,8 +196,8 @@ public class Game
     private int count_tiles (Player color)
     {
         var count = 0;
-        for (var x = 0; x < 8; x++)
-            for (var y = 0; y < 8; y++)
+        for (var x = 0; x < width; x++)
+            for (var y = 0; y < height; y++)
                 if (tiles[x, y] == color)
                     count++;
         return count;
@@ -195,7 +205,7 @@ public class Game
 
     private bool is_valid_location (int x, int y)
     {
-        return x >= 0 && x < 8 && y >= 0 && y < 8;
+        return x >= 0 && x < width && y >= 0 && y < height;
     }
 
     private int flip_tiles (int x, int y, int x_step, int y_step, Player color, bool apply)
@@ -251,7 +261,7 @@ public class Game
                 undo_index--;
                 var c = (Player) (n >> 6);
                 var xy = n & 0x3F;
-                set_tile (xy / 8, xy % 8, c, false);
+                set_tile (xy % width, xy / width, c, false);
             }
 
             /* Previous player to move again */
@@ -272,7 +282,7 @@ public class Game
         /* Store the old color in the history */
         if (update_history)
         {
-            undo_history[undo_index] = ((int) tiles[x, y] << 6) | (x * 8 + y);
+            undo_history[undo_index] = ((int) tiles[x, y] << 6) | (y * width + x);
             undo_index++;
         }
 
diff --git a/iagno/src/iagno.vala b/iagno/src/iagno.vala
index c5bcfd8..d8c7a21 100644
--- a/iagno/src/iagno.vala
+++ b/iagno/src/iagno.vala
@@ -139,6 +139,7 @@ public class Iagno : Gtk3.Application
         view.game = game;
         view.move.connect (player_move_cb);
         view.show_grid = settings.get_boolean ("show-grid");
+        view.flip_final_result = settings.get_boolean ("flip-final-results");
         var tile_set = settings.get_string ("tileset");
         var theme = load_theme_texture (tile_set);
         if (theme == null)
@@ -462,12 +463,18 @@ public class Iagno : Gtk3.Application
         settings.set_boolean ("sound", play_sounds);
     }
 
-    private void grid_select (Gtk.ToggleButton widget)
+    private void grid_toggled_cb (Gtk.ToggleButton widget)
     {
         view.show_grid = widget.get_active ();
         settings.set_boolean ("show-grid", view.show_grid);
     }
 
+    private void flip_final_toggled_cb (Gtk.ToggleButton widget)
+    {
+        view.flip_final_result = widget.get_active ();
+        settings.set_boolean ("flip-final-results", view.flip_final_result);
+    }
+
     private void propbox_response_cb (Gtk.Widget widget, int response_id)
     {
         widget.hide ();
@@ -605,9 +612,14 @@ public class Iagno : Gtk3.Application
 
         var grid_button = new Gtk.CheckButton.with_mnemonic (_("S_how grid"));
         grid_button.set_active (settings.get_boolean ("show-grid"));
-        grid_button.toggled.connect (grid_select);
+        grid_button.toggled.connect (grid_toggled_cb);
         vbox.pack_start (grid_button, false, false, 0);
 
+        var flip_final_button = new Gtk.CheckButton.with_mnemonic (_("_Flip final results"));
+        flip_final_button.set_active (settings.get_boolean ("flip-final-results"));
+        flip_final_button.toggled.connect (flip_final_toggled_cb);
+        vbox.pack_start (flip_final_button, false, false, 0);
+
         var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 12);
         vbox.pack_start (hbox, false, false, 0);
 



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