[gnome-tetravex] Allow custom number of colors.



commit d41e1cebc65c3eb217e67074384f1d1f1d23561a
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Oct 12 05:29:03 2019 +0200

    Allow custom number of colors.

 src/gnome-tetravex.vala | 74 ++++++++++++++++++++++++++-----------------------
 src/puzzle.vala         | 15 +++++-----
 2 files changed, 48 insertions(+), 41 deletions(-)
---
diff --git a/src/gnome-tetravex.vala b/src/gnome-tetravex.vala
index 2a6dc7a..d055173 100644
--- a/src/gnome-tetravex.vala
+++ b/src/gnome-tetravex.vala
@@ -19,7 +19,8 @@ private class Tetravex : Gtk.Application
     private const string KEY_GRID_SIZE = "grid-size";
 
     private static bool start_paused = false;
-    private static int game_size = 0;
+    private static int game_size = int.MIN;
+    private static int colors = 10;
 
     private GLib.Settings settings;
 
@@ -52,13 +53,16 @@ private class Tetravex : Gtk.Application
     private const OptionEntry [] option_entries =
     {
         /* Translators: command-line option description, see 'gnome-tetravex --help' */
-        { "paused",  'p', 0, OptionArg.NONE, null, N_("Start the game paused"),          null },
+        { "colors",  'c', 0, OptionArg.INT,  ref colors,    N_("Set number of colors (2-10)"),    
N_("NUMBER") },
 
         /* Translators: command-line option description, see 'gnome-tetravex --help' */
-        { "size",    's', 0, OptionArg.INT,  null, N_("Set size of board (2-6)"),        null },
+        { "paused",  'p', 0, OptionArg.NONE, null,          N_("Start the game paused"),          null },
 
         /* Translators: command-line option description, see 'gnome-tetravex --help' */
-        { "version", 'v', 0, OptionArg.NONE, null, N_("Print release version and exit"), null },
+        { "size",    's', 0, OptionArg.INT,  ref game_size, N_("Set size of board (2-6)"),        N_("SIZE") 
},
+
+        /* Translators: command-line option description, see 'gnome-tetravex --help' */
+        { "version", 'v', 0, OptionArg.NONE, null,          N_("Print release version and exit"), null },
         {}
     };
 
@@ -99,6 +103,36 @@ private class Tetravex : Gtk.Application
         add_main_option_entries (option_entries);
     }
 
+    protected override int handle_local_options (GLib.VariantDict options)
+    {
+        if (options.contains ("version"))
+        {
+            /* NOTE: Is not translated so can be easily parsed */
+            stderr.printf ("%1$s %2$s\n", "gnome-tetravex", VERSION);
+            return Posix.EXIT_SUCCESS;
+        }
+
+        if (options.contains ("paused"))
+            start_paused = true;
+
+        if (game_size != int.MIN && (game_size < 2 || game_size > 6))
+        {
+            /* Translators: command-line error message, displayed on invalid game size request; see 
'gnome-tetravex -s 1' */
+            stderr.printf (N_("Size could only be from 2 to 6.\n"));
+            return Posix.EXIT_FAILURE;
+        }
+
+        if (colors < 2 || colors > 10)
+        {
+            /* Translators: command-line error message, displayed for an invalid number of colors; see 
'gnome-tetravex -c 1' */
+            stderr.printf (N_("There could only be between 2 and 10 colors.\n"));
+            return Posix.EXIT_FAILURE;
+        }
+
+        /* Activate */
+        return -1;
+    }
+
     protected override void startup ()
     {
         base.startup ();
@@ -144,7 +178,7 @@ private class Tetravex : Gtk.Application
         if (settings.get_boolean ("window-is-maximized"))
             window.maximize ();
 
-        if (game_size != 0)
+        if (game_size != int.MIN)
             settings.set_int (KEY_GRID_SIZE, game_size);
         else
             game_size = settings.get_int (KEY_GRID_SIZE);
@@ -347,33 +381,6 @@ private class Tetravex : Gtk.Application
         settings.apply ();
     }
 
