summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlessandro Iezzi <aiezzi@alessandroiezzi.it>2025-05-22 00:25:09 +0200
committerAlessandro Iezzi <aiezzi@alessandroiezzi.it>2025-05-22 00:25:09 +0200
commitdc9d30ee5ee98f0f4aa024385ce0516fef63305b (patch)
tree72f6484a88aafe43031743dc50d2145736cf8360
parentf9080d2f98311b36a76c01a9592272391b638bbe (diff)
downloadtris-dc9d30ee5ee98f0f4aa024385ce0516fef63305b.tar.gz
tris-dc9d30ee5ee98f0f4aa024385ce0516fef63305b.zip
Move the next sign attribute inside TrisGame struct
-rw-r--r--src/main/game/game.c31
-rw-r--r--src/main/game/game.h1
2 files changed, 21 insertions, 11 deletions
diff --git a/src/main/game/game.c b/src/main/game/game.c
index c8f8a27..e91eb6b 100644
--- a/src/main/game/game.c
+++ b/src/main/game/game.c
@@ -25,6 +25,9 @@
#include "cell.h"
#include "../engine/ui/ui.h"
+const char SIGN_CIRCLE = 'o';
+const char SIGN_CROSS = 'x';
+
static Log *log = NULL;
static void
@@ -34,38 +37,38 @@ init_log(void)
log = log_create("Game");
}
-int sign = 1; /* 1 = circle, 2 = cross */
int moves = 0;
static void game_draw_field(TrisGame *game, float box_size, float x, float y);
+static char game_next_sign(TrisGame *game);
static void
-draw_sign(float x, float y)
+draw_sign(TrisGame *game, float x, float y)
{
float l = 0.20;
- if (sign == 1) {
+ if (game_next_sign(game) == SIGN_CIRCLE) {
engine_draw_circle(x, y, l, 120, 1);
- sign = 2;
- } else if (sign == 2) {
+ game->sign = SIGN_CROSS;
+ } else if (game_next_sign(game) == SIGN_CROSS) {
engine_draw_line(x - l, y + l, x + l, y - l);
engine_draw_line(x + l, y + l, x - l, y - l);
- sign = 1;
+ game->sign = SIGN_CIRCLE;
}
moves++;
}
static void
-loop_cells(list_t *cells, float x, float y)
+loop_cells(TrisGame *game, float x, float y)
{
init_log();
- list_node_t *current = cells->head;
+ list_node_t *current = game->cells->head;
do {
Cell *cell = current->data;
if (!cell_is_filled(cell) && cell_within_bounds(cell, x, y)) {
- draw_sign(cell->cx, cell->cy);
+ draw_sign(game, cell->cx, cell->cy);
cell_set_filled(cell, true);
}
current = current->next;
@@ -75,8 +78,7 @@ loop_cells(list_t *cells, float x, float y)
static void
game_mouse_button_pressed(float x, float y, void *data)
{
- TrisGame *game = data;
- loop_cells(game->cells, x, y);
+ loop_cells(data, x, y);
}
TrisGame *
@@ -93,6 +95,7 @@ game_init(int width, int height)
game->width = width;
game->height = height;
game->cells = list_create();
+ game->sign = SIGN_CIRCLE;
engine_init(width, height);
ui_set_title("Tris Game");
@@ -144,3 +147,9 @@ game_draw_field(TrisGame *game, float box_size, float x, float y)
list_add(game->cells, cell_new(first_col, second_col, second_row, ymin));
list_add(game->cells, cell_new(second_col, xmax, second_row, ymin));
}
+
+static char
+game_next_sign(TrisGame *game)
+{
+ return game->sign;
+}
diff --git a/src/main/game/game.h b/src/main/game/game.h
index d59df85..b90c5fe 100644
--- a/src/main/game/game.h
+++ b/src/main/game/game.h
@@ -25,6 +25,7 @@
typedef struct TrisGame {
int width, height;
list_t *cells;
+ char sign; /* x = cross and o = circle */
} TrisGame;
TrisGame *game_init(int width, int height);