[gnome-2048] Calculate score on the fly.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-2048] Calculate score on the fly.
- Date: Thu, 7 Feb 2019 11:15:37 +0000 (UTC)
commit 20440d93beb56e8bcf182de22aceabdfaa9db7de
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Wed Feb 6 15:05:10 2019 +0100
Calculate score on the fly.
At least when loading or undoing.
src/application.vala | 2 +-
src/game.vala | 40 +++++++---------------------------------
src/grid.vala | 25 +++++++++++++++++++++++++
3 files changed, 33 insertions(+), 34 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index b59e81d..d0f3fd0 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -168,7 +168,7 @@ private class Application : Gtk.Application
private void _init_game ()
{
_game = new Game (ref _settings);
- _game.notify["score"].connect ((s, p) => {
+ _game.notify ["score"].connect ((s, p) => {
_score.label = _game.score.to_string ();
});
_game.finished.connect ((s) => {
diff --git a/src/game.vala b/src/game.vala
index 4cc0706..b9e52ab 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -99,7 +99,7 @@ private class Game : Object
* * others
\*/
- internal uint score { internal get; private set; default = 0; }
+ internal long score { internal get; private set; default = 0; }
internal void new_game (ref GLib.Settings settings)
{
@@ -132,11 +132,7 @@ private class Game : Object
internal void save_game ()
{
- string contents = "";
-
- contents += _grid.save ();
- contents += _score.to_string () + "\n";
-
+ string contents = _grid.save ();
try {
DirUtils.create_with_parents (Path.get_dirname (_saved_path), 0775);
FileUtils.set_contents (_saved_path, contents);
@@ -161,16 +157,7 @@ private class Game : Object
return false;
}
- string [] lines = contents.split ("\n");
- string last_line = lines [lines.length - 1]; // not UNIX?
- if (last_line == "")
- last_line = lines [lines.length - 2];
- uint64 score_64;
- if (!uint64.try_parse (last_line, out score_64))
- return false;
- if (score_64 > (uint64) uint.MAX)
- return false;
- score = (uint) score_64;
+ score = _grid.get_score ();
if (_background_init_done)
_clear_background ();
@@ -524,17 +511,16 @@ private class Game : Object
_dim_tile (((!) e).from);
}
- uint delta_score = 0;
+ long delta_score = 0; // do not notify["score"] multiple times
foreach (Tile? e in _to_show)
{
if (e == null)
assert_not_reached ();
_create_tile ((!) e);
_show_tile (((!) e).pos);
- delta_score += (uint) Math.pow (2, ((!) e).val);
+ delta_score += (long) Math.pow (2, ((!) e).val);
}
score += delta_score;
- _store_score_update (delta_score);
_create_random_tile ();
}
@@ -662,8 +648,7 @@ private class Game : Object
private bool _allow_undo = false;
private uint _undo_stack_max_size;
- private Gee.LinkedList<Grid> _undo_stack = new Gee.LinkedList<Grid> ();
- private Gee.LinkedList<uint> _undo_score_stack = new Gee.LinkedList<uint> ();
+ private Gee.LinkedList<Grid> _undo_stack = new Gee.LinkedList<Grid> ();
internal void undo ()
requires (_allow_undo == true)
@@ -674,7 +659,7 @@ private class Game : Object
_clear_foreground ();
_grid = _undo_stack.poll_head ();
_restore_foreground (false);
- score -= _undo_score_stack.poll_head ();
+ score = _grid.get_score ();
if (_undo_stack.size == 0)
undo_disabled ();
@@ -696,7 +681,6 @@ private class Game : Object
private void _clear_history ()
{
_undo_stack.clear ();
- _undo_score_stack.clear ();
}
private void _store_movement (Grid clone)
@@ -710,14 +694,4 @@ private class Game : Object
if (_undo_stack.size == 1)
undo_enabled ();
}
-
- private void _store_score_update (uint delta_score)
- {
- if (!_allow_undo)
- return;
-
- if (_undo_score_stack.size >= _undo_stack_max_size)
- _undo_score_stack.poll_tail ();
- _undo_score_stack.offer_head (delta_score);
- }
}
diff --git a/src/grid.vala b/src/grid.vala
index 44602a7..ad38e14 100644
--- a/src/grid.vala
+++ b/src/grid.vala
@@ -430,6 +430,29 @@ private class Grid : Object
grid [i, j] = 0;
}
+ internal long get_score ()
+ {
+ return _get_score (ref _grid);
+ }
+ private static long _get_score (ref uint8 [,] grid)
+ {
+ long score = 0;
+
+ uint rows = grid.length [0];
+ uint cols = grid.length [1];
+
+ for (uint i = 0; i < rows; i++)
+ for (uint j = 0; j < cols; j++)
+ score += _calculate_score_value (grid [i, j]);
+ return score;
+ }
+ private static inline long _calculate_score_value (uint8 tile_value)
+ {
+ if (tile_value < 2)
+ return 0;
+ return (long) (Math.pow (2, tile_value) * (tile_value - 1));
+ }
+
/*\
* * getting values
\*/
@@ -460,6 +483,8 @@ private class Grid : Object
{
string ret_string = @"$_rows $_cols\n";
_convert_to_string (ref _grid, ref ret_string);
+ ret_string += _get_score (ref _grid).to_string (); // historical, not
+ ret_string += "\n"; // used when loading
return ret_string;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]