[gnome-2048] Various code improvements.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-2048] Various code improvements.
- Date: Fri, 1 Feb 2019 12:10:44 +0000 (UTC)
commit 80adb04800386cc0b96d5f80231c5ae8fce732c7
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Fri Feb 1 11:49:27 2019 +0100
Various code improvements.
src/game.vala | 29 +++++++++++++++--------------
src/grid.vala | 44 ++++++++++++++++----------------------------
2 files changed, 31 insertions(+), 42 deletions(-)
---
diff --git a/src/game.vala b/src/game.vala
index 7ea6bbc..ba300e1 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -72,11 +72,10 @@ private class Game : Object
int rows = _settings.get_int ("rows");
int cols = _settings.get_int ("cols");
- _grid = new Grid (rows, cols);
- _animations_duration = (int)_settings.get_double ("animations-speed");
+ _init_grid (rows, cols, out _grid, ref _settings);
- _settings.bind ("target-value", _grid, "target-value", GLib.SettingsBindFlags.DEFAULT);
+ _animations_duration = (int)_settings.get_double ("animations-speed");
_allow_undo = _settings.get_boolean ("allow-undo");
_undo_stack_max_size = _settings.get_uint ("allow-undo-max");
@@ -86,6 +85,12 @@ private class Game : Object
_state = GameState.STOPPED;
}
+ private static void _init_grid (int rows, int cols, out Grid grid, ref GLib.Settings settings)
+ {
+ grid = new Grid (rows, cols);
+ settings.bind ("target-value", grid, "target-value", GLib.SettingsBindFlags.DEFAULT |
GLib.SettingsBindFlags.NO_SENSITIVITY);
+ }
+
/*\
* * view
\*/
@@ -231,7 +236,7 @@ private class Game : Object
_clear_foreground ();
_clear_background ();
- _grid = new Grid (rows, cols);
+ _init_grid (rows, cols, out _grid, ref _settings);
_init_background ();
@@ -340,16 +345,14 @@ private class Game : Object
private void _create_random_tile ()
{
Tile tile;
+ _grid.new_tile (out tile);
- if (_grid.new_tile (out tile))
- {
- _create_show_hide_transition (true);
+ _create_show_hide_transition (true);
- _create_tile (tile);
- _to_show.add (tile);
- _show_tile (tile.pos);
- _show_hide_trans.start ();
- }
+ _create_tile (tile);
+ _to_show.add (tile);
+ _show_tile (tile.pos);
+ _show_hide_trans.start ();
}
private void _create_tile (Tile tile)
@@ -547,8 +550,6 @@ private class Game : Object
_move_trans.remove_all ();
- _create_show_hide_transition (true);
-
foreach (TileMovement? e in _to_hide)
{
if (e == null)
diff --git a/src/grid.vala b/src/grid.vala
index a5ceada..80bf505 100644
--- a/src/grid.vala
+++ b/src/grid.vala
@@ -55,29 +55,24 @@ private class Grid : Object
_grid [i, j] = 0;
}
- internal bool new_tile (out Tile tile)
+ internal void new_tile (out Tile tile)
{
- GridPosition pos = { 0, 0 };
- uint val;
- tile = { pos, 0 };
-
if (_grid_is_full ())
- return false;
+ assert_not_reached ();
- val = 2;
-
- while (true)
- {
- pos = _random_position ();
+ GridPosition pos = { 0, 0 }; // TODO report bug: garbage init needed
+ do { _generate_random_position (rows, cols, out pos); }
+ while (_grid [pos.row, pos.col] != 0);
- if (_grid[pos.row,pos.col] == 0)
- {
- _grid[pos.row,pos.col] = val;
- _check_target_value_reached (val);
- tile = { pos, val };
- return true;
- }
- }
+ _grid [pos.row, pos.col] = 2;
+ tile = { pos, /* tile value */ 2 };
+ }
+ private static inline void _generate_random_position (int rows, int cols, out GridPosition pos)
+ requires (rows > 0)
+ requires (cols > 0)
+ {
+ pos = { Random.int_range (0, rows),
+ Random.int_range (0, cols) };
}
internal inline void move (MoveRequest request,
@@ -542,17 +537,10 @@ private class Grid : Object
return true;
}
- private GridPosition _random_position ()
- {
- GridPosition ret = { Random.int_range (0, (int)_rows),
- Random.int_range (0, (int)_cols) };
-
- return ret;
- }
-
private void _check_target_value_reached (uint val)
+ requires (target_value > 3)
{
- if (target_value != 0 && val == target_value)
+ if ( val == target_value)
target_value_reached = true;
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]