[iagno] Don't make unnecessary moves.
- From: Michael Catanzaro <mcatanzaro src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [iagno] Don't make unnecessary moves.
- Date: Wed, 1 Oct 2014 15:02:10 +0000 (UTC)
commit f07ee51943170362c2780874638adf7920592c9a
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Sat Sep 27 08:09:46 2014 +0200
Don't make unnecessary moves.
https://bugzilla.gnome.org/show_bug.cgi?id=737410
src/computer-player.vala | 3 +--
src/game.vala | 36 +++++++++++++++++++++---------------
2 files changed, 22 insertions(+), 17 deletions(-)
---
diff --git a/src/computer-player.vala b/src/computer-player.vala
index 201572e..c3a98fa 100644
--- a/src/computer-player.vala
+++ b/src/computer-player.vala
@@ -204,13 +204,12 @@ public class ComputerPlayer : Object
{
for (var y = 0; y < g.size; y++)
{
- var n_tiles = g.place_tile (x, y);
+ var n_tiles = g.place_tile (x, y, false);
if (n_tiles <= 0)
continue;
var move = PossibleMove (x, y, n_tiles);
moves.insert_sorted (move, compare_move);
- g.undo ();
}
}
diff --git a/src/game.vala b/src/game.vala
index b9fb156..cb56f1f 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -195,27 +195,30 @@ public class Game : Object
* * Actions (apart undo)
\*/
- public int place_tile (int x, int y)
+ public int place_tile (int x, int y, bool apply = true)
requires (is_valid_location (x, y))
{
if (tiles[x, y] != Player.NONE)
return 0;
var tiles_turned = 0;
- tiles_turned += flip_tiles (x, y, 1, 0);
- tiles_turned += flip_tiles (x, y, 1, 1);
- tiles_turned += flip_tiles (x, y, 0, 1);
- tiles_turned += flip_tiles (x, y, -1, 1);
- tiles_turned += flip_tiles (x, y, -1, 0);
- tiles_turned += flip_tiles (x, y, -1, -1);
- tiles_turned += flip_tiles (x, y, 0, -1);
- tiles_turned += flip_tiles (x, y, 1, -1);
+ tiles_turned += flip_tiles (x, y, 1, 0, apply);
+ tiles_turned += flip_tiles (x, y, 1, 1, apply);
+ tiles_turned += flip_tiles (x, y, 0, 1, apply);
+ tiles_turned += flip_tiles (x, y, -1, 1, apply);
+ tiles_turned += flip_tiles (x, y, -1, 0, apply);
+ tiles_turned += flip_tiles (x, y, -1, -1, apply);
+ tiles_turned += flip_tiles (x, y, 0, -1, apply);
+ tiles_turned += flip_tiles (x, y, 1, -1, apply);
if (tiles_turned == 0)
return 0;
- set_tile (x, y);
- end_of_turn ();
+ if (apply)
+ {
+ set_tile (x, y);
+ end_of_turn ();
+ }
return tiles_turned;
}
@@ -263,16 +266,19 @@ public class Game : Object
* * Flipping tiles
\*/
- private int flip_tiles (int x, int y, int x_step, int y_step)
+ private int flip_tiles (int x, int y, int x_step, int y_step, bool apply)
{
var enemy_count = can_flip_tiles (x, y, x_step, y_step, current_color);
if (enemy_count == 0)
return 0;
- for (var i = 1; i <= enemy_count; i++)
+ if (apply)
{
- n_opponent_tiles--;
- set_tile (x + i * x_step, y + i * y_step);
+ for (var i = 1; i <= enemy_count; i++)
+ {
+ n_opponent_tiles--;
+ set_tile (x + i * x_step, y + i * y_step);
+ }
}
return enemy_count;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]