[gnome-nibbles/arnaudb/use-uint8: 33/33] Use uint8 again more.



commit 9fc8b99ebf82ab838e505a2cfe6c15df296ca520
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri Jul 3 17:20:43 2020 +0200

    Use uint8 again more.

 src/boni.vala         | 29 +++++++++++++++++------------
 src/nibbles-game.vala | 45 ++++++++++++++++++++-------------------------
 src/nibbles-view.vala |  2 +-
 src/worm.vala         | 29 ++++++++++++++++-------------
 4 files changed, 54 insertions(+), 51 deletions(-)
---
diff --git a/src/boni.vala b/src/boni.vala
index 05cbf01..29aa93a 100644
--- a/src/boni.vala
+++ b/src/boni.vala
@@ -31,13 +31,14 @@ private enum BonusType
 
 private class Bonus : Object
 {
-    public int x                { internal get; protected construct; }
-    public int y                { internal get; protected construct; }
+    public uint8 x              { internal get; protected construct; }
+    public uint8 y              { internal get; protected construct; }
     public BonusType bonus_type { internal get; protected construct; }
     public bool fake            { internal get; protected construct; }
-    public int countdown        { internal get; internal construct set; }
 
-    internal Bonus (int x, int y, BonusType bonus_type, bool fake, int countdown)
+    public uint16 countdown     { internal get; internal construct set; }
+
+    internal Bonus (uint8 x, uint8 y, BonusType bonus_type, bool fake, uint16 countdown)
     {
         Object (x: x, y: y, bonus_type: bonus_type, fake: fake, countdown: countdown);
     }
@@ -51,7 +52,7 @@ private class Boni : Object
     private uint8 regular_bonus_maxi = 0;
     private uint8 total_bonus_number = 0;
 
-    private const int MAX_BONUSES = 100;
+    private const uint8 MAX_BONUSES = 100;
 
     internal signal void bonus_removed (Bonus bonus);
 
@@ -89,9 +90,9 @@ private class Boni : Object
         total_bonus_number = 0;
     }
 
