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
This commit is contained in:
Johan Manuel 2015-04-09 19:27:00 +02:00 committed by Sahil Sareen
parent dd80c84af0
commit ba128dd084
3 changed files with 44 additions and 14 deletions

View file

@ -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>

View file

@ -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>

View file

@ -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 ();