Commit 248a437a authored by Arnaud B.'s avatar Arnaud B.

Move score management in Scorebox.

parent 239fc76b
...@@ -49,12 +49,6 @@ private class FourInARow : Gtk.Application ...@@ -49,12 +49,6 @@ private class FourInARow : Gtk.Application
private Board game_board; private Board game_board;
private bool one_player_game; private bool one_player_game;
private Difficulty ai_level; private Difficulty ai_level;
/**
* score:
*
* The scores for the current instance (Human, Opponent, Draw)
*/
private uint [] score = { 0, 0, 0 };
private uint playgame_timeout = 0; private uint playgame_timeout = 0;
// widgets // widgets
...@@ -289,10 +283,8 @@ private class FourInARow : Gtk.Application ...@@ -289,10 +283,8 @@ private class FourInARow : Gtk.Application
history_button_2); history_button_2);
scorebox = new Scorebox (window, this, theme_manager); scorebox = new Scorebox (window, this, theme_manager);
settings.changed ["theme-id"].connect (() => {
scorebox.update (score, one_player_game); settings.changed ["theme-id"].connect (prompt_player);
prompt_player ();
});
add_actions (); add_actions ();
...@@ -421,8 +413,7 @@ private class FourInARow : Gtk.Application ...@@ -421,8 +413,7 @@ private class FourInARow : Gtk.Application
if (reload_settings) if (reload_settings)
{ {
one_player_game = settings.get_int ("num-players") == 1; one_player_game = settings.get_int ("num-players") == 1;
score = { 0, 0, 0 }; scorebox.new_match (one_player_game);
scorebox.update (score, one_player_game);
if (one_player_game) if (one_player_game)
{ {
...@@ -539,7 +530,7 @@ private class FourInARow : Gtk.Application ...@@ -539,7 +530,7 @@ private class FourInARow : Gtk.Application
bool human = is_player_human (); bool human = is_player_human ();
window.allow_hint (human && !gameover); window.allow_hint (human && !gameover);
update_round_section (); update_round_section (/* menu init */ false);
if (one_player_game) if (one_player_game)
window.allow_undo ((human && moves > 1) || (!human && gameover)); window.allow_undo ((human && moves > 1) || (!human && gameover));
...@@ -548,7 +539,7 @@ private class FourInARow : Gtk.Application ...@@ -548,7 +539,7 @@ private class FourInARow : Gtk.Application
if (gameover && winner == Player.NOBODY) if (gameover && winner == Player.NOBODY)
{ {
if (score [Player.NOBODY] == 0) if (scorebox.is_first_game ())
set_status_message (null); set_status_message (null);
else else
/* Translators: text displayed on game end in the headerbar/actionbar, if the game is a tie */ /* Translators: text displayed on game end in the headerbar/actionbar, if the game is a tie */
...@@ -609,8 +600,7 @@ private class FourInARow : Gtk.Application ...@@ -609,8 +600,7 @@ private class FourInARow : Gtk.Application
if (gameover) if (gameover)
{ {
score [winner]++; scorebox.win (winner);
scorebox.update (score, one_player_game);
prompt_player (); prompt_player ();
if (winner != Player.NOBODY) if (winner != Player.NOBODY)
blink_winner (3); blink_winner (3);
...@@ -911,8 +901,7 @@ private class FourInARow : Gtk.Application ...@@ -911,8 +901,7 @@ private class FourInARow : Gtk.Application
if (gameover) if (gameover)
{ {
score [winner]--; scorebox.unwin ();
scorebox.update (score, one_player_game);
set_gameover (false); set_gameover (false);
prompt_player (); prompt_player ();
} }
...@@ -1231,11 +1220,11 @@ private class FourInARow : Gtk.Application ...@@ -1231,11 +1220,11 @@ private class FourInARow : Gtk.Application
section.freeze (); section.freeze ();
game_menu.append_section (null, section); game_menu.append_section (null, section);
update_round_section (); update_round_section (/* menu init */ true);
game_menu.append_section (null, round_section); game_menu.append_section (null, round_section);
} }
private void update_round_section () private void update_round_section (bool menu_init)
{ {
round_section.remove_all (); round_section.remove_all ();
if (gameover) if (gameover)
...@@ -1243,7 +1232,7 @@ private class FourInARow : Gtk.Application ...@@ -1243,7 +1232,7 @@ private class FourInARow : Gtk.Application
else else
round_section.append (_("_Give Up"), "app.give-up"); round_section.append (_("_Give Up"), "app.give-up");
if (score [Player.HUMAN] + score [Player.OPPONENT] + score [Player.NOBODY] == 0) if (menu_init || scorebox.is_first_game ())
return; return;
/* Translators: hamburger menu entry; opens the Scores dialog (with a mnemonic that appears pressing Alt) */ /* Translators: hamburger menu entry; opens the Scores dialog (with a mnemonic that appears pressing Alt) */
round_section.append (_("_Scores"), "app.scores"); round_section.append (_("_Scores"), "app.scores");
...@@ -1251,14 +1240,7 @@ private class FourInARow : Gtk.Application ...@@ -1251,14 +1240,7 @@ private class FourInARow : Gtk.Application
private inline void on_give_up (/* SimpleAction action, Variant? parameter */) private inline void on_give_up (/* SimpleAction action, Variant? parameter */)
{ {
if (player == Player.HUMAN) scorebox.give_up (player);
score [Player.OPPONENT]++;
else if (player == Player.OPPONENT)
score [Player.HUMAN]++;
else
assert_not_reached ();
scorebox.update (score, one_player_game);
game_reset (/* reload settings */ false); game_reset (/* reload settings */ false);
} }
......
...@@ -79,14 +79,11 @@ private class Scorebox : Dialog ...@@ -79,14 +79,11 @@ private class Scorebox : Dialog
grid.show_all (); grid.show_all ();
get_content_area ().pack_start (grid); get_content_area ().pack_start (grid);
theme_manager.theme_changed.connect (update);
} }
/** private void update ()
* update:
*
* updates the scorebox with the latest scores
*/
internal void update (uint [] scores, bool one_player_game)
{ {
if (one_player_game) if (one_player_game)
{ {
...@@ -140,4 +137,52 @@ private class Scorebox : Dialog ...@@ -140,4 +137,52 @@ private class Scorebox : Dialog
hide (); hide ();
return true; return true;
} }
/*\
* * score management
\*/
private bool one_player_game = false;
private Player last_winner = Player.NOBODY;
private uint [] scores = { /* human */ 0, /* opponent */ 0, /* draw */ 0 };
internal void new_match (bool _one_player_game)
{
scores = { 0, 0, 0 };
last_winner = Player.NOBODY;
one_player_game = _one_player_game;
update ();
}
internal void give_up (Player player)
{
if (player == Player.HUMAN)
scores [Player.OPPONENT]++;
else if (player == Player.OPPONENT)
scores [Player.HUMAN]++;
else
assert_not_reached ();
update ();
}
internal void win (Player player)
{
scores [player]++;
last_winner = player;
update ();
}
internal void unwin ()
{
if (last_winner == Player.NOBODY)
assert_not_reached ();
scores [last_winner]--;
last_winner = Player.NOBODY;
update ();
}
internal bool is_first_game ()
{
return scores [Player.HUMAN] + scores [Player.OPPONENT] + scores [Player.NOBODY] == 0;
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment