[gnome-nibbles/arnaudb/modernize-code] Do bonuses accounting in Boni.



commit 5de3b9c1dc54428ecefb487e44f69c8293db87ab
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat May 30 14:52:15 2020 +0200

    Do bonuses accounting in Boni.

 src/boni.vala         | 29 +++++++++++++++++++++--------
 src/nibbles-game.vala | 10 +++++-----
 2 files changed, 26 insertions(+), 13 deletions(-)
---
diff --git a/src/boni.vala b/src/boni.vala
index 1e1a678..e7b1e8f 100644
--- a/src/boni.vala
+++ b/src/boni.vala
@@ -47,9 +47,9 @@ private class Boni : Object
 {
     private Gee.LinkedList<Bonus> bonuses = new Gee.LinkedList<Bonus> ();
 
-    internal int numleft    { internal get; internal set; default = 8; }
-    internal int numboni    { internal get; private set; default = 8; }
-    private uint16 numbonuses = 0;
+    private uint8 regular_bonus_left = 0;
+    private uint8 regular_bonus_maxi = 0;
+    private uint8 total_bonus_number = 0;
 
     private const int MAX_BONUSES = 100;
 
@@ -57,7 +57,7 @@ private class Boni : Object
 
     internal bool add_bonus (int[,] board, owned Bonus bonus)
     {
-        if (numbonuses >= MAX_BONUSES)
+        if (total_bonus_number >= MAX_BONUSES)
             return false;
 
         bonuses.add (bonus);
@@ -65,7 +65,7 @@ private class Boni : Object
         board[bonus.x + 1, bonus.y    ] = bonus.bonus_type + 'A';
         board[bonus.x    , bonus.y + 1] = bonus.bonus_type + 'A';
         board[bonus.x + 1, bonus.y + 1] = bonus.bonus_type + 'A';
-        numbonuses++;
+        total_bonus_number++;
         return true;
     }
 
@@ -84,9 +84,9 @@ private class Boni : Object
     {
         bonuses.clear ();
         reset_missed ();
-        numboni = 8 + numworms;
-        numbonuses = 0;
-        numleft = numboni;
+        regular_bonus_maxi = 8 + numworms;
+        total_bonus_number = 0;
+        regular_bonus_left = regular_bonus_maxi;
     }
 
     internal Bonus? get_bonus (int[,] board, int x, int y)
@@ -129,6 +129,19 @@ private class Boni : Object
         }
     }
 
+    internal inline int new_regular_bonus_eaten ()
+    {
+        if (regular_bonus_left == 0)
+            assert_not_reached ();
+        regular_bonus_left--;
+        return regular_bonus_maxi - regular_bonus_left;
+    }
+
+    internal inline bool last_regular_bonus ()
+    {
+        return regular_bonus_left == 0;
+    }
+
     /*\
     * * missed
     \*/
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index b110480..bd941a1 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -502,9 +502,9 @@ private class NibblesGame : Object
         switch (board[worm.head.x, worm.head.y] - 'A')
         {
             case BonusType.REGULAR:
-                boni.numleft--;
-                worm.change += (boni.numboni - boni.numleft) * Worm.GROW_FACTOR;
-                worm.score += (boni.numboni - boni.numleft) * current_level;
+                int nth_bonus = boni.new_regular_bonus_eaten ();
+                worm.change += nth_bonus * Worm.GROW_FACTOR;
+                worm.score  += nth_bonus * current_level;
                 break;
             case BonusType.DOUBLE:
                 worm.score += (worm.length + worm.change) * current_level;
@@ -540,7 +540,7 @@ private class NibblesGame : Object
 
         boni.remove_bonus (board, bonus);
 
-        if (real_bonus && boni.numleft != 0)
+        if (real_bonus && !boni.last_regular_bonus ())
             add_bonus (true);
     }
 
@@ -575,7 +575,7 @@ private class NibblesGame : Object
             return GameStatus.GAMEOVER;
         }
 
-        if (boni.numleft == 0)
+        if (boni.last_regular_bonus ())
             return GameStatus.NEWROUND;
 
         return null;


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