[iagno] Improve readability.



commit 1f1df3d698d2013913f2d6ddf4278646f630e2f3
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Apr 17 18:29:05 2019 +0200

    Improve readability.

 src/computer-player.vala | 11 +++++-----
 src/game-view.vala       | 54 +++++++++++++++++++++++++++---------------------
 src/game.vala            | 47 +++++++++++++++++++----------------------
 3 files changed, 58 insertions(+), 54 deletions(-)
---
diff --git a/src/computer-player.vala b/src/computer-player.vala
index 1a4d38e..4b10032 100644
--- a/src/computer-player.vala
+++ b/src/computer-player.vala
@@ -399,10 +399,11 @@ private class ComputerPlayer : Object
     private static void random_select (Game g, out uint8 move_x, out uint8 move_y)
     {
         List<uint8> moves = new List<uint8> ();
-        for (uint8 x = 0; x < g.size; x++)
-            for (uint8 y = 0; y < g.size; y++)
+        uint8 size = g.size;
+        for (uint8 x = 0; x < size; x++)
+            for (uint8 y = 0; y < size; y++)
                 if (g.can_place (x, y, g.current_color))
-                    moves.append (x * g.size + y);
+                    moves.append (x * size + y);
 
         int length = (int) moves.length ();
         if (length <= 0)
@@ -410,7 +411,7 @@ private class ComputerPlayer : Object
 
         uint8 i = (uint8) Random.int_range (0, length);
         uint8 xy = moves.nth_data (i);
-        move_x = xy / g.size;
-        move_y = xy % g.size;
+        move_x = xy / size;
+        move_y = xy % size;
     }
 }
diff --git a/src/game-view.vala b/src/game-view.vala
index 116177d..e102ecd 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -455,33 +455,43 @@ private class GameView : Gtk.DrawingArea
     * * turning tiles
     \*/
 
-    private void square_changed_cb (uint8 x, uint8 y, Player replacement)
+    private inline void update_highlight_after_undo (uint8 x, uint8 y)
     {
-        if (replacement == Player.NONE)
+        // clear the previous highlight (if any)
+        if (show_highlight)
         {
-            // clear the previous highlight (if any)
-            if (show_highlight)
-            {
-                highlight_state = 0;
-                set_square (highlight_x,
-                            highlight_y,
-                            get_pixmap (game.get_owner (x, y)),
-                            /* is final animation */ false,
-                            /* force redraw */ true);
-                // no highliqht animation after undo
-                if (!game.is_complete)
-                    highlight_state = HIGHLIGHT_MAX;
-            }
-
-            // set highlight on undone play position
-            highlight_set = true;
-            highlight_x = x;
-            highlight_y = y;
+            highlight_state = 0;
+            set_square (highlight_x,
+                        highlight_y,
+                        get_pixmap (game.get_owner (x, y)),
+                        /* is final animation */ false,
+                        /* force redraw */ true);
+            // no highlight animation after undo
+            if (!game.is_complete)
+                highlight_state = HIGHLIGHT_MAX;
         }
 
+        // set highlight on undone play position
+        highlight_set = true;
+        highlight_x = x;
+        highlight_y = y;
+    }
+
+    private void square_changed_cb (uint8 x, uint8 y, Player replacement)
+    {
+        if (replacement == Player.NONE)
+            update_highlight_after_undo (x, y);
+
         update_square (x, y);
     }
 
+    private inline void update_squares ()
+    {
+        for (uint8 x = 0; x < game_size; x++)
+            for (uint8 y = 0; y < game_size; y++)
+                update_square (x, y);
+    }
+
     private inline void update_square (uint8 x, uint8 y)
         requires (game_is_set)
     {
@@ -628,9 +638,7 @@ private class GameView : Gtk.DrawingArea
             return false;
 
         flip_final_result_now = false;
-        for (uint8 x = 0; x < game_size; x++)
-            for (uint8 y = 0; y < game_size; y++)
-                update_square (x, y);
+        update_squares ();
 
         return true;
     }
