[iagno] Allow random start position.



commit 324dfffc5de207d868afcd907be0344a5a9f61c6
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Aug 17 23:46:12 2019 +0200

    Allow random start position.

 data/iagno.6                       | 19 +++++++++++--
 data/org.gnome.Reversi.gschema.xml | 45 +++++++++++++++++-------------
 src/iagno.vala                     | 56 +++++++++++++++++++++++++++++++++-----
 3 files changed, 92 insertions(+), 28 deletions(-)
---
diff --git a/data/iagno.6 b/data/iagno.6
index 0907b06..a1c8549 100644
--- a/data/iagno.6
+++ b/data/iagno.6
@@ -30,9 +30,24 @@ your color as possible without your opponent flipping your tiles.
 This is done by trapping your opponent's tiles between two tiles of
 your own color.
 .SH OPTIONS
-.TP
+.PP
 .B \-\-alternative\-start
-Start the game with an alternative position.
+.br
+.B \-\-random\-start
+.br
+.B \-\-usual\-start
+.RS 7
+Reversi can be played with two different initial positions.
+.br
+The options --usual-start and --alternative-start permit
+to directly choose which initial position will be used.
+.br
+The option --random-start is for choosing one randomly.
+.br
+The three options disable setting the mode from the window.
+.br
+You should only use one of these options at the same time.
+.RE
 .TP
 .B \-f, \-\-fast\-moves
 Play with reduced delay before the computer moves.
diff --git a/data/org.gnome.Reversi.gschema.xml b/data/org.gnome.Reversi.gschema.xml
index d8c7b9b..b042537 100644
--- a/data/org.gnome.Reversi.gschema.xml
+++ b/data/org.gnome.Reversi.gschema.xml
@@ -44,6 +44,13 @@
   </enum>
 
   <schema id="org.gnome.Reversi" path="/org/gnome/iagno/" gettext-domain="iagno">
+    <key name="color" enum="org.gnome.Reversi.Color">
+      <default>'dark'</default>
+      <!-- Translators: summary of a settings key, see 'dconf-editor /org/gnome/iagno/color' -->
+      <summary>Color to play as</summary>
+      <!-- Translators: description of a settings key, see 'dconf-editor /org/gnome/iagno/color' -->
+      <description>Whether to play as Dark or Light. Ignored for two-player games.</description>
+    </key>
     <key name="computer-level" type="i">
       <default>1</default>
       <range min="1" max="3" />
@@ -52,6 +59,13 @@
       <!-- Translators: description of a settings key, see 'dconf-editor /org/gnome/iagno/computer-level' -->
       <description>From 1, the easiest, to 3, the hardest.</description>
     </key>
+    <key name="highlight-turnable-tiles" type="b">
+      <default>false</default>
+      <!-- Translators: summary of a settings key, see 'dconf-editor 
/org/gnome/iagno/highlight-turnable-tiles' -->
+      <summary>A flag to highlight turnable tiles</summary>
+      <!-- Translators: description of a settings key, see 'dconf-editor 
/org/gnome/iagno/highlight-turnable-tiles'; a "ply" here means a "play", a "move": the player moves mouse 
cursor and sees what he can capture by playing there -->
+      <description>If “true”, the tiles that will be captured to the opponent by a ply are 
highlighted.</description>
+    </key>
     <key name="num-players" type="i">
       <default>1</default>
       <range min="1" max="2" />
@@ -59,26 +73,12 @@
       <summary>Whether to play against the computer or another human.</summary>
       <!-- TODO add description? -->
     </key>
-    <key name="color" enum="org.gnome.Reversi.Color">
-      <default>'dark'</default>
-      <!-- Translators: summary of a settings key, see 'dconf-editor /org/gnome/iagno/color' -->
-      <summary>Color to play as</summary>
-      <!-- Translators: description of a settings key, see 'dconf-editor /org/gnome/iagno/color' -->
-      <description>Whether to play as Dark or Light. Ignored for two-player games.</description>
-    </key>
-    <key name="highlight-turnable-tiles" type="b">
+    <key name="random-start-position" type="b">
       <default>false</default>