-    internal Bonus? get_bonus (int[,] board, int x, int y)
+    internal Bonus? get_bonus (int[,] board, uint8 x, uint8 y)
     {
-        foreach (var bonus in bonuses)
+        foreach (Bonus bonus in bonuses)
         {
             if ((x == bonus.x     && y == bonus.y    )
              || (x == bonus.x + 1 && y == bonus.y    )
@@ -110,12 +111,16 @@ private class Boni : Object
         missed_bonuses_to_replace = 0;
 
         // FIXME Use an iterator instead of a second list
-        var found = new Gee.LinkedList<Bonus> ();
-        foreach (var bonus in bonuses)
-            if (bonus.countdown-- == 0)
+        Gee.LinkedList<Bonus> found = new Gee.LinkedList<Bonus> ();
+        foreach (Bonus bonus in bonuses)
+        {
+            if (bonus.countdown == 0)
                 found.add (bonus);
+            else
+                bonus.countdown--;
+        }
 
-        foreach (var bonus in found)
+        foreach (Bonus bonus in found)
         {
             bool real_bonus = bonus.bonus_type == BonusType.REGULAR && !bonus.fake;
 
@@ -129,7 +134,7 @@ private class Boni : Object
         }
     }
 
-    internal inline int new_regular_bonus_eaten ()
+    internal inline uint8 new_regular_bonus_eaten ()
     {
         if (regular_bonus_left == 0)
             assert_not_reached ();
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index f6fd26a..a3358d2 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -502,7 +502,8 @@ private class NibblesGame : Object
     private void add_bonus (bool regular)
     {
         bool good = false;
-        int x = 0, y = 0;
+        uint8 x = 0;
+        uint8 y = 0;
 
         if (!regular)
         {
@@ -513,17 +514,14 @@ private class NibblesGame : Object
         do
         {
             good = true;
-            x = Random.int_range (0, width  - 1);
-            y = Random.int_range (0, height - 1);
 
-            if (board[x, y] != EMPTYCHAR)
-                good = false;
-            if (board[x + 1, y] != EMPTYCHAR)
-                good = false;
-            if (board[x, y + 1] != EMPTYCHAR)
-                good = false;
-            if (board[x + 1, y + 1] != EMPTYCHAR)
-                good = false;
+            x = (uint8) Random.int_range (0, width  - 1);
+            y = (uint8) Random.int_range (0, height - 1);
+
+            if (board [x    , y    ] != EMPTYCHAR) { good = false; continue; }
+            if (board [x + 1, y + 1] != EMPTYCHAR) { good = false; continue; }
+            if (board [x + 1, y    ] != EMPTYCHAR) { good = false; continue; }
+            if (board [x    , y + 1] != EMPTYCHAR) { good = false; continue; }
         } while (!good);
 
         if (regular)
@@ -536,16 +534,13 @@ private class NibblesGame : Object
             {
                 good = true;
 
-                x = Random.int_range (0, width  - 1);
-                y = Random.int_range (0, height - 1);
-                if (board[x, y] != EMPTYCHAR)
-                    good = false;
-                if (board[x + 1, y] != EMPTYCHAR)
-                    good = false;
-                if (board[x, y + 1] != EMPTYCHAR)
-                    good = false;
-                if (board[x + 1, y + 1] != EMPTYCHAR)
-                    good = false;
+                x = (uint8) Random.int_range (0, width  - 1);
+                y = (uint8) Random.int_range (0, height - 1);
+
+                if (board [x    , y    ] != EMPTYCHAR) { good = false; continue; }
+                if (board [x + 1, y + 1] != EMPTYCHAR) { good = false; continue; }
+                if (board [x + 1, y    ] != EMPTYCHAR) { good = false; continue; }
+                if (board [x    , y + 1] != EMPTYCHAR) { good = false; continue; }
             }
             _add_bonus (x, y, BonusType.REGULAR, false, 300);
         }
@@ -594,7 +589,7 @@ private class NibblesGame : Object
             }
         }
     }
-    private inline void _add_bonus (int x, int y, BonusType bonus_type, bool fake, int countdown)
+    private inline void _add_bonus (uint8 x, uint8 y, BonusType bonus_type, bool fake, uint16 countdown)
     {
         Bonus bonus = new Bonus (x, y, bonus_type, fake, countdown);
         if (boni.add_bonus (board, bonus))
@@ -613,9 +608,9 @@ private class NibblesGame : Object
         switch (board[worm.head.x, worm.head.y] - 'A')
         {
             case BonusType.REGULAR:
-                int nth_bonus = boni.new_regular_bonus_eaten ();
-                worm.change += nth_bonus * Worm.GROW_FACTOR;
-                worm.score  += nth_bonus * current_level;
+                uint8 nth_bonus = boni.new_regular_bonus_eaten ();
+                worm.change += (int) nth_bonus * Worm.GROW_FACTOR;
+                worm.score  += (int) nth_bonus * current_level;
                 break;
             case BonusType.DOUBLE:
                 worm.score += (worm.length + worm.change) * current_level;
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index ff698a2..c6600c3 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -735,7 +735,7 @@ private class NibblesView : GtkClutter.Embed
         }
 
         actor.set_size (tile_size, tile_size);
-        actor.set_position (bonus.x * tile_size, bonus.y * tile_size);
+        actor.set_position ((int) bonus.x * tile_size, (int) bonus.y * tile_size);
 
         level.add_child (actor);
         if (bonus.bonus_type != BonusType.REGULAR)
diff --git a/src/worm.vala b/src/worm.vala
index eea1cbc..697b34a 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -527,16 +527,16 @@ private class Worm : Object
         uint8 width  = (uint8) /* int */ board.length [0];
         uint8 height = (uint8) /* int */ board.length [1];
 
-        if (old_position.x < 0 || old_position.x >= width
-         || old_position.y < 0 || old_position.y >= height)
+        if (old_position.x >= width
+         || old_position.y >= height)
             return 0;
 
         deadend_runnumber++;
 
         for (int i = numworms - 1; i >= 0; i--)
         {
-            int target_x = worms [i].head.x;
-            int target_y = worms [i].head.y;
+            uint8 target_x = worms [i].head.x;
+            uint8 target_y = worms [i].head.y;
             if (target_x == old_position.x
              && target_y == old_position.y)
                 continue;
@@ -567,38 +567,41 @@ private class Worm : Object
      * that is, that it's within 3 in the direction we're going and within
      * 1 to the side.
      */
-    private inline bool ai_too_close (Gee.LinkedList<Worm> worms, int numworms)
+    private inline bool ai_too_close (Gee.LinkedList<Worm> worms)
     {
-        int i = numworms;
-        int dx, dy;
-
-        while (i-- > 0)
+        foreach (Worm worm in worms)
         {
-            dx = head.x - worms[i].head.x;
-            dy = head.y - worms[i].head.y;
+            if (worm == this)
+                continue;
+
+            int16 dx = (int16) this.head.x - (int16) worm.head.x;
+            int16 dy = (int16) this.head.y - (int16) worm.head.y;
             switch (direction)
             {
                 case WormDirection.UP:
                     if (dy > 0 && dy <= 3 && dx >= -1 && dx <= 1)
                         return true;
                     break;
+
                 case WormDirection.DOWN:
                     if (dy < 0 && dy >= -3 && dx >= -1 && dx <= 1)
                         return true;
                     break;
+
                 case WormDirection.LEFT:
                     if (dx > 0 && dx <= 3 && dy >= -1 && dy <= 1)
                         return true;
                     break;
+
                 case WormDirection.RIGHT:
                     if (dx < 0 && dx >= -3 && dy >= -1 && dy <= 1)
                         return true;
                     break;
+
                 default:
                     assert_not_reached ();
             }
         }
-
         return false;
     }
 
@@ -683,7 +686,7 @@ private class Worm : Object
             if (!can_move_to (board, numworms, position_move ()))
                 this_len += capacity;
 
-            if (ai_too_close (worms, numworms))
+            if (ai_too_close (worms))
                 this_len += 4;
 
             this_len += ai_deadend_after (board, worms, numworms, head, direction, length + change);


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