[gnome-chess] Add option to alternate between black and white



commit ba128dd084484a4b1c2cae859b5407aa73be35b9
Author: Johan Manuel <johan manuel live fr>
Date:   Thu Apr 9 19:27:00 2015 +0200

    Add option to alternate between black and white
    
    Add the option 'Alternate' in 'Play as' in the preferences menu.
    When selected, the player will play as the opposite color he played the
    last time when starting a new game.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=747411

 data/org.gnome.chess.gschema.xml |   19 +++++++++++++++----
 data/preferences.ui              |   12 ++++++++----
 src/gnome-chess.vala             |   27 +++++++++++++++++++++------
 3 files changed, 44 insertions(+), 14 deletions(-)
---
diff --git a/data/org.gnome.chess.gschema.xml b/data/org.gnome.chess.gschema.xml
index 6a246e6..02cf335 100644
--- a/data/org.gnome.chess.gschema.xml
+++ b/data/org.gnome.chess.gschema.xml
@@ -7,6 +7,12 @@
     <value value="3" nick="lan"/>
   </enum>
 
+  <enum id="org.gnome.chess.PlayAs">
+    <value value="0" nick="white"/>
+    <value value="1" nick="black"/>
+    <value value="2" nick="alternate"/>
+  </enum>
+
   <enum id="org.gnome.chess.BoardSide">
     <value value="0" nick="white"/>
     <value value="1" nick="black"/>
@@ -92,10 +98,15 @@
       <summary>The timer increment set corresponding to clock type (1 second minimum)</summary>
       <description>The timer increment set corresponding to clock type (1 second minimum)</description>
     </key>
-    <key name="play-as-white" type="b">
-      <default>true</default>
-      <summary>true if the human player is playing white</summary>
-      <description>true if the human player is playing white</description>
+    <key name="play-as" enum="org.gnome.chess.PlayAs">
+      <default>'white'</default>
+      <summary>The board side to play as</summary>
+      <description>The board side to play as</description>
+    </key>
+    <key name="last-played-as" enum="org.gnome.chess.PlayAs">
+      <default>'white'</default>
+      <summary>The last side the player played as</summary>
+      <description>This is needed when play-as is set to alternate. This should only be set to black or 
white.</description>
     </key>
     <key name="opponent" type="s">
       <default>''</default>
diff --git a/data/preferences.ui b/data/preferences.ui
index 41d194b..0154009 100644
--- a/data/preferences.ui
+++ b/data/preferences.ui
@@ -793,17 +793,21 @@
     <columns>
       <!-- column-name label -->
       <column type="gchararray"/>
-      <!-- column-name play-as-white -->
-      <column type="gboolean"/>
+      <!-- column-name play-as -->
+      <column type="gint"/>
     </columns>
     <data>
       <row>
         <col id="0" translatable="yes" context="chess-player" comments="Preferences Dialog: Combo box entry 
for playing as white">White</col>
-        <col id="1">True</col>
+        <col id="1">0</col>
       </row>
       <row>
         <col id="0" translatable="yes" context="chess-player" comments="Preferences Dialog: Combo box entry 
for playing as black">Black</col>
-        <col id="1">False</col>
+        <col id="1">1</col>
+      </row>
+      <row>
+        <col id="0" translatable="yes" context="chess-player" comments="Preferences Dialog: Combo box entry 
for alterning between black and white">Alternate</col>
+        <col id="1">2</col>
       </row>
     </data>
   </object>
diff --git a/src/gnome-chess.vala b/src/gnome-chess.vala
index 73bd58f..9cf21ad 100644
--- a/src/gnome-chess.vala
+++ b/src/gnome-chess.vala
@@ -1655,7 +1655,7 @@ public class ChessApplication : Gtk.Application
                        "active", SettingsBindFlags.DEFAULT);
 
         side_combo = (Gtk.ComboBox) preferences_builder.get_object ("side_combo");
-        side_combo.set_active (settings.get_boolean ("play-as-white") ? 0 : 1);
+        side_combo.set_active (settings.get_enum ("play-as"));
 
         var ai_combo = (Gtk.ComboBox) preferences_builder.get_object ("opponent_combo");
         var ai_model = (Gtk.ListStore) ai_combo.model;
@@ -1755,9 +1755,10 @@ public class ChessApplication : Gtk.Application
         Gtk.TreeIter iter;
         if (!combo.get_active_iter (out iter))
             return;
-        bool play_as_white;
-        combo.model.get (iter, 1, out play_as_white, -1);
-        settings.set_boolean ("play-as-white", play_as_white);
+        int player;
+        combo.model.get (iter, 1, out player, -1);
+
+        settings.set_enum ("play-as", player);
     }
 
     [CCode (cname = "G_MODULE_EXPORT opponent_combo_changed_cb", instance_pos = -1)]
@@ -2379,16 +2380,30 @@ public class ChessApplication : Gtk.Application
         var engine_level = settings.get_string ("difficulty");
         if (engine_name != null && engine_name != "human")
         {
-            if (settings.get_boolean ("play-as-white"))
+            var play_as = settings.get_string ("play-as");
+
+            if (play_as == "alternate")
+            {
+                var last_side = settings.get_string ("last-played-as");
+                play_as = (last_side == "white" ? "black" : "white");
+            }
+
+            if (play_as == "white")
             {
                 pgn_game.black_ai = engine_name;
                 pgn_game.black_level = engine_level;
             }
-            else
+            else if (play_as == "black")
             {
                 pgn_game.white_ai = engine_name;
                 pgn_game.white_level = engine_level;
             }
+            else
+            {
+                assert_not_reached ();
+            }
+
+            settings.set_string ("last-played-as", play_as);
         }
 
         start_game ();


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