[iagno] Improve compare_move.



commit b7fa70ba494e5e63a7dc280e2dff5318e9e38f20
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sun Jun 2 20:05:38 2019 +0200

    Improve compare_move.
    
    Use things we know.

 src/computer-reversi.vala | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
---
diff --git a/src/computer-reversi.vala b/src/computer-reversi.vala
index ed7b2a1..d4864ae 100644
--- a/src/computer-reversi.vala
+++ b/src/computer-reversi.vala
@@ -106,10 +106,28 @@ private class ComputerReversiHard : ComputerReversi
      // requires (a != null)
      // requires (b != null)
     {
-        return (int) (heuristic [ ((!) b).x, ((!) b).y ]
-             -        heuristic [ ((!) a).x, ((!) a).y ])
-             + 16 * ((int) ((!) b).n_tiles
-             -       (int) ((!) a).n_tiles);
+        return calculate_move_heuristic ((!) b) - calculate_move_heuristic ((!) a);
+    }
+
+    private inline int calculate_move_heuristic (PossibleMove move)
+    {
+        int comparator = 0;
+        calculate_dir_heuristic (ref comparator, move.x, move.y,  0, -1, move.n_tiles_n );
+        calculate_dir_heuristic (ref comparator, move.x, move.y,  1, -1, move.n_tiles_ne);
+        calculate_dir_heuristic (ref comparator, move.x, move.y,  1,  0, move.n_tiles_e );
+        calculate_dir_heuristic (ref comparator, move.x, move.y,  1,  1, move.n_tiles_se);
+        calculate_dir_heuristic (ref comparator, move.x, move.y,  0,  1, move.n_tiles_s );
+        calculate_dir_heuristic (ref comparator, move.x, move.y, -1,  1, move.n_tiles_so);
+        calculate_dir_heuristic (ref comparator, move.x, move.y, -1,  0, move.n_tiles_o );
+        calculate_dir_heuristic (ref comparator, move.x, move.y, -1, -1, move.n_tiles_no);
+        return 2 * comparator + (int) heuristic [move.x, move.y];
+    }
+
+    private inline void calculate_dir_heuristic (ref int comparator, uint8 x, uint8 y, int8 x_step, int8 
y_step, uint8 count)
+    {
+        for (; count > 0; count--)
+            comparator += (int) heuristic [(int8) x + ((int8) count * x_step),
+                                           (int8) y + ((int8) count * y_step)];
     }
 
     /*\


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