[gnome-nibbles/arnaudb/command-line: 1/6] Add --players and --nibbles options.



commit 87b042a611f34f802db3fc2b3f94f16c0332329a
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Tue Jul 7 16:17:30 2020 +0200

    Add --players and --nibbles options.

 data/gnome-nibbles.6   | 12 +++++++++++-
 src/gnome-nibbles.vala | 52 +++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 62 insertions(+), 2 deletions(-)
---
diff --git a/data/gnome-nibbles.6 b/data/gnome-nibbles.6
index a54d7b2..fb5e160 100644
--- a/data/gnome-nibbles.6
+++ b/data/gnome-nibbles.6
@@ -26,7 +26,17 @@ board, eating diamonds while avoiding the walls placed around it.
 .P
 The game can be played against the computer or against other players.
 .SH OPTIONS
-This program only accepts the standard GNOME and GTK options.
+.TP
+.B \-n, \-\-nibbles=NUMBER
+Set number of nibbles (4-6)
+.TP
+.B \-p, \-\-players=NUMBER
+Set number of players (1-4)
+.TP
+.B \-v, \-\-version
+Print release version and exit
+.P
+This program also accepts the standard GNOME and GTK options.
 .SH AUTHORS
 .B gnome-nibbles
 was written by Sean MacIsaac, Ian Peters and Andreas R\[/o]sdal.
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 1882581..4cedc5f 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -32,10 +32,24 @@ private class Nibbles : Gtk.Application
         {"quit", quit}
     };
 
+    private static int nibbles = int.MIN;
+    private static int players = int.MIN;
     private const OptionEntry[] option_entries =
     {
         /* Translators: command-line option description, see 'gnome-nibbles --help' */
-        { "version", 'v', OptionFlags.NONE, OptionArg.NONE, null, N_("Show release version"), null },
+        { "nibbles",        'n', OptionFlags.NONE, OptionArg.INT,   ref nibbles,    N_("Set number of 
nibbles (4-6)"),
+
+        /* Translators: in the command-line options description, text to indicate the user should specify 
number of nibbles, see 'gnome-nibbles --help' */
+                                                                                    N_("NUMBER") },
+
+        /* Translators: command-line option description, see 'gnome-nibbles --help' */
+        { "players",        'p', OptionFlags.NONE, OptionArg.INT,   ref players,    N_("Set number of 
players (1-4)"),
+
+        /* Translators: in the command-line options description, text to indicate the user should specify 
number of players, see 'gnome-nibbles --help' */
+                                                                                    N_("NUMBER") },
+
+        /* Translators: command-line option description, see 'gnome-nibbles --help' */
+        { "version",        'v', OptionFlags.NONE, OptionArg.NONE,  null,           N_("Show release 
version"),                 null },
         {}
     };
 
@@ -65,6 +79,19 @@ private class Nibbles : Gtk.Application
             return Posix.EXIT_SUCCESS;
         }
 
+        if (nibbles != int.MIN && (nibbles < 4 || nibbles > 6))
+        {
+            /* Translators: command-line error message, displayed for an invalid number of nibbles; see 
'gnome-nibbles -n 1' */
+            stderr.printf (_("There could only be between 4 and 6 nibbles.") + "\n");
+            return Posix.EXIT_FAILURE;
+        }
+        if (players != int.MIN && (players < 1 || players > 4))
+        {
+            /* Translators: command-line error message, displayed for an invalid number of players; see 
'gnome-nibbles -p 5' */
+            stderr.printf (_("There could only be between 1 and 4 players.") + "\n");
+            return Posix.EXIT_FAILURE;
+        }
+
         /* Activate */
         return -1;
     }
@@ -97,6 +124,29 @@ private class Nibbles : Gtk.Application
         set_accels_for_action ("win.back",      {          "Escape" });
         set_accels_for_action ("win.hamburger", {          "F10",
                                                            "Menu"   });
+        bool nibbles_changed = nibbles != int.MIN;
+        bool players_changed = players != int.MIN;
+        if (nibbles_changed || players_changed)
+        {
+            GLib.Settings settings = new GLib.Settings ("org.gnome.Nibbles");
+            if (nibbles_changed && players_changed)
+            {
+                settings.set_int ("players", players);
+                settings.set_int ("ai", nibbles - players);
+            }
+            else if (players_changed)
+            {
+                int old_ai      = settings.get_int ("ai");
+                int old_players = settings.get_int ("players");
+                settings.set_int ("players", players);
+                int new_ai = ((old_ai + old_players).clamp (4, 6) - players).clamp (0, 5);
+                if (old_ai != new_ai)
+                    settings.set_int ("ai", new_ai);
+            }
+            else // (nibbles_changed)
+                settings.set_int ("ai", nibbles - settings.get_int ("players"));
+        }
+
         window = new NibblesWindow ();
         add_window (window);
     }


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