[gnome-taquin] Use int8.



commit a980a2efe7e0bb53ff28856caa502f3699f12835
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Mon Feb 18 14:12:03 2019 +0100

    Use int8.

 src/new-game-screen.vala |  1 +
 src/taquin-game.vala     | 98 +++++++++++++++++++++++++-----------------------
 src/taquin-main.vala     | 14 +++----
 src/taquin-view.vala     | 22 +++++------
 src/test-taquin.vala     |  6 ++-
 5 files changed, 75 insertions(+), 66 deletions(-)
---
diff --git a/src/new-game-screen.vala b/src/new-game-screen.vala
index 546285d..e3f44f3 100644
--- a/src/new-game-screen.vala
+++ b/src/new-game-screen.vala
@@ -34,6 +34,7 @@ private class NewGameScreen : Box, AdaptativeWidget
     {
         /* Translators: when configuring a new game, button label for the size of the game ("3 × 3", or 4, 
or 5) */
         size_button.set_label (_("Size: %d × %d ▾").printf (size, size));
+     // size_button.set_label (_("Size: %hhu × %hhu ▾").printf (size, size));   // TODO uint8
     }
 
     internal void update_theme (string theme)
diff --git a/src/taquin-game.vala b/src/taquin-game.vala
index b571c3d..df12d63 100644
--- a/src/taquin-game.vala
+++ b/src/taquin-game.vala
@@ -36,12 +36,12 @@ private enum GameType
 
 private class Game : Object
 {
-    [CCode (notify = false)] public int size            { internal get; protected construct; }
+    [CCode (notify = false)] public int8 size           { internal get; protected construct; }
     [CCode (notify = false)] public GameType game_type  { internal get; protected construct; }
 
     /* tiles: -1 is the empty tile, if any */
-    private int [,] tiles;
-    internal int get_tile_value (uint x, uint y) { return tiles [x, y]; }
+    private int8 [,] tiles;
+    internal int8 get_tile_value (int8 x, int8 y) { return tiles [x, y]; }
 
     /* undoing */
     private UndoItem? state = null;
@@ -49,12 +49,12 @@ private class Game : Object
     private uint moves_count = 0;
 
     /* position of the empty tile, if any */
-    private int x_gap = 0;
-    private int y_gap = 0;
+    private int8 x_gap = 0;
+    private int8 y_gap = 0;
 
     /* signals */
     internal signal void complete ();
-    internal signal void move (bool x_axis, int number, int x_gap, int y_gap, uint moves_count, bool 
disable_animation);
+    internal signal void move (bool x_axis, int8 number, int8 x_gap, int8 y_gap, uint moves_count, bool 
disable_animation);
     internal signal void bad_click (BadClick reason, bool keyboard_call);
 
     internal enum BadClick {
@@ -73,21 +73,21 @@ private class Game : Object
         do { generate_game (game_type, size, out tiles); } while (check_complete (ref tiles));
     }
 
-    internal Game (GameType game_type = GameType.FIFTEEN, int size = 4)
+    internal Game (GameType game_type = GameType.FIFTEEN, int8 size = 4)
         requires (size >= 2)
         requires (size <= 9)
     {
         Object (game_type: game_type, size: size);
     }
 
-    private static void generate_game (GameType game_type, int size, out int [,] tiles)
+    private static void generate_game (GameType game_type, int8 size, out int8 [,] tiles)
     {
-        var ntiles = size * size;
-        var line = new int? [ntiles];
-        var i = 0;
-        for (var n = ntiles - 1; n >= 0; n--)
+        int8 ntiles = size * size;             // size <= 9
+        int8? [] line = new int8? [ntiles];
+        int i = 0;
+        for (int8 n = ntiles - 1; n >= 0; n--)
         {
-            do { i = Random.int_range (0, ntiles); } while (line [i] != null);       // TODO "i == n ||" ?
+            do { i = Random.int_range (0, (int) ntiles); } while (line [i] != null);       // TODO "i == n 
||" ?
             line [i] = n;
         }
 
@@ -101,24 +101,24 @@ private class Game : Object
         /* Play with parities */
         bool parity_grid = (bool) ((size % 2) ^ (size % 2)) & 1 == 0;
         bool parity_game = false;
-        for (var j = 0; j < ntiles - 1; j++)
-            for (var k = j + 1; k < ntiles; k++)
+        for (uint8 j = 0; j < ntiles - 1; j++)
+            for (uint8 k = j + 1; k < ntiles; k++)
                 if (line [j] > line [k])
                     parity_game = !parity_game;
 
         if (parity_game != parity_grid)
         {
-            var save = line [1];
+            int8? save = line [1];
             line [1] = line [size + 1];
             line [size + 1] = save;
         }
 
         /* Now construct the game description */
-        tiles = new int [size, size];
+        tiles = new int8 [size, size];
 
-        for (var j = 0; j < ntiles; j++)
+        for (uint8 j = 0; j < ntiles; j++)
         {
-            int? line_j = line [j];
+            int8? line_j = line [j];
             if (line_j == null)
                 assert_not_reached ();
             tiles [j % size, j / size] = (!) line_j;
@@ -129,9 +129,9 @@ private class Game : Object
     {
         string s = "\n";
 
-        for (int x = 0; x < size; x++)
+        for (uint8 x = 0; x < size; x++)
         {
-            for (int y = 0; y < size; y++)
+            for (uint8 y = 0; y < size; y++)
                 s += " " + (tiles [y, x] + 1).to_string ();
             s += "\n";
         }
@@ -143,7 +143,7 @@ private class Game : Object
     * * Game code
     \*/
 
-    internal void request_move (int x, int y, bool keyboard_call)
+    internal void request_move (int8 x, int8 y, bool keyboard_call)
     {
         if (game_type == GameType.FIFTEEN)
         {
@@ -176,15 +176,16 @@ private class Game : Object
         }
     }
 
-    private void fifteen_move (int x, int y, bool undoing = false, bool restarting = false)
+    private void fifteen_move (int8 x, int8 y, bool undoing = false, bool restarting = false)
         requires (!restarting || undoing)
-        requires ((x >= 0) && (x < size) && (y >= 0) && (y < size))
+        requires ((x >= 0) && (x < size))
+        requires ((y >= 0) && (y < size))
     {
         /* we do the move before notifying */
         bool was_complete = check_complete (ref tiles);
 
-        var move_x_axis = x != x_gap;
-        var move_number = move_x_axis ? x_gap - x : y_gap - y;
+        bool move_x_axis = x != x_gap;
+        int8 move_number = move_x_axis ? x_gap - x : y_gap - y;
 
         if (undoing)
             moves_count--;
@@ -215,39 +216,42 @@ private class Game : Object
             complete ();
     }
 
-    private void sixteen_move (int x, int y, bool undoing = false, bool restarting = false)
+    private void sixteen_move (int8 x, int8 y, bool undoing = false, bool restarting = false)
         requires (!restarting || undoing)
         requires ((x < 0) || (x >= size) || (y < 0) || (y >= size))
     {
-        var move_x_axis = false;
+        bool move_x_axis;
         if (x < 0 || x >= size)
         {
             if (y < 0 || y >= size)
                 return;
-            else
-                move_x_axis = true;
+            move_x_axis = true;
+        }
+        else
+        {
+            if (y >= 0 && y < size)
+                return;
+            move_x_axis = false;
         }
-        else if (y >= 0 && y < size)
-            return;
 
         /* we do the move before notifying */
         bool was_complete = check_complete (ref tiles);
 
-        var new_coord = 0;
+        int8 new_coord = 0;
         if (move_x_axis)
         {
             if (x < 0)
             {
-                var tmp = tiles [0, y];
-                for (var i = 0; i < size - 1; i++)
+                int8 tmp = tiles [0, y];
+                for (uint8 i = 0; i < size - 1; i++)
                     tiles [i, y] = tiles [i + 1, y];
                 tiles [size - 1, y] = tmp;
                 new_coord = size - 1;
             }
             else
             {
-                var tmp = tiles [size - 1, y];
-                for (var i = size - 1; i > 0; i--)
+                int8 tmp = tiles [size - 1, y];
+                for (uint8 i = size - 1; i > 0; i--)
                     tiles [i, y] = tiles [i - 1, y];
                 tiles [0, y] = tmp;
                 new_coord = 0;
@@ -257,16 +261,16 @@ private class Game : Object
         {
             if (y < 0)
             {
-                var tmp = tiles [x, 0];
-                for (var i = 0; i < size - 1; i++)
+                int8 tmp = tiles [x, 0];
+                for (uint8 i = 0; i < size - 1; i++)
                     tiles [x, i] = tiles [x, i + 1];
                 tiles [x, size - 1] = tmp;
                 new_coord = size - 1;
             }
             else
             {
-                var tmp = tiles [x, size - 1];
-                for (var i = size - 1; i > 0; i--)
+                int8 tmp = tiles [x, size - 1];
+                for (uint8 i = size - 1; i > 0; i--)
                     tiles [x, i] = tiles [x, i - 1];
                 tiles [x, 0] = tmp;
                 new_coord = 0;
@@ -291,10 +295,10 @@ private class Game : Object
             complete ();
     }
 
-    private static bool check_complete (ref int [,] tiles)
+    private static bool check_complete (ref int8 [,] tiles)
     {
-        uint size = tiles.length [0];   /* 2 <= size <= 9 */
-        for (int i = 1; i < size * size; i++)
+        uint8 size = (uint8) tiles.length [0];  /* 2 <= size <= 9 */
+        for (uint8 i = 1; i < size * size; i++)
             if (i != tiles [i % size, i / size])
                 return false;
         return true;
@@ -306,8 +310,8 @@ private class Game : Object
 
     private struct UndoItem
     {
-        public int x;
-        public int y;
+        public int8 x;
+        public int8 y;
         public UndoItem? next;
         public UndoItem? previous;
     }
@@ -340,7 +344,7 @@ private class Game : Object
         }
     }
 
-    private void add_move (int x_gap, int y_gap)
+    private void add_move (int8 x_gap, int8 y_gap)
     {
         previous_state = state == null ? null : state;
         state = UndoItem () { x = x_gap, y = y_gap, next = null, previous = previous_state };
diff --git a/src/taquin-main.vala b/src/taquin-main.vala
index 6c3fbf7..da52153 100644
--- a/src/taquin-main.vala
+++ b/src/taquin-main.vala
@@ -188,10 +188,10 @@ private class Taquin : Gtk.Application, BaseApplication
         /* New-game screen signals */
         settings.changed ["size"].connect (() => {
             if (!size_changed)
-                new_game_screen.update_size_button_label (settings.get_int ("size"));
+                new_game_screen.update_size_button_label (settings.get_int ("size") /* 2 <= size <= 9 */);
             size_changed = false;
         });
-        new_game_screen.update_size_button_label (settings.get_int ("size"));
+        new_game_screen.update_size_button_label (settings.get_int ("size") /* 2 <= size <= 9 */);
 
         settings.changed ["theme"].connect (() => {
             if (!theme_changed)
@@ -251,14 +251,14 @@ private class Taquin : Gtk.Application, BaseApplication
         }
 
         GameType type = (GameType) settings.get_enum ("type");
-        int size = settings.get_int ("size");
+        int8 size = (int8) settings.get_int ("size"); /* 2 <= size <= 9 */
         game = new Game (type, size);
         view.game = (!) game;
         window.move_done (0);
         move_done = false;
 
         string filename = "";
-        var dirlist = theme_dirlist.copy ();
+        List<weak string> dirlist = theme_dirlist.copy ();
         do
         {
             int random = Random.int_range (0, (int) dirlist.length());
@@ -297,7 +297,7 @@ private class Taquin : Gtk.Application, BaseApplication
     * * Signals from game
     \*/
 
-    private void move_cb (bool x_axis, int number, int x_gap, int y_gap, uint moves_count, bool 
disable_animation)
+    private void move_cb (bool x_axis, int8 number, int8 x_gap, int8 y_gap, uint moves_count, bool 
disable_animation)
     {
         window.move_done (moves_count);
         play_sound ("sliding-1");       // TODO sliding-n??
@@ -350,7 +350,7 @@ private class Taquin : Gtk.Application, BaseApplication
     {
         size_changed = true;
         int size = int.parse (((!) variant).get_string ());
-        new_game_screen.update_size_button_label (size);
+        new_game_screen.update_size_button_label (size /* 3 <= size <= 5 */);
         settings.set_int ("size", size);
     }
 
@@ -373,7 +373,7 @@ private class Taquin : Gtk.Application, BaseApplication
             dir = Dir.open (Path.build_filename (DATA_DIRECTORY, "themes", theme));
             while (true)
             {
-                var filename = dir.read_name ();
+                string? filename = dir.read_name ();
                 if (filename == null)
                     break;
                 theme_dirlist.append ((!) filename);
diff --git a/src/taquin-view.vala b/src/taquin-view.vala
index 527e1b3..c6a4018 100644
--- a/src/taquin-view.vala
+++ b/src/taquin-view.vala
@@ -54,8 +54,8 @@ private class TaquinView : Gtk.DrawingArea
     }
 
     /* Arrows (or lights) place */
-    private int x_arrow = 0;
-    private int y_arrow = 0;
+    private int8 x_arrow = 0;
+    private int8 y_arrow = 0;
     private bool draw_lights = false;
 
     /* Pre-rendered image */
@@ -67,8 +67,8 @@ private class TaquinView : Gtk.DrawingArea
     private int animation_offset;
     private bool x_axis;
     private int number;
-    private int x_gap;
-    private int y_gap;
+    private int8 x_gap;
+    private int8 y_gap;
     private bool animate_end = false;
     private bool finished = false;
     private double animation_end_offset;
@@ -329,10 +329,10 @@ private class TaquinView : Gtk.DrawingArea
                                                  ref double animation_end_offset,
                                                  ref int    grid_border_main,
                                                  ref int    tile_size,
-                                                 ref int    x_arrow,
+                                                 ref int8    x_arrow,
                                                  ref int    grid_border_thin,
                                                  ref int    board_size,
-                                                 ref int    y_arrow)
+                                                 ref int8    y_arrow)
     {
         double half_grid_borders_sum = (grid_border_main + grid_border_thin) / 2.0;
         int board_size_plus_borders_diff = board_size + grid_border_main - grid_border_thin;
@@ -388,8 +388,8 @@ private class TaquinView : Gtk.DrawingArea
         _draw_movable_arrows (cr, ref x_arrow, ref y_arrow, ref grid_border_main, ref tile_size, ref 
grid_border_thin, ref board_size);
     }
     private static inline void _draw_movable_arrows (Cairo.Context cr,
-                                                 ref int x_arrow,
-                                                 ref int y_arrow,
+                                                 ref int8 x_arrow,
+                                                 ref int8 y_arrow,
                                                  ref int grid_border_main,
                                                  ref int tile_size,
                                                  ref int grid_border_thin,
@@ -435,7 +435,7 @@ private class TaquinView : Gtk.DrawingArea
         cr.line_to (grid_border_main + tile_size * (number + 2.0 / 3), inside ? y1 : y2);
     }
 
-    private void move_cb (bool x_axis, int number, int x_gap, int y_gap, uint moves_count, bool 
disable_animation)
+    private void move_cb (bool x_axis, int8 number, int8 x_gap, int8 y_gap, uint moves_count, bool 
disable_animation)
     {
         this.x_axis = x_axis;
         this.number = number;
@@ -481,8 +481,8 @@ private class TaquinView : Gtk.DrawingArea
         if (event.button == Gdk.BUTTON_PRIMARY || event.button == Gdk.BUTTON_SECONDARY)
         {
             draw_lights = false;
-            game.request_move ((int) (event.x - x_offset - grid_border_main + tile_size) / tile_size - 1,
-                               (int) (event.y - y_offset - grid_border_main + tile_size) / tile_size - 1,
+            game.request_move ((int8) ((int) (event.x - x_offset - grid_border_main + tile_size) / tile_size 
- 1),
+                               (int8) ((int) (event.y - y_offset - grid_border_main + tile_size) / tile_size 
- 1),
                                /* keyboard */ false);
         }
         return true;
diff --git a/src/test-taquin.vala b/src/test-taquin.vala
index 7665ab4..86a56d4 100644
--- a/src/test-taquin.vala
+++ b/src/test-taquin.vala
@@ -96,7 +96,11 @@ private class TestTaquin : Object
                 Test.fail ();
         }
     }
-    private static bool compare_value (ref Game game, uint x, uint y, uint k)
+    private static bool compare_value (ref Game game, int8 x, int8 y, int8 k)
+        requires (x >= 0)
+        requires (x < game.size)
+        requires (y >= 0)
+        requires (y < game.size)
     {
         return game.get_tile_value (x, y) + 1 == k;
     }


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