summaryrefslogtreecommitdiff
path: root/src/main/game/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/game/game.c')
-rw-r--r--src/main/game/game.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/main/game/game.c b/src/main/game/game.c
index b7beb41..9a5a245 100644
--- a/src/main/game/game.c
+++ b/src/main/game/game.c
@@ -59,6 +59,18 @@ static void game_draw_field(TrisGame *game, float box_size);
static char game_next_sign(TrisGame *game);
static void
+game_restart(TrisGame *game)
+{
+ game->ended = false;
+
+ for (int i = 0; i < BOARD_SIZE; i++) {
+ Cell *cell = game->cells[i];
+ cell->filled = false;
+ cell->sign = '\0';
+ }
+}
+
+static void
check_win(TrisGame *game)
{
/* The minimum number of moves to win the play */
@@ -74,12 +86,14 @@ check_win(TrisGame *game)
&& game->cells[i0]->sign == game->cells[i1]->sign
&& game->cells[i1]->sign == game->cells[i2]->sign) {
engine_set_rendering_background_c(game->engine, game->board->wining_color);
+ game->ended = true;
return;
}
}
if (game->moves >= 9) {
engine_set_rendering_background_c(game->engine, game->board->draft_color);
+ game->ended = true;
}
}
@@ -122,7 +136,12 @@ loop_cells(TrisGame *game, float x, float y)
static void
game_mouse_button_pressed(float x, float y, void *data)
{
- loop_cells(data, x, y);
+ TrisGame *game = data;
+ if (!game->ended) {
+ loop_cells(game, x, y);
+ } else {
+ game_restart(game);
+ }
}
TrisGame *
@@ -142,6 +161,7 @@ game_init(int width, int height)
game->moves = 0;
game->engine = engine_new(width, height);
game->board = board_new();
+ game->ended = false;
ui_set_title("Tris Game");
engine_set_mouse_button_listener(game_mouse_button_pressed, game);