[gnome-2048] Use uint8 a bit more.



commit d17fe3dc61c60b25b795fb4d3b220c9a0dffadc4
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Mon Oct 28 19:29:23 2019 +0100

    Use uint8 a bit more.

 src/game-headerbar.vala |  4 ++--
 src/game-window.vala    | 10 ++++-----
 src/game.vala           | 54 ++++++++++++++++++++++++-------------------------
 src/grid.vala           | 32 ++++++++++++++---------------
 src/test-tw12ht.vala    | 38 +++++++++++++++++-----------------
 5 files changed, 68 insertions(+), 70 deletions(-)
---
diff --git a/src/game-headerbar.vala b/src/game-headerbar.vala
index 04d30b4..dc3193c 100644
--- a/src/game-headerbar.vala
+++ b/src/game-headerbar.vala
@@ -169,9 +169,9 @@ private class GameHeaderBar : HeaderBar
         menu.freeze ();
         _new_game_button.set_menu_model ((MenuModel) menu);
     }
-    private static void _append_new_game_item (string label, int rows, int cols, ref GLib.Menu menu)
+    private static void _append_new_game_item (string label, uint8 rows, uint8 cols, ref GLib.Menu menu)
     {
-        Variant variant = new Variant ("(ii)", rows, cols);
+        Variant variant = new Variant ("(yy)", rows, cols);
         menu.append (label, "ui.new-game-sized(" + variant.print (/* annotate types */ true) + ")");
     }
 
diff --git a/src/game-window.vala b/src/game-window.vala
index 711215c..4073232 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -240,7 +240,7 @@ private class GameWindow : ApplicationWindow
         { "undo",               undo_cb                     },
 
         { "new-game",           new_game_cb                 },
-        { "new-game-sized",     new_game_sized_cb, "(ii)"   },
+        { "new-game-sized",     new_game_sized_cb, "(yy)"   },
 
         { "toggle-new-game",    toggle_new_game_cb          },
         { "toggle-hamburger",   toggle_hamburger_menu       },
