[iagno] Game: code cleanups and general improvements
- From: Michael Catanzaro <mcatanzaro src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [iagno] Game: code cleanups and general improvements
- Date: Sat, 28 Sep 2013 23:41:13 +0000 (UTC)
commit 611f8565fbdab59012f9d590c34d38fe9450835e
Author: Michael Catanzaro <mcatanzaro gnome org>
Date: Fri Sep 27 23:02:58 2013 -0500
Game: code cleanups and general improvements
src/computer-player.vala | 4 +-
src/game-view.vala | 2 +-
src/game.vala | 83 +++++++++++++++-------------------------------
src/iagno.vala | 4 +-
4 files changed, 32 insertions(+), 61 deletions(-)
---
diff --git a/src/computer-player.vala b/src/computer-player.vala
index ff90d47..7ac1150 100644
--- a/src/computer-player.vala
+++ b/src/computer-player.vala
@@ -95,7 +95,7 @@ public class ComputerPlayer : Object
private int search (Game g, Strategy strategy, int depth, int a, int b, int p, ref int move_x, ref int
move_y)
{
/* If the end of the search depth or end of the game calculate how good a result this is */
- if (depth == 0 || g.is_complete)
+ if (depth == 0 || g.is_complete ())
return calculate_heuristic (g, strategy);
/* Find all possible moves and sort from most new tiles to least new tiles */
@@ -263,7 +263,7 @@ public class ComputerPlayer : Object
{
for (var y = 0; y < 8; y++)
{
- if (game.can_place (x, y))
+ if (game.can_place (x, y, game.current_color))
moves.append (x * 8 + y);
}
}
diff --git a/src/game-view.vala b/src/game-view.vala
index 553e7fd..20d18ed 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -217,7 +217,7 @@ public class GameView : Gtk.DrawingArea
var pixmap = get_pixmap (game.get_owner (x, y));
/* If requested show the result by laying the tiles with winning color first */
- if (game.is_complete && flip_final_result && game.n_light_tiles > 0 && game.n_dark_tiles > 0)
+ if (game.is_complete () && flip_final_result && game.n_light_tiles > 0 && game.n_dark_tiles > 0)
{
var n = y * game.width + x;
var winning_color = Player.LIGHT;
diff --git a/src/game.vala b/src/game.vala
index e80b4f5..9a8b0e9 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -59,43 +59,6 @@ public class Game : Object
get { return count_tiles (Player.DARK); }
}
- public bool can_move
- {
- get
- {
- for (var x = 0; x < width; x++)
- for (var y = 0; y < height; y++)
- if (can_place (x, y))
- return true;
- return false;
- }
- }
-
- /* Game is complete if neither side can move */
- public bool is_complete
- {
- get
- {
- var save_color = current_color;
- current_color = Player.DARK;
- if (can_move)
- {
- current_color = save_color;
- return false;
- }
-
- current_color = Player.LIGHT;
- if (can_move)
- {
- current_color = save_color;
- return false;
- }
-
- current_color = save_color;
- return true;
- }
- }
-
public Game (int width = 8, int height = 8)
{
/* Setup board with four tiles by default */
@@ -155,9 +118,25 @@ public class Game : Object
return Player.NONE;
}
- public bool can_place (int x, int y)
+ /* Game is complete if neither side can move */
+ public bool is_complete ()
+ ensures (result || n_tiles < width * height)
+ {
+ return !can_move (Player.LIGHT) && !can_move (Player.DARK);
+ }
+
+ public bool can_move (Player color)
{
- return place (x, y, current_color, false) > 0;
+ for (var x = 0; x < width; x++)
+ for (var y = 0; y < height; y++)
+ if (can_place (x, y, color))
+ return true;
+ return false;
+ }
+
+ public bool can_place (int x, int y, Player color)
+ {
+ return place (x, y, color, false) > 0;
}
public int place_tile (int x, int y)
@@ -166,9 +145,9 @@ public class Game : Object
if (n_tiles == 0)
return 0;
- flip_current_color ();
+ current_color = Player.flip_color (current_color);
- if (is_complete)
+ if (is_complete ())
complete ();
else
move ();
@@ -177,10 +156,11 @@ public class Game : Object
}
public void pass ()
+ requires (!can_move (current_color))
{
undo_history[undo_index] = 0;
undo_index++;
- flip_current_color ();
+ current_color = Player.flip_color (current_color);
move ();
}
@@ -256,17 +236,15 @@ public class Game : Object
return enemy_count;
}
- public bool can_undo
+ public bool can_undo ()
{
- get { return undo_index > 0; }
+ return undo_index > 0;
}
public void undo (int count = 1)
+ requires (count == 1 || count == 2)
{
- if (!can_undo)
- return;
-
- if (count < 1)
+ if (!can_undo ())
return;
for (var i = 0; i < count; i++)
@@ -285,7 +263,7 @@ public class Game : Object
}
/* Previous player to move again */
- flip_current_color ();
+ current_color = Player.flip_color (current_color);
}
move ();
@@ -321,11 +299,4 @@ public class Game : Object
return s;
}
- private void flip_current_color ()
- {
- if (current_color == Player.LIGHT)
- current_color = Player.DARK;
- else
- current_color = Player.LIGHT;
- }
}
diff --git a/src/iagno.vala b/src/iagno.vala
index 15ab00d..8631fad 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -248,7 +248,7 @@ public class Iagno : Gtk.Application
if (light_computer != null && dark_computer != null)
undo_action.set_enabled (false);
else
- undo_action.set_enabled (game.can_undo);
+ undo_action.set_enabled (game.can_undo ());
if (was_pass)
{
@@ -346,7 +346,7 @@ public class Iagno : Gtk.Application
{
play_sound ("flip-piece");
- if (!game.can_move)
+ if (!game.can_move (game.current_color))
{
was_pass = true;
game.pass ();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]