[iagno] Calculate possible moves in GameState.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [iagno] Calculate possible moves in GameState.
- Date: Wed, 22 May 2019 13:00:52 +0000 (UTC)
commit 805b98e86a378a8e5c4eb482f2f5fe794aec1528
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Fri May 3 15:15:05 2019 +0200
Calculate possible moves in GameState.
src/computer-reversi.vala | 58 ++++++++++++++++-------------------------------
src/game.vala | 15 ++++++++++++
2 files changed, 35 insertions(+), 38 deletions(-)
---
diff --git a/src/computer-reversi.vala b/src/computer-reversi.vala
index 793d1c3..383dd04 100644
--- a/src/computer-reversi.vala
+++ b/src/computer-reversi.vala
@@ -20,22 +20,22 @@
along with GNOME Reversi. If not, see <https://www.gnu.org/licenses/>.
*/
-private class ComputerReversi : ComputerPlayer
+private struct PossibleMove
{
- private struct PossibleMove
- {
- public uint8 x;
- public uint8 y;
- public uint8 n_tiles;
+ public uint8 x;
+ public uint8 y;
+ public uint8 n_tiles;
- private PossibleMove (uint8 x, uint8 y, uint8 n_tiles)
- {
- this.x = x;
- this.y = y;
- this.n_tiles = n_tiles;
- }
+ internal PossibleMove (uint8 x, uint8 y, uint8 n_tiles)
+ {
+ this.x = x;
+ this.y = y;
+ this.n_tiles = n_tiles;
}
+}
+private class ComputerReversi : ComputerPlayer
+{
/* Game being played */
private Game game;
@@ -99,7 +99,8 @@ private class ComputerReversi : ComputerPlayer
int16 a = LESS_THAN_NEGATIVE_INFINITY;
List<PossibleMove?> moves;
- get_possible_moves_sorted (g, out moves);
+ g.get_possible_moves (out moves);
+ moves.sort (compare_move);
/* Try each move using alpha-beta pruning to optimise finding the best branch */
foreach (PossibleMove? move in moves)
@@ -139,7 +140,8 @@ private class ComputerReversi : ComputerPlayer
if (g.current_player_can_move)
{
List<PossibleMove?> moves;
- get_possible_moves_sorted (g, out moves);
+ g.get_possible_moves (out moves);
+ moves.sort (compare_move);
/* Try each move using alpha-beta pruning to optimise finding the best branch */
foreach (PossibleMove? move in moves)
@@ -170,34 +172,14 @@ private class ComputerReversi : ComputerPlayer
return a;
}
- private static void get_possible_moves_sorted (GameState g, out List<PossibleMove?> moves)
- {
- uint8 size = g.size;
- moves = new List<PossibleMove?> ();
-
- for (uint8 x = 0; x < size; x++)
- {
- for (uint8 y = 0; y < size; y++)
- {
- uint8 n_tiles = g.test_placing_tile (x, y);
- if (n_tiles == 0)
- continue;
-
- PossibleMove move = PossibleMove (x, y, n_tiles);
- // the g_list_insert_sorted() documentation says: "if you are adding many new elements to
- // a list, and the number of new elements is much larger than the length of the list, use
- // g_list_prepend() to add the new items and sort the list afterwards with g_list_sort()"
- // but the perfs tests on complete games disagree; so let's keep things like that for now
- moves.insert_sorted (move, compare_move);
- }
- }
- }
-
private static int compare_move (PossibleMove? a, PossibleMove? b)
requires (a != null)
requires (b != null)
{
- return ((!) b).n_tiles - ((!) a).n_tiles;
+ if (((!) a).n_tiles >= ((!) b).n_tiles)
+ return -1;
+ else
+ return 1;
}
/*\
diff --git a/src/game.vala b/src/game.vala
index 4e3d230..c534f8a 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -172,6 +172,21 @@ private class GameState : Object
* * ... // completeness
\*/
+ internal void get_possible_moves (out List<PossibleMove?> moves)
+ {
+ moves = new List<PossibleMove?> ();
+
+ for (uint8 x = 0; x < size; x++)
+ {
+ for (uint8 y = 0; y < size; y++)
+ {
+ uint8 n_tiles = place_tile (x, y, current_color, /* apply move */ false);
+ if (n_tiles != 0)
+ moves.prepend (PossibleMove (x, y, n_tiles));
+ }
+ }
+ }
+
internal uint8 test_placing_tile (uint8 x, uint8 y)
{
return place_tile (x, y, current_color, /* apply move */ false);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]