[iagno] Use uint8 when possible.



commit 6b9ae803b42fbc6e7657fb30830932c6f04c04db
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sun Feb 10 06:53:47 2019 +0100

    Use uint8 when possible.

 src/computer-player.vala | 44 +++++++++++-----------
 src/game-view.vala       | 41 +++++++++++----------
 src/game.vala            | 96 +++++++++++++++++++++++++-----------------------
 src/iagno.vala           | 10 ++++-
 4 files changed, 102 insertions(+), 89 deletions(-)
---
diff --git a/src/computer-player.vala b/src/computer-player.vala
index c0914e0..5a5a310 100644
--- a/src/computer-player.vala
+++ b/src/computer-player.vala
@@ -22,11 +22,11 @@ private class ComputerPlayer : Object
 {
     private struct PossibleMove
     {
-        public int x;
-        public int y;
-        public int n_tiles;
+        public uint8 x;
+        public uint8 y;
+        public uint8 n_tiles;
 
-        private PossibleMove (int x, int y, int n_tiles)
+        private PossibleMove (uint8 x, uint8 y, uint8 n_tiles)
         {
             this.x = x;
             this.y = y;
@@ -88,7 +88,7 @@ private class ComputerPlayer : Object
         this.difficulty_level = difficulty_level;
     }
 
-    private void complete_move (int x, int y)
+    private void complete_move (uint8 x, uint8 y)
     {
         if (game.place_tile (x, y) == 0)
         {
@@ -100,8 +100,8 @@ private class ComputerPlayer : Object
     /* For tests only. */
     internal void move ()
     {
-        int x = 0;
-        int y = 0;
+        uint8 x = 0;
+        uint8 y = 0;
 
         run_search (ref x, ref y);
         complete_move (x, y);
@@ -110,8 +110,8 @@ private class ComputerPlayer : Object
     internal async void move_async (double delay_seconds = 0.0)
     {
         var timer = new Timer ();
-        int x = 0;
-        int y = 0;
+        uint8 x = 0;
+        uint8 y = 0;
 
         while (move_pending)
         {
@@ -172,7 +172,7 @@ private class ComputerPlayer : Object
     * * Minimax / Negamax / alpha-beta pruning
     \*/
 
-    private void run_search (ref int x, ref int y)
+    private void run_search (ref uint8 x, ref uint8 y)
         requires (game.current_player_can_move)
     {
         /* For the first/first two moves play randomly so the game is not always the same */
@@ -277,15 +277,15 @@ private class ComputerPlayer : Object
 
     private void get_possible_moves_sorted (Game g, ref List<PossibleMove?> moves)
     {
-        for (var x = 0; x < g.size; x++)
+        for (uint8 x = 0; x < g.size; x++)
         {
-            for (var y = 0; y < g.size; y++)
+            for (uint8 y = 0; y < g.size; y++)
             {
-                var n_tiles = g.place_tile (x, y, false);
-                if (n_tiles <= 0)
+                uint8 n_tiles = g.place_tile (x, y, false);
+                if (n_tiles == 0)
                     continue;
 
-                var move = PossibleMove (x, y, n_tiles);
+                PossibleMove move = PossibleMove (x, y, n_tiles);
                 moves.insert_sorted (move, compare_move);
             }
         }
@@ -366,7 +366,7 @@ private class ComputerPlayer : Object
         return count;
     }
 
-    private static int is_empty (Game g, int x, int y)
+    private static int is_empty (Game g, uint8 x, uint8 y)
     {
         if (g.is_valid_location (x, y) && g.get_owner (x, y) == Player.NONE)
             return 1;
@@ -378,11 +378,11 @@ private class ComputerPlayer : Object
     * * First random moves
     \*/
 
-    private static void random_select (Game g, out int move_x, out int move_y)
+    private static void random_select (Game g, out uint8 move_x, out uint8 move_y)
     {
-        List<int> moves = new List<int> ();
-        for (var x = 0; x < g.size; x++)
-            for (var y = 0; y < g.size; y++)
+        List<uint8> moves = new List<uint8> ();
+        for (uint8 x = 0; x < g.size; x++)
+            for (uint8 y = 0; y < g.size; y++)
                 if (g.can_place (x, y, g.current_color))
                     moves.append (x * g.size + y);
 
@@ -390,8 +390,8 @@ private class ComputerPlayer : Object
         if (length == 0)
             assert_not_reached ();
 
-        var i = Random.int_range (0, length);
-        var xy = moves.nth_data (i);
+        uint8 i = (uint8) Random.int_range (0, length);
+        uint8 xy = moves.nth_data (i);
         move_x = xy / g.size;
         move_y = xy % g.size;
     }
diff --git a/src/game-view.vala b/src/game-view.vala
index fb8f405..ed6e496 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -72,8 +72,8 @@ private class GameView : Gtk.DrawingArea
 
     /* Keyboard */
     private bool show_highlight;
-    private int highlight_x;
-    private int highlight_y;
+    private uint8 highlight_x;
+    private uint8 highlight_y;
     private int highlight_state;
     private const int HIGHLIGHT_MAX = 5;
 
@@ -94,7 +94,7 @@ private class GameView : Gtk.DrawingArea
     // private double cursor = 0;
     private int current_player_number = 0;
 
-    internal signal void move (int x, int y);
+    internal signal void move (uint8 x, uint8 y);
 
     /* Used for a delay between the last move and flipping the pieces */
     private bool flip_final_result_now = false;
@@ -120,9 +120,9 @@ private class GameView : Gtk.DrawingArea
             _game = value;
             game_is_set = true;
             pixmaps = new int [_game.size, _game.size];
-            for (int x = 0; x < _game.size; x++)
-                for (int y = 0; y < _game.size; y++)
-                    pixmaps[x, y] = get_pixmap (_game.get_owner (x, y));
+            for (uint8 x = 0; x < _game.size; x++)
+                for (uint8 y = 0; y < _game.size; y++)
+                    pixmaps [x, y] = get_pixmap (_game.get_owner (x, y));
             _game.notify ["is-complete"].connect (game_is_complete_cb);
             _game.square_changed.connect (square_changed_cb);
 
@@ -399,14 +399,14 @@ private class GameView : Gtk.DrawingArea
          */
         Timeout.add_seconds (2, () =>  {
             flip_final_result_now = true;
-            for (int x = 0; x < game.size; x++)
-                for (int y = 0; y < game.size; y++)
+            for (uint8 x = 0; x < game.size; x++)
+                for (uint8 y = 0; y < game.size; y++)
                     update_square (x, y);
             return Source.REMOVE;
         });
     }
 
-    private void square_changed_cb (int x, int y, Player replacement, bool undoing)
+    private void square_changed_cb (uint8 x, uint8 y, Player replacement, bool undoing)
     {
         if (replacement == Player.NONE)
         {
@@ -416,7 +416,7 @@ private class GameView : Gtk.DrawingArea
         update_square (x, y, undoing);  // FIXME undoing is only for when undoing the counter turn
     }
 
-    private void update_square (int x, int y, bool undoing = false)
+    private void update_square (uint8 x, uint8 y, bool undoing = false)
         requires (game_is_set)
     {
         int pixmap = get_pixmap (game.get_owner (x, y));
@@ -424,7 +424,7 @@ private class GameView : Gtk.DrawingArea
         /* Show the result by laying the tiles with winning color first */
         if (flip_final_result_now && game.is_complete && !undoing)
         {
-            int n = y * game.size + x;
+            uint8 n = y * game.size + x;
             Player winning_color = Player.LIGHT;
             Player losing_color = Player.DARK;
             int n_winning_tiles = game.n_light_tiles;
@@ -453,7 +453,7 @@ private class GameView : Gtk.DrawingArea
         set_square (x, y, pixmap);
     }
 
-    private void set_square (int x, int y, int pixmap)
+    private void set_square (uint8 x, uint8 y, int pixmap)
     {
         if (pixmaps[x, y] == pixmap)
             return;
@@ -469,7 +469,10 @@ private class GameView : Gtk.DrawingArea
             if (animate_timeout == 0)
                 animate_timeout = Timeout.add (PIXMAP_FLIP_DELAY, animate_cb);
         }
-        queue_draw_area (board_x + x * paving_size, board_y + y * paving_size, tile_size, tile_size);
+        queue_draw_area ((int) (board_x + x * paving_size),
+                         (int) (board_y + y * paving_size),
+                         tile_size,
+                         tile_size);
     }
 
     private bool animate_cb ()
@@ -477,13 +480,13 @@ private class GameView : Gtk.DrawingArea
     {
         bool animating = false;
 
-        for (int x = 0; x < game.size; x++)
+        for (uint8 x = 0; x < game.size; x++)
         {
-            for (int y = 0; y < game.size; y++)
+            for (uint8 y = 0; y < game.size; y++)
             {
-                int old = pixmaps[x, y];
+                int old = pixmaps [x, y];
                 update_square (x, y);
-                if (pixmaps[x, y] != old)
+                if (pixmaps [x, y] != old)
                     animating = true;
             }
         }
@@ -518,8 +521,8 @@ private class GameView : Gtk.DrawingArea
 
         if (event.button == Gdk.BUTTON_PRIMARY || event.button == Gdk.BUTTON_SECONDARY)
         {
-            int x = (int) (event.x - board_x) / paving_size;
-            int y = (int) (event.y - board_y) / paving_size;
+            uint8 x = (uint8) ((event.x - board_x) / paving_size);
+            uint8 y = (uint8) ((event.y - board_y) / paving_size);
             if (x >= 0 && x < game.size && y >= 0 && y < game.size)
             {
                 show_highlight = false;
diff --git a/src/game.vala b/src/game.vala
index 39f2339..4ae2ed8 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -21,23 +21,23 @@
 private class Game : Object
 {
     /* Tiles on the board */
-    private Player[,] tiles;
+    private Player [,] tiles;
 
-    private int _size;
-    [CCode (notify = false)] internal int size
+    private uint8 _size;
+    [CCode (notify = false)] internal uint8 size
     {
         internal get { return _size; }
         private set { _size = value; }
     }
 
     /* Undoing */
-    private int?[] undo_stack;
+    private uint8? [] undo_stack;
     private int history_index = -1;
 
     /* Color to move next; Dark always plays first;
      * should be dark if number_of_moves % 2 == 0 */
     [CCode (notify = false)] internal Player current_color { internal get; private set; default = 
Player.DARK; }
-    [CCode (notify = false)] internal int number_of_moves { internal get; private set; default = 0; }
+    [CCode (notify = false)] internal uint8 number_of_moves { internal get; private set; default = 0; }
 
     /* Indicate who's the next player who can move */
     [CCode (notify = false)] internal bool current_player_can_move { internal get; private set; default = 
true; }
@@ -46,7 +46,7 @@ private class Game : Object
     /* Indicate that a player should move */
     internal signal void turn_ended ();
     /* Indicate a square has changed */
-    internal signal void square_changed (int x, int y, Player new_color, bool undoing);
+    internal signal void square_changed (uint8 x, uint8 y, Player new_color, bool undoing);
 
     /*\
     * * Number of tiles on the board
@@ -96,19 +96,20 @@ private class Game : Object
     * * Creation / exporting
     \*/
 
-    internal Game (bool alternative_start = false, int tmp_size = 8)
+    internal Game (bool alternative_start = false, uint8 tmp_size = 8)
         requires (tmp_size >= 4)
+        requires (tmp_size <= 16)
     {
         size = tmp_size;
-        tiles = new Player[size, size];
-        for (var x = 0; x < size; x++)
-            for (var y = 0; y < size; y++)
-                tiles[x, y] = Player.NONE;
+        tiles = new Player [size, size];
+        for (uint8 x = 0; x < size; x++)
+            for (uint8 y = 0; y < size; y++)
+                tiles [x, y] = Player.NONE;
 
         /* Stack is oversized: there is 60 turns, each adds one piece,
          * there's place for the end of turn and the opponent passing,
          * and you could flip max ((size - 2) * 3) tiles in one turn. */
-        undo_stack = new int?[180 * (size - 1)]; /* (3 + (size - 2) * 3) * 60 */
+        undo_stack = new int? [180 * (size - 1)]; /* (3 + (size - 2) * 3) * 60 */
 
         if (size % 2 == 0)
         {
@@ -137,19 +138,19 @@ private class Game : Object
         }
     }
 
-    internal Game.from_strings (string [] setup, Player to_move, int tmp_size = 8)
+    internal Game.from_strings (string [] setup, Player to_move, uint8 tmp_size = 8)
         requires (setup.length == tmp_size)
     {
         size = tmp_size;
         tiles = new Player[size, size];
-        undo_stack = new int?[180 * (size - 1)];
+        undo_stack = new int? [180 * (size - 1)];
 
-        for (int y = 0; y < size; y++)
+        for (uint8 y = 0; y < size; y++)
         {
-            if (setup[y].length != size * 2)
+            if (setup [y].length != size * 2)
                 warn_if_reached ();
-            for (int x = 0; x < size; x++)
-                tiles[x, y] = Player.from_char (setup[y][x * 2 + 1]);
+            for (uint8 x = 0; x < size; x++)
+                tiles [x, y] = Player.from_char (setup [y][x * 2 + 1]);
         }
 
         current_color = to_move;
@@ -174,15 +175,18 @@ private class Game : Object
     internal Game.copy (Game game)
     {
         size = game.size;
-        tiles = new Player[size, size];
-        undo_stack = new int?[180 * (size - 1)];
-        for (var x = 0; x < size; x++)
-            for (var y = 0; y < size; y++)
-                tiles[x, y] = game.tiles[x, y];
-        number_of_moves = game.number_of_moves;
-        current_color = game.current_color;
-        n_current_tiles = game.n_current_tiles;
+        tiles = new Player [size, size];
+
+        for (uint8 x = 0; x < size; x++)
+            for (uint8 y = 0; y < size; y++)
+                tiles [x, y] = game.tiles [x, y];
+
+        number_of_moves  = game.number_of_moves;
+        current_color    = game.current_color;
+        n_current_tiles  = game.n_current_tiles;
         n_opponent_tiles = game.n_opponent_tiles;
+
+        undo_stack = new int? [180 * (size - 1)];
         /* warning: history not copied */
     }
 
@@ -190,22 +194,22 @@ private class Game : Object
     * * Public information
     \*/
 
-    internal bool is_valid_location (int x, int y)
+    internal bool is_valid_location (uint8 x, uint8 y)
     {
-        return x >= 0 && x < size && y >= 0 && y < size;
+        return x < size && y < size;
     }
 
-    internal Player get_owner (int x, int y)
+    internal Player get_owner (uint8 x, uint8 y)
         requires (is_valid_location (x, y))
     {
-        return tiles[x, y];
+        return tiles [x, y];
     }
 
-    internal bool can_place (int x, int y, Player color)
+    internal bool can_place (uint8 x, uint8 y, Player color)
         requires (is_valid_location (x, y))
         requires (color != Player.NONE)
     {
-        if (tiles[x, y] != Player.NONE)
+        if (tiles [x, y] != Player.NONE)
             return false;
 
         if (can_flip_tiles (x, y, 1, 0, color) > 0) return true;
@@ -223,7 +227,7 @@ private class Game : Object
     * * Actions (apart undo)
     \*/
 
-    internal int place_tile (int x, int y, bool apply = true)
+    internal uint8 place_tile (uint8 x, uint8 y, bool apply = true)
         requires (is_valid_location (x, y))
     {
         if (tiles[x, y] != Player.NONE)
@@ -263,7 +267,7 @@ private class Game : Object
         current_color = Player.flip_color (current_color);
         number_of_moves++;
         history_index++;
-        undo_stack[history_index] = null;
+        undo_stack [history_index] = null;
         update_who_can_move ();
         turn_ended ();
     }
@@ -294,7 +298,7 @@ private class Game : Object
     * * Flipping tiles
     \*/
 
-    private int flip_tiles (int x, int y, int x_step, int y_step, bool apply)
+    private int flip_tiles (uint8 x, uint8 y, int8 x_step, int8 y_step, bool apply)
     {
         var enemy_count = can_flip_tiles (x, y, x_step, y_step, current_color);
         if (enemy_count == 0)
@@ -311,19 +315,19 @@ private class Game : Object
         return enemy_count;
     }
 
-    private int can_flip_tiles (int x, int y, int x_step, int y_step, Player color)
+    private int can_flip_tiles (uint8 x, uint8 y, int8 x_step, int8 y_step, Player color)
     {
         var enemy = Player.flip_color (color);
 
         /* Count number of enemy pieces we are beside */
         var enemy_count = -1;
-        var xt = x;
-        var yt = y;
+        uint8 xt = x;
+        uint8 yt = y;
         do {
             enemy_count++;
             xt += x_step;
             yt += y_step;
-        } while (is_valid_location (xt, yt) && tiles[xt, yt] == enemy);
+        } while (is_valid_location (xt, yt) && tiles [xt, yt] == enemy);
 
         /* Must be a line of enemy pieces then one of ours */
         if (enemy_count == 0 || !is_valid_location (xt, yt) || tiles[xt, yt] != color)
@@ -332,13 +336,13 @@ private class Game : Object
         return enemy_count;
     }
 
-    private void set_tile (int x, int y)
+    private void set_tile (uint8 x, uint8 y)
         requires (history_index >= -1 && history_index < undo_stack.length - 2)
     {
         n_current_tiles++;
         history_index++;
-        undo_stack[history_index] = x + y * size;
-        tiles[x, y] = current_color;
+        undo_stack [history_index] = x + y * size;
+        tiles [x, y] = current_color;
         square_changed (x, y, current_color, /* undoing */ false);
     }
 
@@ -359,7 +363,7 @@ private class Game : Object
         history_index--;
 
         /* if not pass */
-        int? undo_item = undo_stack [history_index];
+        uint8? undo_item = undo_stack [history_index];
         if (undo_item != null)
         {
             /* last log entry is the placed tile, previous are flipped tiles */
@@ -387,12 +391,12 @@ private class Game : Object
         }
     }
 
-    private void unset_tile (int tile_number, Player replacement_color)
+    private void unset_tile (uint8 tile_number, Player replacement_color)
     {
         n_current_tiles--;
         history_index--;
-        var x = tile_number % size;
-        var y = tile_number / size;
+        uint8 x = tile_number % size;
+        uint8 y = tile_number / size;
         tiles [x, y] = replacement_color;
         square_changed (x, y, replacement_color, /* undoing */ true);
     }
diff --git a/src/iagno.vala b/src/iagno.vala
index ec3bad2..7debeef 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -136,6 +136,12 @@ private class Iagno : Gtk.Application
             stderr.printf ("%s\n", _("Size must be at least 4."));
             return Posix.EXIT_FAILURE;
         }
+        if (size > 16)
+        {
+            /* Translators: command-line error message, displayed for an incorrect game size request; try 
'iagno -s 17' */
+            stderr.printf ("%s\n", _("Size must not be more than 16."));
+            return Posix.EXIT_FAILURE;
+        }
 
         if (options.contains ("mute"))
             sound = false;
@@ -358,7 +364,7 @@ private class Iagno : Gtk.Application
         if (computer != null)
             ((!) computer).cancel_move ();
 
-        game = new Game (alternative_start, size);
+        game = new Game (alternative_start, (uint8) size /* 4 <= size <= 16 */);
         game_is_set = true;
         game.turn_ended.connect (turn_ended_cb);
         view.game = game;
@@ -497,7 +503,7 @@ private class Iagno : Gtk.Application
             play_sound (Sound.GAMEOVER);
     }
 
-    private void player_move_cb (int x, int y)
+    private void player_move_cb (uint8 x, uint8 y)
         requires (game_is_set)
     {
         /* Ignore if we are waiting for the AI to move or if game is finished */


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