-    protected override int handle_local_options (GLib.VariantDict options)
-    {
-        if (options.contains ("version"))
-        {
-            /* NOTE: Is not translated so can be easily parsed */
-            stderr.printf ("%1$s %2$s\n", "gnome-tetravex", VERSION);
-            return Posix.EXIT_SUCCESS;
-        }
-
-        if (options.contains ("paused"))
-            start_paused = true;
-
-        if (options.contains ("size"))
-        {
-            game_size = (int) options.lookup_value ("size", VariantType.INT32);
-            if ((game_size < 2) || (game_size > 6))
-            {
-                /* Translators: command-line error message, displayed on invalid game size request; see 
'gnome-tetravex -s 1' */
-                stderr.printf (N_("Size could only be from 2 to 6.\n"));
-                return Posix.EXIT_FAILURE;
-            }
-        }
-
-        /* Activate */
-        return -1;
-    }
-
     protected override void activate ()
     {
         window.present ();
@@ -393,7 +400,7 @@ private class Tetravex : Gtk.Application
             SignalHandler.disconnect_by_func (puzzle, null, this);
 
         int size = settings.get_int (KEY_GRID_SIZE);
-        puzzle = new Puzzle ((uint8) size);
+        puzzle = new Puzzle ((uint8) size, (uint8) colors);
         puzzle_init_done = true;
         puzzle.tick.connect (tick_cb);
         puzzle.solved.connect (solved_cb);
@@ -614,7 +621,6 @@ private class Tetravex : Gtk.Application
             hamburger_button.set_active (false);
         }
         settings.set_int (KEY_GRID_SIZE, size);
-        game_size = (int) size;
         action.set_state (variant);
         new_game ();
     }
diff --git a/src/puzzle.vala b/src/puzzle.vala
index da9318d..b3987c1 100644
--- a/src/puzzle.vala
+++ b/src/puzzle.vala
@@ -29,7 +29,8 @@ private class Tile : Object
 
 private class Puzzle : Object
 {
-    [CCode (notify = false)] public uint8 size { internal get; protected construct; }
+    [CCode (notify = false)] public uint8 size   { internal get; protected construct; }
+    [CCode (notify = false)] public uint8 colors { internal get; protected construct; }
     private Tile? [,] board;
 
     /* Game timer */
@@ -89,19 +90,19 @@ private class Puzzle : Object
         return true;
     }
 
-    internal Puzzle (uint8 size)
+    internal Puzzle (uint8 size, uint8 colors)
     {
-        Object (size: size);
+        Object (size: size, colors: colors);
     }
 
     construct
     {
-        do { init_board (size, out board); }
+        do { init_board (size, (int32) colors, out board); }
         while (solved_on_right ());
 
         start_clock ();
     }
-    private static inline void init_board (uint8 size, out Tile? [,] board)
+    private static inline void init_board (uint8 size, int32 colors, out Tile? [,] board)
     {
         board = new Tile? [size * 2, size];
         for (uint8 x = 0; x < size; x++)
@@ -113,7 +114,7 @@ private class Puzzle : Object
         {
             for (uint8 y = 0; y <= size; y++)
             {
-                uint8 n = (uint8) Random.int_range (0, 10);
+                uint8 n = (uint8) Random.int_range (0, colors);
                 if (y >= 1)
                     ((!) board [x, y - 1]).south = n;
                 if (y < size)
@@ -124,7 +125,7 @@ private class Puzzle : Object
         {
             for (uint8 y = 0; y < size; y++)
             {
-                uint8 n = (uint8) Random.int_range (0, 10);
+                uint8 n = (uint8) Random.int_range (0, colors);
                 if (x >= 1)
                     ((!) board [x - 1, y]).east = n;
                 if (x < size)


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