[four-in-a-row] Fusion two enums.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [four-in-a-row] Fusion two enums.
- Date: Thu, 26 Dec 2019 17:12:04 +0000 (UTC)
commit 655292c9b647178d61fd05f9e29d30f9939df2aa
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Mon Dec 23 09:17:02 2019 +0100
Fusion two enums.
src/ai.vala | 47 ++++++++++++-----------
src/four-in-a-row.vala | 84 +++++++++++++++++++----------------------
src/game-board-view.vala | 16 ++++----
src/game-board.vala | 38 +++++++++----------
src/meson.build | 2 +
src/player.vala | 26 +++++++++++++
src/scorebox.vala | 98 ++++++++++++++++++++++++++----------------------
src/theme.vala | 12 +++---
8 files changed, 176 insertions(+), 147 deletions(-)
---
diff --git a/src/ai.vala b/src/ai.vala
index d61eb4b..6a07354 100644
--- a/src/ai.vala
+++ b/src/ai.vala
@@ -18,7 +18,6 @@
with GNOME Four-in-a-row. If not, see <https://www.gnu.org/licenses/>.
*/
-private enum Player { NONE, HUMAN, AI; }
private enum Difficulty { EASY, MEDIUM, HARD; }
private int playgame(string moves_until_now)
@@ -42,8 +41,8 @@ private class DecisionTree
private Player[,] board = new Player [BOARD_ROWS, BOARD_COLUMNS];
/* plies - depth of the DecisionTree */
private int plies = 8;
- /* last_moving_player - The player who made the last move, set to Player.NONE if no one has made a move
yet */
- private Player last_moving_player = Player.NONE;
+ /* last_moving_player - The player who made the last move, set to Player.NOBODY if no one has made a
move yet */
+ private Player last_moving_player = Player.NOBODY;
/* next_move_in_column - stores the column number in which next move is to be made */
private int next_move_in_column = -1;
/* stores the difficulty level of the game */
@@ -54,7 +53,7 @@ private class DecisionTree
{
for (int i = 0; i < BOARD_ROWS; i++)
for (int j = 0; j < BOARD_COLUMNS; j++)
- board [i, j] = Player.NONE;
+ board [i, j] = Player.NOBODY;
}
/* utility function for debugging purposes, prints a snapshot of current status of the board */
@@ -78,7 +77,7 @@ private class DecisionTree
{
if (last_moving_player == Player.HUMAN)
return heurist();
- else if (last_moving_player == Player.AI)
+ else if (last_moving_player == Player.OPPONENT)
return -1 * heurist();
else
return 0;
@@ -130,7 +129,7 @@ private class DecisionTree
{
/* find the cell on which the last move was made */
int row;
- for (row = 0; row < BOARD_ROWS && board [row, column] == Player.NONE; row++);
+ for (row = 0; row < BOARD_ROWS && board [row, column] == Player.NOBODY; row++);
return vertical_win(row, column) ||
horizontal_win(row, column) ||
@@ -181,7 +180,7 @@ private class DecisionTree
private bool board_full()
{
for (int i = 0 ; i < BOARD_COLUMNS ; i++)
- if (board [0, i] == Player.NONE)
+ if (board [0, i] == Player.NOBODY)
return false;
return true;
}
@@ -191,13 +190,13 @@ private class DecisionTree
{
/* find the cell on which to move */
int row;
- for (row = BOARD_ROWS - 1; row >= 0 && board [row, column] != Player.NONE; row--);
+ for (row = BOARD_ROWS - 1; row >= 0 && board [row, column] != Player.NOBODY; row--);
if (row < 0)
return false;
/* don't forget AI could make the first move */
- Player player = last_moving_player != Player.AI ? Player.AI : Player.HUMAN;
+ Player player = last_moving_player != Player.OPPONENT ? Player.OPPONENT : Player.HUMAN;
board [row, column] = player;
last_moving_player = player;
@@ -206,15 +205,15 @@ private class DecisionTree
/* unmove the last move made in the column'th column */
private void unmove(int column)
- requires(last_moving_player != Player.NONE)
+ requires(last_moving_player != Player.NOBODY)
{
/* find the cell on which the last move was made */
int row;
- for (row = 0; row < BOARD_ROWS && board [row, column] == Player.NONE; row++);
+ for (row = 0; row < BOARD_ROWS && board [row, column] == Player.NOBODY; row++);
- board [row, column] = Player.NONE;
+ board [row, column] = Player.NOBODY;
- last_moving_player = last_moving_player == Player.AI ? Player.HUMAN : Player.AI;
+ last_moving_player = last_moving_player == Player.OPPONENT ? Player.HUMAN : Player.OPPONENT;
}
/* vstr is the sequence of moves made until now. We update DecisionTree::board to reflect these sequence
of moves. */
@@ -225,7 +224,7 @@ private class DecisionTree
/* AI will make the first move, nothing to add to the board */
if (vstr.length == 2) return;
- Player move = vstr.length % 2 == 0 ? Player.AI : Player.HUMAN;
+ Player move = vstr.length % 2 == 0 ? Player.OPPONENT : Player.HUMAN;
for (int i = 1; i < vstr.length - 1; i++)
{
@@ -233,23 +232,23 @@ private class DecisionTree
/* find the cell on which the move is made */
int row;
- for (row = BOARD_ROWS - 1; row >= 0 && board [row, column] != Player.NONE; row--);
+ for (row = BOARD_ROWS - 1; row >= 0 && board [row, column] != Player.NOBODY; row--);
board [row, column] = move;
- move = move == Player.HUMAN ? Player.AI : Player.HUMAN;
+ move = move == Player.HUMAN ? Player.OPPONENT : Player.HUMAN;
}
last_moving_player = Player.HUMAN;
}
- /* Check for immediate win of AI/HUMAN. It checks the current state of the board. Returns -1 if no
immediate win for Player P.
+ /* Check for immediate win of HUMAN or OPPONENT. It checks the current state of the board. Returns -1 if
no immediate win for Player P.
Otherwise returns the column number in which Player P should move to win. */
private int immediate_win(Player p)
{
Player old_last_moving_player = last_moving_player;
- last_moving_player = p == Player.AI ? Player.HUMAN : Player.AI;
+ last_moving_player = p == Player.OPPONENT ? Player.HUMAN : Player.OPPONENT;
bool player_wins = false;
int i;
@@ -282,7 +281,7 @@ private class DecisionTree
/* if AI can win by making a move immediately, make that move;
main.c has indexing beginning from 1 instead of 0, hence, we add 1 */
- int temp = immediate_win(Player.AI);
+ int temp = immediate_win(Player.OPPONENT);
if (temp != -1)
return temp + 1;
@@ -322,7 +321,7 @@ private class DecisionTree
private int heurist_hard()
{
- int count = count_3_in_a_row(Player.AI) - count_3_in_a_row(Player.HUMAN);
+ int count = count_3_in_a_row(Player.OPPONENT) - count_3_in_a_row(Player.HUMAN);
return count == 0 ? (int) Random.int_range(1, 49) : count * 100;
}
@@ -340,7 +339,7 @@ private class DecisionTree
{
for (int i = 0; i < BOARD_ROWS; i++)
{
- if (board [i, j] != Player.NONE)
+ if (board [i, j] != Player.NOBODY)
break;
if (all_adjacent_empty(i, j))
@@ -351,7 +350,7 @@ private class DecisionTree
if (victory(j))
count++;
- board [i, j] = Player.NONE;
+ board [i, j] = Player.NOBODY;
}
}
last_moving_player = old_last_moving_player;
@@ -367,7 +366,7 @@ private class DecisionTree
{
if (k == 0 && l == 0)
continue;
- if (i + k >= 0 && i + k < BOARD_ROWS && j + l >= 0 && j + l < BOARD_COLUMNS && board [i + k,
j + l] != Player.NONE)
+ if (i + k >= 0 && i + k < BOARD_ROWS && j + l >= 0 && j + l < BOARD_COLUMNS && board [i + k,
j + l] != Player.NOBODY)
return false;
}
}
@@ -401,7 +400,7 @@ private class DecisionTree
set_level(vstr);
update_board(vstr);
- int temp = immediate_win(Player.AI);
+ int temp = immediate_win(Player.OPPONENT);
if (temp != -1)
return 1000;
diff --git a/src/four-in-a-row.vala b/src/four-in-a-row.vala
index 24abb40..85b482d 100644
--- a/src/four-in-a-row.vala
+++ b/src/four-in-a-row.vala
@@ -52,9 +52,9 @@ private class FourInARow : Gtk.Application
// game status
private bool gameover = true;
- private PlayerID player = PlayerID.NOBODY;
- private PlayerID winner = PlayerID.NOBODY;
- private PlayerID last_first_player = PlayerID.NOBODY;
+ private Player player = Player.NOBODY;
+ private Player winner = Player.NOBODY;
+ private Player last_first_player = Player.NOBODY;
private Board game_board = new Board ();
private bool one_player_game;
private uint8 ai_level;
@@ -86,7 +86,7 @@ private class FourInARow : Gtk.Application
private static AnimID anim = AnimID.NONE;
private uint8 [,] blink_lines = {{}};
private uint8 blink_line = 0; // index of currenly blinking line in blink_lines
- private PlayerID blink_t = PlayerID.NOBODY; // garbage
+ private Player blink_t = Player.NOBODY; // garbage
private uint8 blink_n = 0;
private bool blink_on = false;
private uint timeout = 0;
@@ -265,19 +265,19 @@ private class FourInARow : Gtk.Application
if (settings.get_string ("first-player") == "human")
{
game_type_action.set_state (new Variant.string ("human"));
- last_first_player = PlayerID.HUMAN;
+ last_first_player = Player.HUMAN;
}
else
{
game_type_action.set_state (new Variant.string ("computer"));
- last_first_player = PlayerID.OPPONENT;
+ last_first_player = Player.OPPONENT;
}
}
else
{
new_game_screen.update_sensitivity (false);
game_type_action.set_state (new Variant.string ("two"));
- last_first_player = PlayerID.NOBODY;
+ last_first_player = Player.NOBODY;
}
});
bool solo = settings.get_int ("num-players") == 1;
@@ -288,19 +288,19 @@ private class FourInARow : Gtk.Application
if (settings.get_string ("first-player") == "human")
{
game_type_action.set_state (new Variant.string ("human"));
- last_first_player = PlayerID.HUMAN;
+ last_first_player = Player.HUMAN;
}
else
{
game_type_action.set_state (new Variant.string ("computer"));
- last_first_player = PlayerID.OPPONENT;
+ last_first_player = Player.OPPONENT;
}
}
else
{
new_game_screen.update_sensitivity (false);
game_type_action.set_state (new Variant.string ("two"));
- last_first_player = PlayerID.NOBODY;
+ last_first_player = Player.NOBODY;
}
}
@@ -335,9 +335,9 @@ private class FourInARow : Gtk.Application
if (one_player_game)
{
- player = settings.get_string ("first-player") == "computer" ? PlayerID.OPPONENT :
PlayerID.HUMAN;
+ player = settings.get_string ("first-player") == "computer" ? Player.OPPONENT : Player.HUMAN;
// we keep inverting that, because it would be surprising that all people use the "next
round" thing
- settings.set_string ("first-player", player == PlayerID.HUMAN ? "computer" : "human");
+ settings.set_string ("first-player", player == Player.HUMAN ? "computer" : "human");
ai_level = (uint8) settings.get_int ("opponent");
}
else
@@ -377,9 +377,9 @@ private class FourInARow : Gtk.Application
{
switch (last_first_player)
{
- case PlayerID.HUMAN : player = PlayerID.OPPONENT; break;
- case PlayerID.OPPONENT:
- case PlayerID.NOBODY : player = PlayerID.HUMAN; break;
+ case Player.HUMAN : player = Player.OPPONENT; break;
+ case Player.OPPONENT:
+ case Player.NOBODY : player = Player.HUMAN; break;
}
last_first_player = player;
}
@@ -387,7 +387,7 @@ private class FourInARow : Gtk.Application
private void blink_winner (uint8 n) /* blink the winner's line(s) n times */
// requires (n < 128)
{
- if (winner == PlayerID.NOBODY)
+ if (winner == Player.NOBODY)
return;
blink_t = winner;
@@ -405,7 +405,7 @@ private class FourInARow : Gtk.Application
}
}
- private inline void draw_line (uint8 _r1, uint8 _c1, uint8 _r2, uint8 _c2, PlayerID owner)
+ private inline void draw_line (uint8 _r1, uint8 _c1, uint8 _r2, uint8 _c2, Player owner)
{
/* draw a line of 'tile' from r1,c1 to r2,c2 */
@@ -451,9 +451,9 @@ private class FourInARow : Gtk.Application
else
window.allow_undo (moves > 0);
- if (gameover && winner == PlayerID.NOBODY)
+ if (gameover && winner == Player.NOBODY)
{
- if (score [PlayerID.NOBODY] == 0)
+ if (score [Player.NOBODY] == 0)
set_status_message (null);
else
/* Translators: text displayed on game end in the headerbar/actionbar, if the game is a tie
*/
@@ -486,11 +486,11 @@ private class FourInARow : Gtk.Application
{
string who;
if (gameover)
- who = player == HUMAN ? theme_get_player_win (PlayerID.HUMAN, theme_id)
- : theme_get_player_win (PlayerID.OPPONENT, theme_id);
+ who = player == HUMAN ? theme_get_player_win (Player.HUMAN, theme_id)
+ : theme_get_player_win (Player.OPPONENT, theme_id);
else
- who = player == HUMAN ? theme_get_player_turn (PlayerID.HUMAN, theme_id)
- : theme_get_player_turn (PlayerID.OPPONENT, theme_id);
+ who = player == HUMAN ? theme_get_player_turn (Player.HUMAN, theme_id)
+ : theme_get_player_turn (Player.OPPONENT, theme_id);
set_status_message (_(who));
}
@@ -498,7 +498,7 @@ private class FourInARow : Gtk.Application
private void swap_player ()
{
- player = (player == PlayerID.HUMAN) ? PlayerID.OPPONENT : PlayerID.HUMAN;
+ player = (player == Player.HUMAN) ? Player.OPPONENT : Player.HUMAN;
move_cursor (3);
prompt_player ();
}
@@ -517,7 +517,7 @@ private class FourInARow : Gtk.Application
score [winner]++;
scorebox.update (score, one_player_game);
prompt_player ();
- if (winner != PlayerID.NOBODY)
+ if (winner != Player.NOBODY)
blink_winner (3);
}
else
@@ -563,7 +563,7 @@ private class FourInARow : Gtk.Application
private bool is_player_human ()
{
if (one_player_game)
- return player == PlayerID.HUMAN;
+ return player == Player.HUMAN;
else
return true;
}
@@ -600,9 +600,9 @@ private class FourInARow : Gtk.Application
private inline void drop ()
{
- PlayerID tile = player == PlayerID.HUMAN ? PlayerID.HUMAN : PlayerID.OPPONENT;
+ Player tile = player == Player.HUMAN ? Player.HUMAN : Player.OPPONENT;
- game_board [row, column] = PlayerID.NOBODY;
+ game_board [row, column] = Player.NOBODY;
game_board_view.draw_tile (row, column);
row++;
@@ -612,11 +612,11 @@ private class FourInARow : Gtk.Application
private inline void move (uint8 c)
{
- game_board [0, column] = PlayerID.NOBODY;
+ game_board [0, column] = Player.NOBODY;
game_board_view.draw_tile (0, column);
column = c;
- game_board [0, c] = player == PlayerID.HUMAN ? PlayerID.HUMAN : PlayerID.OPPONENT;
+ game_board [0, c] = player == Player.HUMAN ? Player.HUMAN : Player.OPPONENT;
game_board_view.draw_tile (0, c);
}
@@ -672,7 +672,7 @@ private class FourInARow : Gtk.Application
moves = 0;
}
- private inline void blink_tile (uint8 row, uint8 col, PlayerID tile, uint8 n)
+ private inline void blink_tile (uint8 row, uint8 col, Player tile, uint8 n)
{
if (timeout != 0)
return;
@@ -743,7 +743,7 @@ private class FourInARow : Gtk.Application
/* col 1 */ application.blink_lines [application.blink_line, 1],
/* row 2 */ application.blink_lines [application.blink_line, 2],
/* col 2 */ application.blink_lines [application.blink_line, 3],
- /* tile */ application.blink_on ? application.blink_t :
PlayerID.NOBODY);
+ /* tile */ application.blink_on ? application.blink_t :
Player.NOBODY);
application.blink_n--;
if (application.blink_n == 0 && application.blink_on)
{
@@ -840,7 +840,7 @@ private class FourInARow : Gtk.Application
swap_player ();
move_cursor (c);
- game_board [r, c] = PlayerID.NOBODY;
+ game_board [r, c] = Player.NOBODY;
game_board_view.draw_tile (r, c);
if (one_player_game
@@ -854,7 +854,7 @@ private class FourInARow : Gtk.Application
moves--;
swap_player ();
move_cursor (c);
- game_board [r, c] = PlayerID.NOBODY;
+ game_board [r, c] = Player.NOBODY;
game_board_view.draw_tile (r, c);
}
}
@@ -1170,7 +1170,7 @@ private class FourInARow : Gtk.Application
else
round_section.append (_("_Give Up"), "app.give-up");
- if (score [PlayerID.HUMAN] + score [PlayerID.OPPONENT] + score [PlayerID.NOBODY] == 0)
+ if (score [Player.HUMAN] + score [Player.OPPONENT] + score [Player.NOBODY] == 0)
return;
/* Translators: hamburger menu entry; opens the Scores dialog (with a mnemonic that appears pressing
Alt) */
round_section.append (_("_Scores"), "app.scores");
@@ -1178,10 +1178,10 @@ private class FourInARow : Gtk.Application
private inline void on_give_up (/* SimpleAction action, Variant? parameter */)
{
- if (player == PlayerID.HUMAN)
- score [PlayerID.OPPONENT]++;
- else if (player == PlayerID.OPPONENT)
- score [PlayerID.HUMAN]++;
+ if (player == Player.HUMAN)
+ score [Player.OPPONENT]++;
+ else if (player == Player.OPPONENT)
+ score [Player.HUMAN]++;
else
assert_not_reached ();
scorebox.update (score, one_player_game);
@@ -1194,9 +1194,3 @@ private class FourInARow : Gtk.Application
game_reset (/* reload settings */ false);
}
}
-
-private enum PlayerID {
- HUMAN,
- OPPONENT,
- NOBODY;
-}
diff --git a/src/game-board-view.vala b/src/game-board-view.vala
index e2ebe4e..5aae168 100644
--- a/src/game-board-view.vala
+++ b/src/game-board-view.vala
@@ -124,24 +124,24 @@ private class GameBoardView : Gtk.DrawingArea
private inline void paint_tile (Cairo.Context cr, uint8 row, uint8 col)
{
- PlayerID tile = game_board [row, col];
- if (tile == PlayerID.NOBODY && row != 0)
+ Player tile = game_board [row, col];
+ if (tile == Player.NOBODY && row != 0)
return;
int os = 0;
if (row == 0)
switch (tile)
{
- case PlayerID.HUMAN : os = offset [Tile.PLAYER1_CURSOR]; break;
- case PlayerID.OPPONENT: os = offset [Tile.PLAYER2_CURSOR]; break;
- case PlayerID.NOBODY : os = offset [Tile.CLEAR_CURSOR]; break;
+ case Player.HUMAN : os = offset [Tile.PLAYER1_CURSOR]; break;
+ case Player.OPPONENT: os = offset [Tile.PLAYER2_CURSOR]; break;
+ case Player.NOBODY : os = offset [Tile.CLEAR_CURSOR]; break;
}
else
switch (tile)
{
- case PlayerID.HUMAN : os = offset [Tile.PLAYER1]; break;
- case PlayerID.OPPONENT: os = offset [Tile.PLAYER2]; break;
- case PlayerID.NOBODY : assert_not_reached ();
+ case Player.HUMAN : os = offset [Tile.PLAYER1]; break;
+ case Player.OPPONENT: os = offset [Tile.PLAYER2]; break;
+ case Player.NOBODY : assert_not_reached ();
}
cr.save ();
diff --git a/src/game-board.vala b/src/game-board.vala
index b953170..362578b 100644
--- a/src/game-board.vala
+++ b/src/game-board.vala
@@ -20,19 +20,19 @@
private class Board : Object
{
- private static PlayerID [,] gboard;
+ private static Player [,] gboard;
internal Board ()
{
- gboard = new PlayerID [BOARD_COLUMNS, BOARD_ROWS_PLUS_ONE];
+ gboard = new Player [BOARD_COLUMNS, BOARD_ROWS_PLUS_ONE];
}
- internal new void @set (uint8 x, uint8 y, PlayerID tile)
+ internal new void @set (uint8 x, uint8 y, Player tile)
{
gboard [x, y] = tile;
}
- internal new PlayerID @get (uint8 x, uint8 y)
+ internal new Player @get (uint8 x, uint8 y)
{
return gboard [x, y];
}
@@ -41,14 +41,14 @@ private class Board : Object
{
for (uint8 row = 0; row < BOARD_ROWS_PLUS_ONE; row++)
for (uint8 col = 0; col < BOARD_COLUMNS; col++)
- gboard [row, col] = PlayerID.NOBODY;
+ gboard [row, col] = Player.NOBODY;
}
internal uint8 first_empty_row (uint8 col)
{
uint8 row = 1;
- while (row < BOARD_ROWS_PLUS_ONE && gboard [row, col] == PlayerID.NOBODY)
+ while (row < BOARD_ROWS_PLUS_ONE && gboard [row, col] == Player.NOBODY)
row++;
return row - 1;
}
@@ -57,7 +57,7 @@ private class Board : Object
* * check if there is a line passing by a given point
\*/
- internal bool is_line_at (PlayerID tile, uint8 row, uint8 col, out uint8 [,] lines = null)
+ internal bool is_line_at (Player tile, uint8 row, uint8 col, out uint8 [,] lines = null)
{
uint8 n_lines = 0;
uint8 [,] lines_tmp = new uint8 [4, 4];
@@ -90,9 +90,9 @@ private class Board : Object
return n_lines != 0;
}
- private inline bool is_hline_at (PlayerID tile, uint8 row, uint8 col,
- out uint8 row_1, out uint8 col_1,
- out uint8 row_2, out uint8 col_2)
+ private inline bool is_hline_at (Player tile, uint8 row, uint8 col,
+ out uint8 row_1, out uint8 col_1,
+ out uint8 row_2, out uint8 col_2)
{
row_1 = row;
row_2 = row;
@@ -105,9 +105,9 @@ private class Board : Object
return col_2 - col_1 >= 3;
}
- private inline bool is_vline_at (PlayerID tile, uint8 row, uint8 col,
- out uint8 row_1, out uint8 col_1,
- out uint8 row_2, out uint8 col_2)
+ private inline bool is_vline_at (Player tile, uint8 row, uint8 col,
+ out uint8 row_1, out uint8 col_1,
+ out uint8 row_2, out uint8 col_2)
{
row_1 = row;
row_2 = row;
@@ -120,9 +120,9 @@ private class Board : Object
return row_2 - row_1 >= 3;
}
- private inline bool is_dline1_at (PlayerID tile, uint8 row, uint8 col,
- out uint8 row_1, out uint8 col_1,
- out uint8 row_2, out uint8 col_2)
+ private inline bool is_dline1_at (Player tile, uint8 row, uint8 col,
+ out uint8 row_1, out uint8 col_1,
+ out uint8 row_2, out uint8 col_2)
{
/* upper left to lower right */
row_1 = row;
@@ -142,9 +142,9 @@ private class Board : Object
return row_2 - row_1 >= 3;
}
- private inline bool is_dline2_at (PlayerID tile, uint8 row, uint8 col,
- out uint8 row_1, out uint8 col_1,
- out uint8 row_2, out uint8 col_2)
+ private inline bool is_dline2_at (Player tile, uint8 row, uint8 col,
+ out uint8 row_1, out uint8 col_1,
+ out uint8 row_2, out uint8 col_2)
{
/* upper right to lower left */
row_1 = row;
diff --git a/src/meson.build b/src/meson.build
index 5ad148d..ddf22aa 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -3,6 +3,7 @@ four_in_a_row_tests = executable(
'four-in-a-row-tests',
[
'ai.vala',
+ 'player.vala',
'test-ai.vala'
],
dependencies: [
@@ -27,6 +28,7 @@ sources = files(
'game-window.vala',
'history-button.vala',
'new-game-screen.vala',
+ 'player.vala',
'scorebox.vala',
'theme.vala',
'vapi/config.vapi'
diff --git a/src/player.vala b/src/player.vala
new file mode 100644
index 0000000..c24ce76
--- /dev/null
+++ b/src/player.vala
@@ -0,0 +1,26 @@
+/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ This file is part of GNOME Four-in-a-row.
+
+ Copyright © 2019 Arnaud Bonatti
+
+ GNOME Four-in-a-row is free software: you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as published
+ by the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ GNOME Four-in-a-row is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with GNOME Four-in-a-row. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+private enum Player
+{
+ NOBODY,
+ HUMAN,
+ OPPONENT;
+}
diff --git a/src/scorebox.vala b/src/scorebox.vala
index 17a160e..782e21d 100644
--- a/src/scorebox.vala
+++ b/src/scorebox.vala
@@ -23,8 +23,12 @@ using Gtk;
private class Scorebox : Dialog {
[CCode (notify = false)] internal int theme_id { private get; internal set; }
- private Label[] label_name;
- private Label[] label_score;
+ private Label label_name_top;
+ private Label label_score_top;
+ private Label label_name_mid;
+ private Label label_score_mid;
+ // no change to the draw name line
+ private Label label_score_end;
internal Scorebox(Window parent, FourInARow application) {
/* Translators: title of the Scores dialog; plural noun */
@@ -40,9 +44,6 @@ private class Scorebox : Dialog {
Grid grid, grid2;
- label_name = new Label[3];
- label_score = new Label[3];
-
grid = new Grid();
grid.halign = Align.CENTER;
grid.row_spacing = 6;
@@ -55,36 +56,36 @@ private class Scorebox : Dialog {
grid.add(grid2);
grid2.column_spacing = 6;
- label_name[PlayerID.HUMAN] = new Label(null);
- grid2.attach(label_name[PlayerID.HUMAN], 0, 0, 1, 1);
- label_name[PlayerID.HUMAN].xalign = 0;
- label_name[PlayerID.HUMAN].yalign = 0.5f;
+ label_name_top = new Label(null);
+ grid2.attach(label_name_top, 0, 0, 1, 1);
+ label_name_top.xalign = 0;
+ label_name_top.yalign = 0.5f;
- label_score[PlayerID.HUMAN] = new Label(null);
- grid2.attach(label_score[PlayerID.HUMAN], 1, 0, 1, 1);
- label_score[PlayerID.HUMAN].xalign = 0;
- label_score[PlayerID.HUMAN].yalign = 0.5f;
+ label_score_top = new Label(null);
+ grid2.attach(label_score_top, 1, 0, 1, 1);
+ label_score_top.xalign = 0;
+ label_score_top.yalign = 0.5f;
- label_name[PlayerID.OPPONENT] = new Label(null);
- grid2.attach(label_name[PlayerID.OPPONENT], 0, 1, 1, 1);
- label_name[PlayerID.OPPONENT].xalign = 0;
- label_name[PlayerID.OPPONENT].yalign = 0.5f;
+ label_name_mid = new Label(null);
+ grid2.attach(label_name_mid, 0, 1, 1, 1);
+ label_name_mid.xalign = 0;
+ label_name_mid.yalign = 0.5f;
- label_score[PlayerID.OPPONENT] = new Label(null);
- grid2.attach(label_score[PlayerID.OPPONENT], 1, 1, 1, 1);
- label_score[PlayerID.OPPONENT].set_xalign(0);
- label_score[PlayerID.OPPONENT].set_yalign(0.5f);
+ label_score_mid = new Label(null);
+ grid2.attach(label_score_mid, 1, 1, 1, 1);
+ label_score_mid.set_xalign(0);
+ label_score_mid.set_yalign(0.5f);
/* Translators: in the Scores dialog, label of the line where is indicated the number of tie games */
- label_name[PlayerID.NOBODY] = new Label(_("Drawn:"));
- grid2.attach(label_name[PlayerID.NOBODY], 0, 2, 1, 1);
- label_name[PlayerID.NOBODY].set_xalign(0);
- label_name[PlayerID.NOBODY].set_yalign(0.5f);
-
- label_score[PlayerID.NOBODY] = new Label(null);
- grid2.attach(label_score[PlayerID.NOBODY], 1, 2, 1, 1);
- label_score[PlayerID.NOBODY].set_xalign(0);
- label_score[PlayerID.NOBODY].set_yalign(0.5f);
+ Label label_name_end = new Label(_("Drawn:"));
+ grid2.attach(label_name_end, 0, 2, 1, 1);
+ label_name_end.set_xalign(0);
+ label_name_end.set_yalign(0.5f);
+
+ label_score_end = new Label(null);
+ grid2.attach(label_score_end, 1, 2, 1, 1);
+ label_score_end.set_xalign(0);
+ label_score_end.set_yalign(0.5f);
grid.show_all();
}
@@ -95,34 +96,41 @@ private class Scorebox : Dialog {
*/
internal void update(uint[] scores, bool one_player_game) {
if (one_player_game) {
- if (scores[PlayerID.HUMAN] >= scores[PlayerID.OPPONENT]) {
+ if (scores[Player.HUMAN] >= scores[Player.OPPONENT]) {
/* Translators: in the Scores dialog, label of the line where is indicated the number of
games won by the human player */
- label_name[0].set_text(_("You:"));
+ label_name_top.set_text(_("You:"));
/* Translators: in the Scores dialog, label of the line where is indicated the number of
games won by the computer player */
- label_name[1].set_text(_("Me:"));
+ label_name_mid.set_text(_("Me:"));
- label_score[0].label = scores[PlayerID.HUMAN].to_string();
- label_score[1].label = scores[PlayerID.OPPONENT].to_string();
+ label_score_top.label = scores[Player.HUMAN].to_string();
+ label_score_mid.label = scores[Player.OPPONENT].to_string();
} else {
/* Translators: in the Scores dialog, label of the line where is indicated the number of
games won by the computer player */
- label_name[0].set_text(_("Me:"));
+ label_name_top.set_text(_("Me:"));
/* Translators: in the Scores dialog, label of the line where is indicated the number of
games won by the human player */
- label_name[1].set_text(_("You:"));
+ label_name_mid.set_text(_("You:"));
- label_score[0].label = scores[1].to_string();
- label_score[1].label = scores[0].to_string();
+ label_score_top.label = scores[Player.OPPONENT].to_string();
+ label_score_mid.label = scores[Player.HUMAN].to_string();
}
} else {
- label_name[0].label = theme_get_player(PlayerID.HUMAN, (uint8) theme_id); // FIXME missing
":" at end
- label_name[1].label = theme_get_player(PlayerID.OPPONENT, (uint8) theme_id); // idem
+ if (scores[Player.HUMAN] >= scores[Player.OPPONENT]) {
+ label_name_top.label = theme_get_player(Player.HUMAN, (uint8) theme_id); // FIXME
missing ":" at end
+ label_name_mid.label = theme_get_player(Player.OPPONENT, (uint8) theme_id); // idem
- label_score[PlayerID.HUMAN].label = scores[PlayerID.HUMAN].to_string();
- label_score[PlayerID.OPPONENT].label = scores[PlayerID.OPPONENT].to_string();
- }
- label_score[PlayerID.NOBODY].label = scores[PlayerID.NOBODY].to_string();
+ label_score_top.label = scores[Player.HUMAN].to_string();
+ label_score_mid.label = scores[Player.OPPONENT].to_string();
+ } else {
+ label_name_top.label = theme_get_player(Player.OPPONENT, (uint8) theme_id); // FIXME
missing ":" at end
+ label_name_mid.label = theme_get_player(Player.HUMAN, (uint8) theme_id); // idem
+ label_score_top.label = scores[Player.OPPONENT].to_string();
+ label_score_mid.label = scores[Player.HUMAN].to_string();
+ }
+ }
+ label_score_end.label = scores[Player.NOBODY].to_string();
}
protected override bool delete_event(Gdk.EventAny event) { // TODO use hide_on_delete (Gtk3) or
hide-on-close (Gtk4) 1/2
diff --git a/src/theme.vala b/src/theme.vala
index 7e8c3f0..e237b30 100644
--- a/src/theme.vala
+++ b/src/theme.vala
@@ -44,25 +44,25 @@ private static string theme_get_title (uint8 id)
return _(theme [id].title); // FIXME this gettext call feels horrible
}
-private static string theme_get_player_turn (PlayerID who, uint8 theme_id)
+private static string theme_get_player_turn (Player who, uint8 theme_id)
{
- if (who == PlayerID.HUMAN)
+ if (who == Player.HUMAN)
return theme [theme_id].player1_turn;
else
return theme [theme_id].player2_turn;
}
-private static string theme_get_player_win (PlayerID who, uint8 theme_id)
+private static string theme_get_player_win (Player who, uint8 theme_id)
{
- if (who == PlayerID.HUMAN)
+ if (who == Player.HUMAN)
return theme [theme_id].player1_win;
else
return theme [theme_id].player2_win;
}
-private static string theme_get_player (PlayerID who, uint8 theme_id)
+private static string theme_get_player (Player who, uint8 theme_id)
{
- if (who == PlayerID.HUMAN)
+ if (who == Player.HUMAN)
return theme [theme_id].player1;
else
return theme [theme_id].player2;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]