1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/*-
* Copyright (C) 2025 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it>
*
* This file is part of Tris Game.
*
* Tris Game 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.
*
* Tris Game 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 Tris Game. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <log.h>
#include "../engine/engine.h"
#include "game.h"
#include "cell.h"
#include "../engine/ui/ui.h"
static Log *log = NULL;
static void
init_log(void)
{
if (log != NULL) return;
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 moves = 0;
static void game_draw_field(TrisGame *game, float box_size, float x, float y);
static void
draw_sign(float x, float y)
{
float l = 0.20;
if (sign == 1) {
engine_draw_circle(x, y, l, 120, 1);
sign = 2;
} else if (sign == 2) {
engine_draw_line(x - l, y + l, x + l, y - l);
engine_draw_line(x + l, y + l, x - l, y - l);
sign = 1;
}
moves++;
}
static void
loop_cells(list_t *cells, float x, float y)
{
init_log();
list_node_t *current = cells->head;
do {
Cell *cell = current->data;
if (cell_within_bounds(cell, x, y)) {
}
current = current->next;
} while (current != NULL);
}
static void
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) {
draw_sign(half_first_col, half_first_row);
field_matrix[0][0] = sign;
} else if (x >= first_col && x <= second_col && field_matrix[0][1] == -1) {
draw_sign(half_second_col, half_first_row);
field_matrix[0][1] = sign;
} else if (x >= second_col && field_matrix[0][2] == -1) {
draw_sign(half_third_col, half_first_row);
field_matrix[0][2] = sign;
}
} else if (y >= second_row) {
if (x <= first_col && field_matrix[1][0] == -1) {
draw_sign(half_first_col, half_second_row);
field_matrix[1][0] = sign;
} else if (x >= first_col && x <= second_col && field_matrix[1][1] == -1) {
draw_sign(half_second_col, half_second_row);
field_matrix[1][1] = sign;
} else if (x >= second_col && field_matrix[1][2] == -1) {
draw_sign(half_third_col, half_second_row);
field_matrix[1][2] = sign;
}
} else if (y >= third_row) {
if (x <= first_col && field_matrix[2][0] == -1) {
draw_sign(half_first_col, half_third_row);
field_matrix[2][0] = sign;
} else if (x >= first_col && x <= second_col && field_matrix[2][1] == -1) {
draw_sign(half_second_col, half_third_row);
field_matrix[2][1] = sign;
} else if (x >= second_col && field_matrix[2][2] == -1) {
draw_sign(half_third_col, half_third_row);
field_matrix[2][2] = sign;
}
}
}
check_win();
}
TrisGame *
game_init(int width, int height)
{
init_log();
/* Init of the TrisGame */
TrisGame *game = malloc(sizeof(TrisGame));
if (game == NULL) {
log_error(log, "Error allocating memory for the game");
exit(EXIT_FAILURE);
}
game->width = width;
game->height = height;
game->cells = list_create();
engine_init(width, height);
ui_set_title("Tris Game");
engine_set_mouse_button_listener(game_mouse_button_pressed, game);
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;
}
void
game_start()
{
engine_loop();
}
static void
game_draw_field(TrisGame *game, float _box_size, float x, float y)
{
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;
half_first_col = xmin + box_size / 2;
half_second_col = xmin + (box_size / 2) * 3;
half_third_col = xmin + (box_size / 2) * 5;
/* First row */
list_add(game->cells, cell_new(xmin, first_col, ymax, first_row));
list_add(game->cells, cell_new(first_col, second_col, ymax, first_row));
list_add(game->cells, cell_new(second_col, xmax, ymax, first_row));
/* Second row */
list_add(game->cells, cell_new(xmin, first_col, first_row, second_row));
list_add(game->cells, cell_new(first_col, second_col, first_row, second_row));
list_add(game->cells, cell_new(second_col, xmax, first_row, second_row));
/* Third row */
list_add(game->cells, cell_new(xmin, first_col, second_row, ymin));
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));
}
|