summaryrefslogtreecommitdiff
path: root/src/main/game/game.c
diff options
context:
space:
mode:
authorAlessandro Iezzi <aiezzi@alessandroiezzi.it>2025-05-22 15:06:17 +0200
committerAlessandro Iezzi <aiezzi@alessandroiezzi.it>2025-05-22 15:06:17 +0200
commit398b3160789ab1f871f92d69d05ad4e5ffb8c54a (patch)
tree9bf7c9434a54542eca34cb9a2a9ad8d30692b3e2 /src/main/game/game.c
parent839b0ebe37c5e574e197e46eda19bbf3d3013a05 (diff)
downloadtris-398b3160789ab1f871f92d69d05ad4e5ffb8c54a.tar.gz
tris-398b3160789ab1f871f92d69d05ad4e5ffb8c54a.zip
Add a draft of restarting the game
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);