[iagno] Small optimisation.



commit dcf8f7c476a364398679909fb4aacc8736ae66c6
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Thu May 2 13:00:43 2019 +0200

    Small optimisation.

 src/computer-reversi.vala |  6 +++---
 src/game.vala             | 14 ++++++++++++--
 2 files changed, 15 insertions(+), 5 deletions(-)
---
diff --git a/src/computer-reversi.vala b/src/computer-reversi.vala
index f724996..64d499c 100644
--- a/src/computer-reversi.vala
+++ b/src/computer-reversi.vala
@@ -230,7 +230,7 @@ private class ComputerReversi : ComputerPlayer
             {
                 // heuristic
                 int16 h = heuristic [x, y];
-                if (g.get_owner (x, y) != g.current_color)
+                if (!g.is_current_color (x, y))
                     h = -h;
                 count += h;
 
@@ -238,7 +238,7 @@ private class ComputerReversi : ComputerPlayer
                 int16 a = (int16) g.get_empty_neighbors (x, y);
                 if (a == 0) // completely surrounded
                     a = -2;
-                count += (g.get_owner ((uint8) x, (uint8) y) == g.current_color) ? -a : a;
+                count += g.is_current_color (x, y) ? -a : a;
             }
         }
         return count;
@@ -254,7 +254,7 @@ private class ComputerReversi : ComputerPlayer
         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))
+                if (g.can_move (x, y))
                     moves.append (x * size + y);
 
         int length = (int) moves.length ();
diff --git a/src/game.vala b/src/game.vala
index d68b6b0..701eccf 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -167,6 +167,12 @@ private class GameState : Object
  //     return tiles [x, y];
  // }
 
+    internal bool is_current_color (uint8 x, uint8 y)
+        requires (is_valid_location_unsigned (x, y))
+    {
+        return tiles [x, y] == current_color;
+    }
+
     internal inline bool is_valid_location_signed (int8 x, int8 y)
     {
         return x >= 0 && x < size
@@ -246,9 +252,13 @@ private class GameState : Object
             is_complete = true;
     }
 
-    internal bool can_place (uint8 x, uint8 y, Player color)
+    internal bool can_move (uint8 x, uint8 y)
         requires (is_valid_location_unsigned (x, y))
-        requires (color != Player.NONE)
+    {
+        return can_place (x, y, current_color);
+    }
+
+    private bool can_place (uint8 x, uint8 y, Player color)
     {
         if (tiles [x, y] != Player.NONE)
             return false;


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