[iagno] Allow even depth.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [iagno] Allow even depth.
- Date: Tue, 4 Jun 2019 10:14:12 +0000 (UTC)
commit cd871695ac1942fe4ad81d9858f8d44329d15140
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Thu May 30 15:07:38 2019 +0200
Allow even depth.
And so use depth 3.
src/computer-reversi.vala | 22 ++++++++++++++--------
src/game.vala | 33 +++++++++++++++++++++------------
src/iagno.vala | 2 +-
3 files changed, 36 insertions(+), 21 deletions(-)
---
diff --git a/src/computer-reversi.vala b/src/computer-reversi.vala
index c7eb9ee..75bf8fb 100644
--- a/src/computer-reversi.vala
+++ b/src/computer-reversi.vala
@@ -81,6 +81,8 @@ private class ComputerReversiEasy : ComputerReversi
private class ComputerReversiHard : ComputerReversi
{
+ public bool even_depth { private get; protected construct; }
+
construct
{
init_heuristic (size, out heuristic);
@@ -88,7 +90,7 @@ private class ComputerReversiHard : ComputerReversi
internal ComputerReversiHard (Game game, uint8 initial_depth)
{
- Object (game: game, initial_depth: initial_depth);
+ Object (game: game, even_depth: initial_depth % 2 == 0, initial_depth: initial_depth);
}
/*\
@@ -116,10 +118,10 @@ private class ComputerReversiHard : ComputerReversi
protected override int16 calculate_heuristic (GameStateStruct g)
{
- return eval_heuristic (g, ref heuristic);
+ return eval_heuristic (g, ref heuristic, even_depth);
}
- private static inline int16 eval_heuristic (GameStateStruct g, ref int16 [,] heuristic)
+ private static inline int16 eval_heuristic (GameStateStruct g, ref int16 [,] heuristic, bool even_depth)
{
uint8 size = g.size;
int16 count = 0;
@@ -127,19 +129,23 @@ private class ComputerReversiHard : ComputerReversi
{
for (uint8 y = 0; y < size; y++)
{
- bool is_current_color = g.is_current_color (x, y);
+ bool is_move_color = (even_depth && !g.is_current_color (x, y)) || g.is_opponent_color (x,
y);
// heuristic
int16 h = heuristic [x, y];
- if (!is_current_color)
- h = -h;
- count += h;
+ if (is_move_color)
+ count -= h;
+ else
+ count += h;
// around
int16 a = (int16) g.get_empty_neighbors (x, y);
if (a == 0) // completely surrounded
a = -7;
- count += 4 * (is_current_color ? -a : a);
+ if (is_move_color)
+ count += 4 * a;
+ else
+ count -= 4 * a;
}
}
return count;
diff --git a/src/game.vala b/src/game.vala
index 7a2e40e..ce58895 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -23,6 +23,7 @@
private struct GameStateStruct
{
public Player current_color;
+ public Player opponent_color;
public uint8 size;
public uint8 n_current_tiles;
@@ -54,7 +55,8 @@ private struct GameStateStruct
// requires (!game.is_complete)
{
// move color
- current_color = Player.flip_color (game.current_color);
+ opponent_color = game.current_color;
+ current_color = Player.flip_color (opponent_color);
// always given
size = game.size;
@@ -81,8 +83,8 @@ private struct GameStateStruct
internal GameStateStruct.copy_and_move (GameStateStruct game, PossibleMove move)
{
// move color
- Player move_color = game.current_color;
- current_color = Player.flip_color (move_color);
+ opponent_color = game.current_color;
+ current_color = Player.flip_color (opponent_color);
// always given
size = game.size;
@@ -90,15 +92,15 @@ private struct GameStateStruct
// tiles grid
tiles = game.tiles;
- flip_tiles (ref tiles, move.x, move.y, move_color, 0, -1, move.n_tiles_n );
- flip_tiles (ref tiles, move.x, move.y, move_color, 1, -1, move.n_tiles_ne);
- flip_tiles (ref tiles, move.x, move.y, move_color, 1, 0, move.n_tiles_e );
- flip_tiles (ref tiles, move.x, move.y, move_color, 1, 1, move.n_tiles_se);
- flip_tiles (ref tiles, move.x, move.y, move_color, 0, 1, move.n_tiles_s );
- flip_tiles (ref tiles, move.x, move.y, move_color, -1, 1, move.n_tiles_so);
- flip_tiles (ref tiles, move.x, move.y, move_color, -1, 0, move.n_tiles_o );
- flip_tiles (ref tiles, move.x, move.y, move_color, -1, -1, move.n_tiles_no);
- tiles [move.x, move.y] = move_color;
+ flip_tiles (ref tiles, move.x, move.y, opponent_color, 0, -1, move.n_tiles_n );
+ flip_tiles (ref tiles, move.x, move.y, opponent_color, 1, -1, move.n_tiles_ne);
+ flip_tiles (ref tiles, move.x, move.y, opponent_color, 1, 0, move.n_tiles_e );
+ flip_tiles (ref tiles, move.x, move.y, opponent_color, 1, 1, move.n_tiles_se);
+ flip_tiles (ref tiles, move.x, move.y, opponent_color, 0, 1, move.n_tiles_s );
+ flip_tiles (ref tiles, move.x, move.y, opponent_color, -1, 1, move.n_tiles_so);
+ flip_tiles (ref tiles, move.x, move.y, opponent_color, -1, 0, move.n_tiles_o );
+ flip_tiles (ref tiles, move.x, move.y, opponent_color, -1, -1, move.n_tiles_no);
+ tiles [move.x, move.y] = opponent_color;
// tiles counters
n_current_tiles = game.n_opponent_tiles - move.n_tiles;
@@ -125,6 +127,7 @@ private struct GameStateStruct
{
// move color
current_color = color;
+ opponent_color = Player.flip_color (color);
// always given
size = _size;
@@ -165,6 +168,12 @@ private struct GameStateStruct
return tiles [x, y] == current_color;
}
+ internal inline bool is_opponent_color (uint8 x, uint8 y)
+ // requires (is_valid_location_unsigned (x, y))
+ {
+ return tiles [x, y] == opponent_color;
+ }
+
internal inline bool is_valid_location_signed (int8 x, int8 y)
{
return x >= 0 && x < size
diff --git a/src/iagno.vala b/src/iagno.vala
index e38f080..06e1de4 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -380,7 +380,7 @@ private class Iagno : Gtk.Application
{
case 1 : computer = new ComputerReversiEasy (game); break;
case 2 : computer = new ComputerReversiHard (game, /* depth */ 2); break;
- case 3 : computer = new ComputerReversiHard (game, /* depth */ 4); break;
+ case 3 : computer = new ComputerReversiHard (game, /* depth */ 3); break;
default: assert_not_reached ();
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]