[gnome-nibbles/wip/vala] Replace head () method by property



commit 675c6a043f6c60a7ccf4447e2a9b44e507a5e35e
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Mon Aug 17 22:55:17 2015 +0300

    Replace head () method by property

 src/nibbles-game.vala |    6 ++--
 src/nibbles-view.vala |    4 +-
 src/worm.vala         |   86 ++++++++++++++++++++++++++++---------------------
 3 files changed, 54 insertions(+), 42 deletions(-)
---
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index e16309b..0d252ad 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -415,7 +415,7 @@ public class NibblesGame : Object
             return;
         }
 
-        switch (walls[worm.head ().x, worm.head ().y] - 'A')
+        switch (walls[worm.head.x, worm.head.y] - 'A')
         {
             case BonusType.REGULAR:
                 boni.numleft--;
@@ -452,13 +452,13 @@ public class NibblesGame : Object
 
     public void bonus_found_cb (Worm worm)
     {
-        var bonus = boni.get_bonus (walls, worm.head ().x, worm.head ().y);
+        var bonus = boni.get_bonus (walls, worm.head.x, worm.head.y);
         if (bonus == null)
             return;
         apply_bonus (bonus, worm);
         bonus_applied (worm);
 
-        if (walls[worm.head ().x, worm.head ().y] == BonusType.REGULAR + 'A'
+        if (walls[worm.head.x, worm.head.y] == BonusType.REGULAR + 'A'
             && !bonus.fake)
         {
             // FIXME: 2/3
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index f74363f..3de0902 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -388,7 +388,7 @@ public class NibblesView : GtkClutter.Embed
             else if (worm.direction == WormDirection.LEFT || worm.direction == WormDirection.RIGHT)
             {
                 label.set_x (worm.list[middle].x * game.tile_size - label.width / 2 + game.tile_size / 2);
-                label.set_y (worm.head ().y * game.tile_size - 3 * game.tile_size);
+                label.set_y (worm.head.y * game.tile_size - 3 * game.tile_size);
             }
             name_labels.add (label);
         }
@@ -444,7 +444,7 @@ public class NibblesView : GtkClutter.Embed
             else if (worm.direction == WormDirection.LEFT || worm.direction == WormDirection.RIGHT)
             {
                 actor.set_x (worm.list[middle].x * tile_size - actor.width / 2 + tile_size / 2);
-                actor.set_y (worm.head ().y * tile_size - 3 * tile_size);
+                actor.set_y (worm.head.y * tile_size - 3 * tile_size);
             }
         }
     }
diff --git a/src/worm.vala b/src/worm.vala
index 375aeaf..ffe6d6a 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -43,6 +43,16 @@ public class Worm : Object
         set {}
     }
 
+    public Position head
+    {
+        get
+        {
+            Position head = list.first ();
+            return head;
+        }
+        set {}
+    }
+
     public WormDirection direction;
 
     public WormDirection starting_direction;
@@ -70,11 +80,6 @@ public class Worm : Object
         key_queue = new Gee.ArrayQueue<WormDirection> ();
     }
 