@@ -271,8 +271,8 @@ private class GameWindow : ApplicationWindow
     private void new_game_sized_cb (SimpleAction action, Variant? variant)
         requires (variant != null)
     {
-        int rows, cols;
-        ((!) variant).@get ("(ii)", out rows, out cols);
+        uint8 rows, cols;
+        ((!) variant).@get ("(yy)", out rows, out cols);
         _settings.delay ();
         _settings.set_int ("rows", rows);
         _settings.set_int ("cols", cols);
@@ -460,8 +460,8 @@ private class GameWindow : ApplicationWindow
 
     private inline void _show_best_scores ()
     {
-        int rows = _settings.get_int ("rows");
-        int cols = _settings.get_int ("cols");
+        uint8 rows = (uint8) _settings.get_int ("rows");  // schema ranges rows
+        uint8 cols = (uint8) _settings.get_int ("cols"); // and cols from 1 to 9
         if (rows != cols)
             return;                 // FIXME add categories for non-square grids
         Scores.Category cat;
diff --git a/src/game.vala b/src/game.vala
index 04766bb..0aaec6a 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -68,12 +68,12 @@ private class Game : Object
 
     internal Game (ref GLib.Settings settings)
     {
-        int rows = settings.get_int ("rows");
-        int cols = settings.get_int ("cols");
+        uint8 rows = (uint8) settings.get_int ("rows");  // schema ranges rows
+        uint8 cols = (uint8) settings.get_int ("cols"); // and cols from 1 to 9
         _init_grid (rows, cols, out _grid, ref settings);
     }
 
-    private static void _init_grid (int rows, int cols, out Grid grid, ref GLib.Settings settings)
+    private static void _init_grid (uint8 rows, uint8 cols, out Grid grid, ref GLib.Settings settings)
     {
         grid = new Grid (rows, cols);
         settings.bind ("target-value", grid, "target-value", GLib.SettingsBindFlags.DEFAULT | 
GLib.SettingsBindFlags.NO_SENSITIVITY);
@@ -127,8 +127,8 @@ private class Game : Object
         _grid.clear ();
         _clear_history ();
 
-        int rows = settings.get_int ("rows");
-        int cols = settings.get_int ("cols");
+        uint8 rows = (uint8) settings.get_int ("rows");  // schema ranges rows
+        uint8 cols = (uint8) settings.get_int ("cols"); // and cols from 1 to 9
 
         if ((rows != _grid.rows) || (cols != _grid.cols))
         {
@@ -186,8 +186,8 @@ private class Game : Object
         _init_background ();
         _restore_foreground (true);
 
-        int rows = _grid.rows;
-        int cols = _grid.cols;
+        uint8 rows = _grid.rows;
+        uint8 cols = _grid.cols;
         if ((rows == 3 && cols != 3)
          || (rows == 4 && cols != 4)
          || (rows == 5 && cols != 5)
@@ -219,8 +219,8 @@ private class Game : Object
 
     private void _init_background ()
     {
-        int rows = _grid.rows;
-        int cols = _grid.cols;
+        uint8 rows = _grid.rows;
+        uint8 cols = _grid.cols;
         Clutter.Color background_color = Clutter.Color.from_string ("#babdb6");
         _view.set_background_color (background_color);
 
@@ -237,9 +237,9 @@ private class Game : Object
         float tile_width  = canvas_width  / cols;
         float tile_height = canvas_height / rows;
 
-        for (int i = 0; i < rows; i++)
+        for (uint8 i = 0; i < rows; i++)
         {
-            for (int j = 0; j < cols; j++)
+            for (uint8 j = 0; j < cols; j++)
             {
                 float x = j * tile_width  + (j + 1) * BLANK_COL_WIDTH;
                 float y = i * tile_height + (i + 1) * BLANK_ROW_HEIGHT;
@@ -260,8 +260,8 @@ private class Game : Object
 
     private void _resize_view ()
     {
-        int rows = _grid.rows;
-        int cols = _grid.cols;
+        uint8 rows = _grid.rows;
+        uint8 cols = _grid.cols;
         float canvas_width  = _view.width;
         float canvas_height = _view.height;
 
@@ -271,9 +271,9 @@ private class Game : Object
         float tile_width  = canvas_width  / cols;
         float tile_height = canvas_height / rows;
 
-        for (int i = 0; i < rows; i++)
+        for (uint8 i = 0; i < rows; i++)
         {
-            for (int j = 0; j < cols; j++)
+            for (uint8 j = 0; j < cols; j++)
             {
                 float x = j * tile_width  + (j + 1) * BLANK_COL_WIDTH;
                 float y = i * tile_height + (i + 1) * BLANK_ROW_HEIGHT;
@@ -294,11 +294,11 @@ private class Game : Object
 
     private bool _idle_resize_view ()
     {
-        int rows = _grid.rows;
-        int cols = _grid.cols;
-        for (int i = 0; i < rows; i++)
+        uint8 rows = _grid.rows;
+        uint8 cols = _grid.cols;
+        for (uint8 i = 0; i < rows; i++)
         {
-            for (int j = 0; j < cols; j++)
+            for (uint8 j = 0; j < cols; j++)
             {
                 _background [i, j].idle_resize ();
 
@@ -441,12 +441,12 @@ private class Game : Object
 
     private void _clear_foreground ()
     {
-        int rows = _grid.rows;
-        int cols = _grid.cols;
+        uint8 rows = _grid.rows;
+        uint8 cols = _grid.cols;
         _view_foreground.remove_all_children ();
-        for (int i = 0; i < rows; i++)
+        for (uint8 i = 0; i < rows; i++)
         {
-            for (int j = 0; j < cols; j++)
+            for (uint8 j = 0; j < cols; j++)
             {
                 if (_foreground_cur [i, j] != null)
                     _foreground_cur [i, j] = null;
@@ -458,14 +458,14 @@ private class Game : Object
 
     private void _restore_foreground (bool animate)
     {
-        int rows = _grid.rows;
-        int cols = _grid.cols;
+        uint8 rows = _grid.rows;
+        uint8 cols = _grid.cols;
 
         _create_show_hide_transition (animate);
 
-        for (int i = 0; i < rows; i++)
+        for (uint8 i = 0; i < rows; i++)
         {
-            for (int j = 0; j < cols; j++)
+            for (uint8 j = 0; j < cols; j++)
             {
                 uint8 val = _grid [i, j];
                 if (val != 0)
diff --git a/src/grid.vala b/src/grid.vala
index 8c1220c..0488f34 100644
--- a/src/grid.vala
+++ b/src/grid.vala
@@ -22,8 +22,8 @@ private class Grid : Object
 {
     protected uint8 [,] _grid;
 
-    [CCode (notify = false)] public int rows { internal get; protected construct; }
-    [CCode (notify = false)] public int cols { internal get; protected construct; }
+    [CCode (notify = false)] public uint8 rows { internal get; protected construct; }
+    [CCode (notify = false)] public uint8 cols { internal get; protected construct; }
 
     [CCode (notify = false)] internal uint target_value          { internal get; internal set; default = 0; }
     [CCode (notify = false)] internal bool target_value_reached  { internal get; internal set; default = 
false; }
@@ -37,7 +37,7 @@ private class Grid : Object
         _clear (ref _grid);
     }
 
-    internal Grid (int rows, int cols)
+    internal Grid (uint8 rows, uint8 cols)
     {
         Object (rows: rows, cols: cols);
     }
@@ -59,12 +59,10 @@ private class Grid : Object
         tile = { pos, /* tile value */ 1 };
     }
 
-    private static inline void _generate_random_position (int rows, int cols, out GridPosition pos)
-        requires (rows > 0)
-        requires (cols > 0)
+    private static inline void _generate_random_position (uint8 rows, uint8 cols, out GridPosition pos)
     {
-        pos = { Random.int_range (0, rows),
-                Random.int_range (0, cols) };
+        pos = { Random.int_range (0, (int32) rows),
+                Random.int_range (0, (int32) cols) };
     }
 
     /*\
@@ -391,16 +389,16 @@ private class Grid : Object
         if (!_grid_is_full (ref _grid))
             return false;
 
-        for (int i = 0; i < _rows; i++)
+        for (uint8 i = 0; i < _rows; i++)
         {
-            for (int j = 0; j < _cols; j++)
+            for (uint8 j = 0; j < _cols; j++)
             {
                 uint8 val = _grid [i, j];
 
-                if (i < (_rows - 1) && val == _grid [i+1, j])
+                if (i < (_rows - 1) && val == _grid [i + 1, j])
                     return false;
 
-                if (j < (_cols - 1) && val == _grid [i, j+1])
+                if (j < (_cols - 1) && val == _grid [i, j + 1])
                     return false;
             }
         }
@@ -471,7 +469,7 @@ private class Grid : Object
         return grid;
     }
 
-    internal new uint8 get (int row, int col)    // allows calling "uint val = _grid [i, j];" in game.vala
+    internal new uint8 get (uint8 row, uint8 col)    // allows calling "uint8 val = _grid [i, j];" in 
game.vala
     {
         if ((row >= _rows) || (col >= _cols))
             return 0;
@@ -537,8 +535,8 @@ private class Grid : Object
         if (!_load_from_string (ref content, ref grid))
             return false;
 
-        _rows = grid.length [0];
-        _cols = grid.length [1];
+        _rows = (uint8) grid.length [0];
+        _cols = (uint8) grid.length [1];
         _grid = grid;
         return true;
     }
@@ -578,14 +576,14 @@ private class Grid : Object
 
         grid = new uint8 [rows, cols];
 
-        for (uint i = 0; i < rows; i++)
+        for (uint8 i = 0; i < rows; i++)
         {
             _parse_line (lines [i + 1], out tokens);
             // we do need to be strict here
             if (tokens.length != cols)
                 return false;
 
-            for (uint j = 0; j < cols; j++)
+            for (uint8 j = 0; j < cols; j++)
             {
                 if (!uint64.try_parse (tokens [j], out number_64))
                     return false;
diff --git a/src/test-tw12ht.vala b/src/test-tw12ht.vala
index cd28156..14cf6d8 100644
--- a/src/test-tw12ht.vala
+++ b/src/test-tw12ht.vala
@@ -49,19 +49,19 @@ private class TestTw12ht : Object
         test_full_grid (4, 3);
     }
 
-    private static void test_full_grid (int rows, int cols)
+    private static void test_full_grid (uint8 rows, uint8 cols)
     {
         TestGrid grid = new TestGrid (rows, cols);
         test_new_grid_size (ref grid, rows, cols);
 
-        for (int i = 0; i < rows * cols; i++)
+        for (uint8 i = 0; i < rows * cols; i++)
             test_tile_creation (ref grid);
 
         test_grid_fullness (ref grid);
         test_grid_clearing (ref grid);
     }
 
-    private static void test_new_grid_size (ref TestGrid grid, int rows, int cols)
+    private static void test_new_grid_size (ref TestGrid grid, uint8 rows, uint8 cols)
     {
         assert_true (grid.rows == rows);
         assert_true (grid.cols == cols);
@@ -69,8 +69,8 @@ private class TestTw12ht : Object
 
     private static void test_tile_creation (ref TestGrid grid)
     {
-        int rows = grid.rows;
-        int cols = grid.cols;
+        uint8 rows = grid.rows;
+        uint8 cols = grid.cols;
 
         Tile tile;
         grid.new_tile (out tile);
@@ -84,25 +84,25 @@ private class TestTw12ht : Object
 
     private static void test_grid_fullness (ref TestGrid grid)
     {
-        int rows = grid.rows;
-        int cols = grid.cols;
+        uint8 rows = grid.rows;
+        uint8 cols = grid.cols;
 
         assert_true (grid.grid_is_full ());
 
-        for (int i = 0; i < rows; i++)
-            for (int j = 0; j < cols; j++)
+        for (uint8 i = 0; i < rows; i++)
+            for (uint8 j = 0; j < cols; j++)
                 assert_true (grid [i, j] == 1);
     }
 
     private static void test_grid_clearing (ref TestGrid grid)
     {
-        int rows = grid.rows;
-        int cols = grid.cols;
+        uint8 rows = grid.rows;
+        uint8 cols = grid.cols;
 
         grid.clear ();
 
-        for (int i = 0; i < rows; i++)
-            for (int j = 0; j < cols; j++)
+        for (uint8 i = 0; i < rows; i++)
+            for (uint8 j = 0; j < cols; j++)
                 assert_true (grid [i, j] == 0);
     }
 
@@ -112,7 +112,7 @@ private class TestTw12ht : Object
 
     private static void test_load_game ()
     {
-        int rows, cols;
+        uint8 rows, cols;
         string old_content, new_content;
         bool loaded;
 
@@ -165,22 +165,22 @@ private class TestTw12ht : Object
 
     private static void test_load_grid (ref string  old_content,
                                         out bool    loaded,
-                                        out int     rows,
-                                        out int     cols,
+                                        out uint8   rows,
+                                        out uint8   cols,
                                         out string  new_content)
     {
         Grid grid = new Grid (1, 1);   // TODO transform load into a constructor
         loaded = grid.load (ref old_content);
 
-        rows        = loaded ? grid.rows    : -1;
-        cols        = loaded ? grid.cols    : -1;
+        rows        = loaded ? grid.rows    : uint8.MAX;
+        cols        = loaded ? grid.cols    : uint8.MAX;
         new_content = loaded ? grid.save () : "";
     }
 }
 
 private class TestGrid : Grid
 {
-    internal TestGrid (int rows, int cols)
+    internal TestGrid (uint8 rows, uint8 cols)
     {
         Object (rows: rows, cols: cols);
     }


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