diff --git a/src/game.vala b/src/game.vala
index 34022f0..bd7be36 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -107,22 +107,15 @@ private class GameState : Object
     * * number of tiles on the board
     \*/
 
-    [CCode (notify = false)] internal uint8 n_tiles
-    {
-        internal get { return n_dark_tiles + n_light_tiles; }
-    }
-
     private uint8 _n_light_tiles = 2;
-    [CCode (notify = false)] internal uint8 n_light_tiles
-    {
-        internal get { return _n_light_tiles; }
-    }
-
     private uint8 _n_dark_tiles = 2;
+
+    [CCode (notify = false)] internal uint8 n_tiles
+                                            { internal get { return _n_dark_tiles + _n_light_tiles; }}
+    [CCode (notify = false)] internal uint8 n_light_tiles
+                                            { internal get { return _n_light_tiles; }}
     [CCode (notify = false)] internal uint8 n_dark_tiles
-    {
-        internal get { return _n_dark_tiles; }
-    }
+                                            { internal get { return _n_dark_tiles; }}
 
     [CCode (notify = false)] internal uint8 n_current_tiles
     {
@@ -349,26 +342,28 @@ private class Game : GameState
 
         if (_size % 2 == 0)
         {
-            /* Setup board with four tiles by default */
+            /* setup board with four tiles by default */
             initial_number_of_tiles = 4;
-            tiles [_size / 2 - 1, _size / 2 - 1] = alternative_start ? Player.DARK : Player.LIGHT;
-            tiles [_size / 2 - 1, _size / 2] = Player.DARK;
-            tiles [_size / 2, _size / 2 - 1] = alternative_start ? Player.LIGHT : Player.DARK;
-            tiles [_size / 2, _size / 2] = Player.LIGHT;
+            uint8 half_size = _size / 2;
+            tiles [half_size - 1, half_size - 1] = alternative_start ? Player.DARK : Player.LIGHT;
+            tiles [half_size - 1, half_size    ] = Player.DARK;
+            tiles [half_size    , half_size - 1] = alternative_start ? Player.LIGHT : Player.DARK;
+            tiles [half_size    , half_size    ] = Player.LIGHT;
             n_current_tiles = 2;
             n_opponent_tiles = 2;
         }
         else
         {
-            /* Logical starting position for odd board */
+            /* logical starting position for odd board */
             initial_number_of_tiles = 7;
-            tiles [(_size - 1) / 2, (_size - 1) / 2] = Player.DARK;
-            tiles [(_size + 1) / 2, (_size - 3) / 2] = alternative_start ? Player.LIGHT : Player.DARK;
-            tiles [(_size - 3) / 2, (_size + 1) / 2] = alternative_start ? Player.LIGHT : Player.DARK;
-            tiles [(_size - 1) / 2, (_size - 3) / 2] = Player.LIGHT;
-            tiles [(_size - 3) / 2, (_size - 1) / 2] = alternative_start ? Player.DARK : Player.LIGHT;
-            tiles [(_size + 1) / 2, (_size - 1) / 2] = alternative_start ? Player.DARK : Player.LIGHT;
-            tiles [(_size - 1) / 2, (_size + 1) / 2] = Player.LIGHT;
+            uint8 mid_board = (_size - 1) / 2;
+            tiles [mid_board    , mid_board    ] = Player.DARK;
+            tiles [mid_board + 1, mid_board - 1] = alternative_start ? Player.LIGHT : Player.DARK;
+            tiles [mid_board - 1, mid_board + 1] = alternative_start ? Player.LIGHT : Player.DARK;
+            tiles [mid_board    , mid_board - 1] = Player.LIGHT;
+            tiles [mid_board - 1, mid_board    ] = alternative_start ? Player.DARK : Player.LIGHT;
+            tiles [mid_board + 1, mid_board    ] = alternative_start ? Player.DARK : Player.LIGHT;
+            tiles [mid_board    , mid_board + 1] = Player.LIGHT;
             n_current_tiles = 3;
             n_opponent_tiles = 4;
         }


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