-    public Position head ()
-    {
-        return list.first ();
-    }
-
     public void set_start (int xhead, int yhead, WormDirection direction)
     {
         list.clear ();
@@ -97,26 +102,26 @@ public class Worm : Object
         if (is_human)
             keypress = false;
 
-        var position = head ();
+        var position = head;
         switch (direction)
         {
             case WormDirection.UP:
-                position.y = --head ().y;
+                position.y = --head.y;
                 if (position.y < 0)
                     position.y = NibblesGame.HEIGHT - 1;
                 break;
             case WormDirection.DOWN:
-                position.y = ++head ().y;
+                position.y = ++head.y;
                 if (position.y >= NibblesGame.HEIGHT)
                     position.y = 0;
                 break;
             case WormDirection.LEFT:
-                position.x = --head ().x;
+                position.x = --head.x;
                 if (position.x < 0)
                     position.x = NibblesGame.WIDTH - 1;
                 break;
             case WormDirection.RIGHT:
-                position.x = ++head ().x;
+                position.x = ++head.x;
                 if (position.x >= NibblesGame.WIDTH)
                     position.x = 0;
                 break;
@@ -140,11 +145,11 @@ public class Worm : Object
         }
 
         /* Check for bonus before changing tile */
-        if (walls[head ().x, head ().y] != NibblesGame.EMPTYCHAR)
+        if (walls[head.x, head.y] != NibblesGame.EMPTYCHAR)
             bonus_found ();
 
         /* Mark the tile as occupied by the worm's body */
-        walls[head ().x, head ().y] = NibblesGame.WORMCHAR + id;
+        walls[head.x, head.y] = NibblesGame.WORMCHAR + id;
 
         if (!key_queue.is_empty)
             dequeue_keypress ();
@@ -244,27 +249,27 @@ public class Worm : Object
 
     private Position position_move ()
     {
-        Position position = head ();
+        Position position = head;
 
         switch (direction)
         {
             case WormDirection.UP:
-                position.y = --head ().y;
+                position.y = --head.y;
                 if (position.y < 0)
                     position.y = NibblesGame.HEIGHT - 1;
                 break;
             case WormDirection.DOWN:
-                position.y = ++head ().y;
+                position.y = ++head.y;
                 if (position.y >= NibblesGame.HEIGHT)
                     position.y = 0;
                 break;
             case WormDirection.LEFT:
-                position.x = --head ().x;
+                position.x = --head.x;
                 if (position.x < 0)
                     position.x = NibblesGame.WIDTH - 1;
                 break;
             case WormDirection.RIGHT:
-                position.x = ++head ().x;
+                position.x = ++head.x;
                 if (position.x >= NibblesGame.WIDTH)
                     position.x = 0;
                 break;
@@ -295,6 +300,21 @@ public class Worm : Object
         keypress = true;
     }
 
+    /*\
+    * * Keys and key presses
+    \*/
+    private uint upper_key (uint keyval)
+    {
+        if (keyval > 255)
+            return keyval;
+        return ((char) keyval).toupper ();
+    }
+
+    public void handle_direction (WormDirection dir)
+    {
+        direction_set (dir);
+    }
+
     public bool handle_keypress (uint keyval, Gee.HashMap<Worm, WormProperties?> worm_props)
     {
         WormProperties properties;
@@ -334,18 +354,6 @@ public class Worm : Object
         return false;
     }
 
-    private uint upper_key (uint keyval)
-    {
-        if (keyval > 255)
-            return keyval;
-        return ((char) keyval).toupper ();
-    }
-
-    public void handle_direction (WormDirection dir)
-    {
-        direction_set (dir);
-    }
-
     public void queue_keypress (WormDirection dir)
     {
         /* Ignore duplicates in normal movement mode. This resolves the key
@@ -363,6 +371,10 @@ public class Worm : Object
         direction_set (key_queue.poll ());
     }
 
+    /*\
+    * * AI
+    \*/
+
     /* Check whether the worm will be trapped in a dead end. A location
      * within the dead end and the length of the worm is given. This
      * prevents worms getting trapped in a spiral, or in a corner sharper
@@ -465,8 +477,8 @@ public class Worm : Object
         i = numworms;
         while (i-- > 0)
         {
-            cx = worms[i].head ().x;
-            cy = worms[i].head ().y;
+            cx = worms[i].head.x;
+            cy = worms[i].head.y;
             if (cx != x || cy != y) {
                 if (cx > 0)
                     deadend_board[cx-1, cy] = deadend_runnumber;
@@ -526,8 +538,8 @@ public class Worm : Object
 
         while (i-- > 0)
         {
-            dx = head ().x - worms[i].head ().x;
-            dy = head ().y - worms[i].head ().y;
+            dx = head.x - worms[i].head.x;
+            dy = head.y - worms[i].head.y;
             switch (direction)
             {
                 case WormDirection.UP:
@@ -617,9 +629,9 @@ public class Worm : Object
     {
         var opposite = (direction + 1) % 4 + 1;
 
-        var front = Worm.ai_wander (walls, numworms, head ().x, head ().y, direction, head ().x, head ().y);
-        var left = Worm.ai_wander (walls, numworms, head ().x, head ().y, direction - 1, head ().x, head 
().y);
-        var right = Worm.ai_wander (walls, numworms, head ().x, head ().y, direction + 1, head ().x, head 
().y);
+        var front = Worm.ai_wander (walls, numworms, head.x, head.y, direction, head.x, head.y);
+        var left = Worm.ai_wander (walls, numworms, head.x, head.y, direction - 1, head.x, head.y);
+        var right = Worm.ai_wander (walls, numworms, head.x, head.y, direction + 1, head.x, head.y);
 
         int dir;
         if (!front)
@@ -691,7 +703,7 @@ public class Worm : Object
             if (ai_too_close (worms, numworms))
                 this_len += 4;
 
-            this_len += ai_deadend_after (walls, worms, numworms, head ().x, head ().y, dir, length + 
change);
+            this_len += ai_deadend_after (walls, worms, numworms, head.x, head.y, dir, length + change);
 
             if (dir == old_dir && this_len <= 0)
                 this_len -= 100;


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