-      <!-- Translators: summary of a settings key, see 'dconf-editor 
/org/gnome/iagno/highlight-turnable-tiles' -->
-      <summary>A flag to highlight turnable tiles</summary>
-      <!-- Translators: description of a settings key, see 'dconf-editor 
/org/gnome/iagno/highlight-turnable-tiles'; a "ply" here means a "play", a "move": the player moves mouse 
cursor and sees what he can capture by playing there -->
-      <description>If “true”, the tiles that will be captured to the opponent by a ply are 
highlighted.</description>
-    </key>
-    <key name="theme" type="s">
-      <default>'default'</default>
-      <!-- Translators: summary of a settings key, see 'dconf-editor /org/gnome/iagno/theme' -->
-      <summary>Theme</summary>
-      <!-- Translators: description of a settings key, see 'dconf-editor /org/gnome/iagno/theme'; TODO the 
themes names are translated in the UI, but not in the settings; not sure what to do for now -->
-      <description>Filename of the theme used, or "default". Are provided "adwaita.theme", 
"high_contrast.theme" and "sun_and_star.theme".</description>
+      <!-- Translators: summary of a settings key, see 'dconf-editor /org/gnome/iagno/random-start-position' 
-->
+      <summary>A flag to randomize start position</summary>
+      <!-- Translators: description of a settings key, see 'dconf-editor 
/org/gnome/iagno/random-start-position' -->
+      <description>Reversi can be played with two different initial positions (and their variants). If 
“true”, the game will randomly start with one or the other position; if “false”, only the most common initial 
position is used. You can force the game to use the alternative position by using the “--alternative-start” 
argument on the command-line; also, if either “--usual-start” or “--random-start” is given instead on the 
command-line, this flag will be ignored.</description>
     </key>
     <key name="sound" type="b">
       <default>true</default>
@@ -87,6 +87,13 @@
       <!-- Translators: description of a settings key, see 'dconf-editor /org/gnome/iagno/sound' -->
       <description>Whether or not to play event sounds.</description>
     </key>
+    <key name="theme" type="s">
+      <default>'default'</default>
+      <!-- Translators: summary of a settings key, see 'dconf-editor /org/gnome/iagno/theme' -->
+      <summary>Theme</summary>
+      <!-- Translators: description of a settings key, see 'dconf-editor /org/gnome/iagno/theme'; TODO the 
themes names are translated in the UI, but not in the settings; not sure what to do for now -->
+      <description>Filename of the theme used, or "default". Are provided "adwaita.theme", 
"high_contrast.theme" and "sun_and_star.theme".</description>
+    </key>
     <key name="type" enum="org.gnome.Reversi.GameType">
       <default>'classic'</default>
       <!-- Translators: summary of a settings key, see 'dconf-editor /org/gnome/iagno/type' -->
diff --git a/src/iagno.vala b/src/iagno.vala
index c7ead0c..c8572ec 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -31,6 +31,8 @@ private class Iagno : Gtk.Application, BaseApplication
     private GLib.Settings settings;
     private static bool fast_mode;
     private static bool alternative_start;
+    private static bool random_start;
+    private static bool usual_start;
     private static string? level = null;
     private static int size = 8;
     private static bool? sound = null;
@@ -74,6 +76,9 @@ private class Iagno : Gtk.Application, BaseApplication
         /* Translators: command-line option description, see 'iagno --help' */
         { "mute", 0, 0, OptionArg.NONE, null,                               N_("Turn off the sound"), null},
 
