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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*-
* 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"
#define WIN_PATTERNS 8
typedef struct CrossShape {
list_t *lines;
} CrossShape;
static CrossShape *
cross_shape_new(float left, float right, float top, float bottom)
{
CrossShape *shape = malloc(sizeof(CrossShape));
shape->lines = list_create();
list_add(shape->lines, engine_line_new(left, top, right, bottom));
list_add(shape->lines, engine_line_new(right, top, left, bottom));
return shape;
}
static const char SIGN_CIRCLE = 'o';
static const char SIGN_CROSS = 'x';
/* There are 8 lines to win:
* - 0, 1, 2 (horizontal)
* - 3, 4, 5 (horizontal)
* - 6, 7, 8 (horizontal)
* - 0, 3, 6 (vertical)
* - 1, 4, 7 (vertical)
* - 2, 5, 8 (vertical)
* - 0, 4, 8 (diagonal)
* - 2, 4, 6 (diagonal)
*/
static const int win_patterns[WIN_PATTERNS][3] = {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, /* horizontal */
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, /* vertical */
{0, 4, 8}, {2, 4, 6} /* diagonal */
};
static Log *log = NULL;
static void
init_log(void)
{
if (log != NULL) return;
log = log_create("Game");
}
static void game_draw_field(TrisGame *game, float box_size);
static char game_next_sign(TrisGame *game);
static Color *COLOR_BOARD;
static Color *COLOR_CIRCLE;
static Color *COLOR_CROSS;
static void
draw_frames(void *data)
{
if (data == NULL) return;
TrisGame *game = data;
engine_render_lines(game->board->lines, COLOR_BOARD);
for (int i = 0; i < BOARD_SIZE; i++) {
Cell *cell = game->cells[i];
if (cell->sign == SIGN_CIRCLE) {
engine_render_circle(cell->shape);
} else if (cell->sign == SIGN_CROSS) {
CrossShape *shape = cell->shape;
engine_render_lines(shape->lines, COLOR_CROSS);
}
}
}
static void
game_restart(TrisGame *game)
{
game->ended = false;
game->moves = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
Cell *cell = game->cells[i];
cell->filled = false;
cell->sign = '\0';
if (cell->shape != NULL) {
free(cell->shape);
}
}
engine_set_rendering_background_c(game->engine, game->board->default_color);
}
static void
check_win(TrisGame *game)
{
/* The minimum number of moves to win the play */
if (game->moves < 5) return;
for (int i = 0; i < WIN_PATTERNS; i++) {
/* These are the indexes of every pattern */
int i0 = win_patterns[i][0];
int i1 = win_patterns[i][1];
int i2 = win_patterns[i][2];
if (game->cells[i0]->sign != '\0'
&& 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;
}
}
static void
draw_sign(TrisGame *game, Cell *cell)
{
float x = cell->cx;
float y = cell->cy;
float l = 0.20f;
char sign = game_next_sign(game);
cell_set_sign(cell, sign);
if (sign == SIGN_CIRCLE) {
cell->shape = engine_circle_new(x, y, l, 120, 1);
engine_circle_set_color(cell->shape, COLOR_CIRCLE);
game->sign = SIGN_CROSS;
} else if (sign == SIGN_CROSS) {
cell->shape = cross_shape_new(x - l, x + l, y + l, y - l);
game->sign = SIGN_CIRCLE;
}
game->moves++;
check_win(game);
}
static void
loop_cells(TrisGame *game, float x, float y)
{
init_log();
for (int i = 0; i < BOARD_SIZE; i++) {
Cell *cell = game->cells[i];
if (!cell_is_filled(cell) && cell_within_bounds(cell, x, y)) {
draw_sign(game, cell);
cell_set_filled(cell, true);
}
}
}
static void
game_mouse_button_pressed(float x, float y, void *data)
{
TrisGame *game = data;
if (!game->ended) {
loop_cells(game, x, y);
} else {
game_restart(game);
}
}
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->sign = SIGN_CIRCLE;
game->moves = 0;
game->engine = engine_new(width, height);
game->ended = false;
COLOR_BOARD = color_new(1.0f, 1.0f, 0.0f, 1.0f);
COLOR_CIRCLE = color_new(1.0f, 0.3f, 0.5f, 1.0f);
COLOR_CROSS = color_new(0.3f, 0.2f, 0.8f, 1.0f);
engine_set_window_title(game->engine, "Tris Game");
engine_set_mouse_button_listener(game_mouse_button_pressed, game);
engine_set_rendering_listener(game->engine, draw_frames, game);
game_draw_field(game, 0.5f);
return game;
}
void
game_start()
{
engine_loop();
}
static void
game_draw_field(TrisGame *game, float box_size)
{
float x = 0.0f;
float y = 0.0f;
float half_box = box_size / 2;
float xmin = x - half_box * 3;
float xmax = x + half_box * 3;
float ymin = y - half_box * 3;
float ymax = y + half_box * 3;
game->board = board_new(xmin, xmax, ymin, ymax, box_size);
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 */
game->cells[0] = cell_new(xmin, first_col, ymax, first_row);
game->cells[1] = cell_new(first_col, second_col, ymax, first_row);
game->cells[2] = cell_new(second_col, xmax, ymax, first_row);
/* Second row */
game->cells[3] = cell_new(xmin, first_col, first_row, second_row);
game->cells[4] = cell_new(first_col, second_col, first_row, second_row);
game->cells[5] = cell_new(second_col, xmax, first_row, second_row);
/* Third row */
game->cells[6] = cell_new(xmin, first_col, second_row, ymin);
game->cells[7] = cell_new(first_col, second_col, second_row, ymin);
game->cells[8] = cell_new(second_col, xmax, second_row, ymin);
}
static char
game_next_sign(TrisGame *game)
{
return game->sign;
}
|