summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlessandro Iezzi <aiezzi@alessandroiezzi.it>2025-05-22 00:15:22 +0200
committerAlessandro Iezzi <aiezzi@alessandroiezzi.it>2025-05-22 00:15:22 +0200
commitf9080d2f98311b36a76c01a9592272391b638bbe (patch)
treebaefba2ce132b65ea38d822946dd2eb6ffef7dc0
parentc07d2adb16532691e22a34d11ad87b56a4dc1a89 (diff)
downloadtris-f9080d2f98311b36a76c01a9592272391b638bbe.tar.gz
tris-f9080d2f98311b36a76c01a9592272391b638bbe.zip
Clean the code
-rw-r--r--src/main/game/game.c85
1 files changed, 11 insertions, 74 deletions
diff --git a/src/main/game/game.c b/src/main/game/game.c
index e92e283..c8f8a27 100644
--- a/src/main/game/game.c
+++ b/src/main/game/game.c
@@ -34,20 +34,7 @@ init_log(void)
log = log_create("Game");
}
-float xmin, xmax, ymin, ymax;
-float box_size;
-int field_matrix[3][3];
-
-float first_row, half_first_row;
-float first_col, half_first_col;
-float second_row, half_second_row;
-float second_col, half_second_col;
-float third_row, half_third_row;
-float third_col, half_third_col;
-
-/* 1 = circle, 2 = cross */
-int sign = 1;
-
+int sign = 1; /* 1 = circle, 2 = cross */
int moves = 0;
static void game_draw_field(TrisGame *game, float box_size, float x, float y);
@@ -90,37 +77,6 @@ game_mouse_button_pressed(float x, float y, void *data)
{
TrisGame *game = data;
loop_cells(game->cells, x, y);
-
- /* Click inside the field */
- if (x >= xmin && x <= xmax && y >= ymin && y <= ymax) {
- if (y >= first_row) {
- if (x <= first_col && field_matrix[0][0] == -1) {
- field_matrix[0][0] = sign;
- } else if (x >= first_col && x <= second_col && field_matrix[0][1] == -1) {
- field_matrix[0][1] = sign;
- } else if (x >= second_col && field_matrix[0][2] == -1) {
- field_matrix[0][2] = sign;
- }
- } else if (y >= second_row) {
- if (x <= first_col && field_matrix[1][0] == -1) {
- field_matrix[1][0] = sign;
- } else if (x >= first_col && x <= second_col && field_matrix[1][1] == -1) {
- field_matrix[1][1] = sign;
- } else if (x >= second_col && field_matrix[1][2] == -1) {
- field_matrix[1][2] = sign;
- }
- } else if (y >= third_row) {
- if (x <= first_col && field_matrix[2][0] == -1) {
- field_matrix[2][0] = sign;
- } else if (x >= first_col && x <= second_col && field_matrix[2][1] == -1) {
- field_matrix[2][1] = sign;
- } else if (x >= second_col && field_matrix[2][2] == -1) {
- field_matrix[2][2] = sign;
- }
- }
- }
-
- check_win();
}
TrisGame *
@@ -144,12 +100,6 @@ game_init(int width, int height)
game_draw_field(game, 0.5, 0, 0);
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- field_matrix[i][j] = -1;
- }
- }
-
return game;
}
@@ -160,37 +110,24 @@ game_start()
}
static void
-game_draw_field(TrisGame *game, float _box_size, float x, float y)
+game_draw_field(TrisGame *game, float box_size, float x, float y)
{
- float half_box = _box_size / 2;
+ float half_box = box_size / 2;
engine_draw_line(x - half_box, y + half_box * 3, x - half_box, y - half_box * 3);
engine_draw_line(x + half_box, y + half_box * 3, x + half_box, y - half_box * 3);
engine_draw_line(x - half_box * 3, y + half_box, x + half_box * 3, y + half_box);
engine_draw_line(x - half_box * 3, y - half_box, x + half_box * 3, y - half_box);
- xmin = x - half_box * 3;
- xmax = x + half_box * 3;
- ymin = y - half_box * 3;
- ymax = y + half_box * 3;
-
- box_size = _box_size;
-
- first_row = ymax - box_size;
- second_row = ymax - box_size * 2;
- third_row = ymax - box_size * 3;
-
- half_first_row = ymax - box_size / 2;
- half_second_row = ymax - (box_size / 2) * 3;
- half_third_row = ymax - (box_size / 2) * 5;
-
- first_col = xmin + box_size;
- second_col = xmin + box_size * 2;
- third_col = xmin + box_size * 3;
+ float xmin = x - half_box * 3;
+ float xmax = x + half_box * 3;
+ float ymin = y - half_box * 3;
+ float ymax = y + half_box * 3;
- half_first_col = xmin + box_size / 2;
- half_second_col = xmin + (box_size / 2) * 3;
- half_third_col = xmin + (box_size / 2) * 5;
+ float first_row = ymax - box_size;
+ float second_row = ymax - box_size * 2;
+ float first_col = xmin + box_size;
+ float second_col = xmin + box_size * 2;
/* First row */
list_add(game->cells, cell_new(xmin, first_col, ymax, first_row));