+        /* Translators: command-line option description, see 'iagno --help' */
+        { "random-start", 0, 0, OptionArg.NONE, ref random_start,           N_("Start with a random 
position"), null},
+
         /* Translators: command-line option description, see 'iagno --help' */
         { "second", 0, 0, OptionArg.NONE, null,                             N_("Play second"), null},
 
@@ -86,6 +91,9 @@ private class Iagno : Gtk.Application, BaseApplication
         /* Translators: command-line option description, see 'iagno --help' */
         { "unmute", 0, 0, OptionArg.NONE, null,                             N_("Turn on the sound"), null},
 
+        /* Translators: command-line option description, see 'iagno --help' */
+        { "usual-start", 0, 0, OptionArg.NONE, ref usual_start,             N_("Start with the usual 
position"), null},
+
         /* Translators: command-line option description, see 'iagno --help' */
         { "version", 'v', 0, OptionArg.NONE, null,                          N_("Print release version and 
exit"), null},
         {}
@@ -129,6 +137,15 @@ private class Iagno : Gtk.Application, BaseApplication
             return Posix.EXIT_SUCCESS;
         }
 
+        if ((usual_start && random_start)
+         || (random_start && alternative_start)
+         || (alternative_start && usual_start))
+        {
+            /* Translators: command-line error message, displayed when two antagonist arguments are used; 
try 'iagno --usual-start --alternative-start' */
+            stderr.printf ("%s\n", _("The “--alternative-start”, “--random-start” and “--usual-start” 
arguments are mutually exclusive."));
+            return Posix.EXIT_FAILURE;
+        }
+
         if (size < 4)
         {
             /* Translators: command-line error message, displayed for an incorrect game size request; try 
'iagno -s 2' */
@@ -212,16 +229,29 @@ private class Iagno : Gtk.Application, BaseApplication
         view.clear_impossible_to_move_here_warning.connect (clear_impossible_to_move_here_warning);
 
         GLib.Menu size_menu = new GLib.Menu ();
+        GLib.Menu section = new GLib.Menu ();
         /* Translators: when configuring a new game, in the first menubutton's menu, label of the entry to 
choose to play first/Dark (with a mnemonic that appears pressing Alt) */
-        size_menu.append (_("Play _first (Dark)"),  "app.game-type('dark')");
+        section.append (_("Play _first (Dark)"),  "app.game-type('dark')");
 
 
         /* Translators: when configuring a new game, in the first menubutton's menu, label of the entry to 
choose to play second/Light (with a mnemonic that appears pressing Alt) */
-        size_menu.append (_("Play _second (Light)"), "app.game-type('light')");
+        section.append (_("Play _second (Light)"), "app.game-type('light')");
 
 
         /* Translators: when configuring a new game, in the first menubutton's menu, label of the entry to 
choose a two-players game (with a mnemonic that appears pressing Alt) */
-        size_menu.append (_("_Two players"), "app.game-type('two')");
+        section.append (_("_Two players"), "app.game-type('two')");
+        section.freeze ();
+        size_menu.append_section (null, section);
+
+        if (!alternative_start && !random_start && !usual_start)
+        {
+            section = new GLib.Menu ();
+            /* Translators: when configuring a new game, in the first menubutton's menu, label of the entry 
to choose to use randomly an alternative start position (with a mnemonic that appears pressing Alt) */
+            section.append (_("_Random start position"), "app.random-start-position");
+            section.freeze ();
+            size_menu.append_section (null, section);
+        }
+
         size_menu.freeze ();
 
         GLib.Menu theme_menu = new GLib.Menu ();
@@ -252,7 +282,7 @@ private class Iagno : Gtk.Application, BaseApplication
             init_sound ();
 
         GLib.Menu appearance_menu = new GLib.Menu ();
-        GLib.Menu section = new GLib.Menu ();
+        section = new GLib.Menu ();
         /* Translators: hamburger menu "Appearance" submenu entry; a name for the default theme */
         section.append (_("Default"), "app.theme('default')");
         Dir dir;
@@ -351,10 +381,12 @@ private class Iagno : Gtk.Application, BaseApplication
         set_accels_for_action ("base.toggle-hamburger", {                 "F10"     });
      // set_accels_for_action ("app.help",              {                 "F1"      });
      // set_accels_for_action ("base.about",            {          "<Shift>F1"      });
-        add_action (settings.create_action ("sound"));
-        add_action (settings.create_action ("type"));        // TODO window action?
         add_action (settings.create_action ("highlight-turnable-tiles"));
+        if (!alternative_start && !random_start && !usual_start)
+            add_action (settings.create_action ("random-start-position"));
+        add_action (settings.create_action ("sound"));
         add_action (settings.create_action ("theme"));
+        add_action (settings.create_action ("type"));        // TODO window action?
 
         settings.bind ("highlight-turnable-tiles", view, "show-turnable-tiles", SettingsBindFlags.GET);
         settings.bind ("theme",                    view, "theme",               SettingsBindFlags.GET);
@@ -539,8 +571,18 @@ private class Iagno : Gtk.Application, BaseApplication
         if (computer != null)
             ((!) computer).cancel_move ();
 
+        bool use_alternative;
+        if (alternative_start)
+            use_alternative = true;
+        else if (usual_start)
+            use_alternative = false;
+        else if (random_start || settings.get_boolean ("random-start-position"))
+            use_alternative = Random.boolean ();
+        else
+            use_alternative = false;
+
         bool reverse = settings.get_string ("type") == "reverse";
-        game = new Game (reverse, alternative_start, (uint8) size /* 4 <= size <= 16 */);
+        game = new Game (reverse, use_alternative, (uint8) size /* 4 <= size <= 16 */);
         game_is_set = true;
         game.turn_ended.connect (turn_ended_cb);
         view.game = game;


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