-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.c
249 lines (219 loc) · 5.5 KB
/
tetris.c
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
#include "tetromino.h"
#include "tetris.h"
#include "utils.h"
#include "term.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#define CELLS_SIZE BOARD_ROWS*BOARD_COLS
#define START_POS BOARD_COLS + BOARD_COLS/2 - TETROMINO_BITMAP_WIDTH/2
static int is_colliding(Tetris *tetris, int new_pos);
static int move_active_tetromino(Tetris *tetris, int step);
static int insert_tetromino(Tetris *tetris, Tetromino *tetromino);
static void remove_tetromino(Tetris *tetris, Tetromino *tetromino);
static void overwrite_tetromino(Tetris *tetris, Tetromino *tetromino, int val);
static int rotate_active_tetromino(Tetris *tetris,
void (*rotate)(Tetromino *tetromino),
void (*undo)(Tetromino *tetromino));
void initialize_tetris(Tetris *tetris)
{
int i;
tetris->active_tetromino = NULL;
if ((tetris->cells = calloc(CELLS_SIZE, sizeof(int))) == NULL)
{
die("Failed to initialize tetris");
}
/* Add horizontal borders */
for (i = 0; i < BOARD_COLS; ++i)
{
tetris->cells[i] = 1;
tetris->cells[CELLS_SIZE - i - 1] = 1;
}
/* Add vertical borders */
for (i = BOARD_COLS; i < CELLS_SIZE; i += BOARD_COLS)
{
tetris->cells[i-1] = 1;
tetris->cells[i] = 1;
}
/* Initialize next tetromino */
if ((tetris->next_tetromino = malloc(sizeof(Tetromino))) == NULL)
{
die("Failed to initialize next tetromino");
}
initialize_tetromino(tetris->next_tetromino, START_POS);
}
void terminate_tetris(Tetris *tetris)
{
free(tetris->cells);
free(tetris->active_tetromino);
}
int add_new_tetromino(Tetris *tetris)
{
free(tetris->active_tetromino);
tetris->active_tetromino = tetris->next_tetromino;
if ((tetris->next_tetromino = malloc(sizeof(Tetromino))) == NULL)
{
die("Failed to add new tetromino");
}
initialize_tetromino(tetris->next_tetromino, START_POS);
return insert_tetromino(tetris, tetris->active_tetromino);
}
int move_active_tetromino_left(Tetris *tetris)
{
return move_active_tetromino(tetris, -1);
}
int move_active_tetromino_right(Tetris *tetris)
{
return move_active_tetromino(tetris, 1);
}
int move_active_tetromino_down(Tetris *tetris)
{
return move_active_tetromino(tetris, BOARD_COLS);
}
int rotate_active_tetromino_clockwise(Tetris *tetris)
{
return rotate_active_tetromino(tetris,
rotate_tetromino_clockwise,
rotate_tetromino_anticlockwise);
}
int rotate_active_tetromino_anticlockwise(Tetris *tetris)
{
return rotate_active_tetromino(tetris,
rotate_tetromino_anticlockwise,
rotate_tetromino_clockwise);
}
void drop_active_tetromino(Tetris *tetris)
{
while (move_active_tetromino_down(tetris))
{
}
}
int remove_full_rows(Tetris *tetris)
{
int row, col, is_full, num_of_rows_removed = 0;
int start = tetris->active_tetromino->pos / BOARD_COLS;
int end = (start + TETROMINO_BITMAP_HEIGHT + 1 > BOARD_ROWS - 1) ? BOARD_ROWS - 1 : start + TETROMINO_BITMAP_HEIGHT + 1;
for (row = start; row < end; ++row)
{
is_full = 1;
for (col = 1; col < BOARD_COLS - 1; ++col)
{
if (tetris->cells[col + row*BOARD_COLS] == 0)
{
is_full = 0;
break;
}
}
if (is_full == 1)
{
for (col = 1; col < BOARD_COLS - 1; ++col)
{
tetris->cells[col + row*BOARD_COLS] = 0;
}
++num_of_rows_removed;
}
}
return num_of_rows_removed;
}
void remove_empty_rows(Tetris *tetris)
{
int row, tmp_row, col, is_empty;
int start = tetris->active_tetromino->pos / BOARD_COLS;
for (row = BOARD_ROWS - 2; row >= start; --row)
{
is_empty = 1;
for (col = 1; col < BOARD_COLS - 1; ++col)
{
if (tetris->cells[col + row*BOARD_COLS] != 0)
{
is_empty = 0;
break;
}
}
if (is_empty == 1)
{
for (col = 1; col < BOARD_COLS - 1; ++col)
{
for (tmp_row = row; tmp_row > 1; --tmp_row)
{
tetris->cells[col + tmp_row*BOARD_COLS] = tetris->cells[col + (tmp_row - 1)*BOARD_COLS];
}
}
++row;
++start;
}
}
}
static int is_colliding(Tetris *tetris, int new_pos)
{
int x, y;
for (y = 0; y < TETROMINO_BITMAP_WIDTH; ++y)
{
for (x = 0; x < TETROMINO_BITMAP_WIDTH; ++x)
{
if (tetris->active_tetromino->bitmap[x + y*TETROMINO_BITMAP_WIDTH] == 1
&& tetris->cells[new_pos + x + y*BOARD_COLS] != 0
&& tetris->cells[new_pos + x + y*BOARD_COLS] != tetris->active_tetromino->id)
{
return 1;
}
}
}
return 0;
}
int move_active_tetromino(Tetris *tetris, int step)
{
if (is_colliding(tetris, tetris->active_tetromino->pos + step))
{
return 0;
}
remove_tetromino(tetris, tetris->active_tetromino);
tetris->active_tetromino->pos += step;
insert_tetromino(tetris, tetris->active_tetromino);
return 1;
}
int insert_tetromino(Tetris *tetris, Tetromino *tetromino)
{
if (is_colliding(tetris, tetromino->pos))
{
return 0;
}
overwrite_tetromino(tetris, tetromino, tetromino->id);
return 1;
}
void remove_tetromino(Tetris *tetris, Tetromino *tetromino)
{
overwrite_tetromino(tetris, tetromino, 0);
}
void overwrite_tetromino(Tetris *tetris, Tetromino *tetromino, int val)
{
int x, y;
for (y = 0; y < TETROMINO_BITMAP_WIDTH; ++y)
{
for (x = 0; x < TETROMINO_BITMAP_WIDTH; ++x)
{
if (tetromino->bitmap[x + y*TETROMINO_BITMAP_WIDTH] == 1)
{
tetris->cells[tetromino->pos + x + y*BOARD_COLS] = val;
}
}
}
}
static int rotate_active_tetromino(Tetris *tetris, void (*rotate)(Tetromino *tetromino), void (*undo)(Tetromino *tetromino))
{
int ret;
remove_tetromino(tetris, tetris->active_tetromino);
rotate(tetris->active_tetromino);
if (is_colliding(tetris, tetris->active_tetromino->pos))
{
undo(tetris->active_tetromino);
ret = 0;
}
else
{
ret = 1;
}
insert_tetromino(tetris, tetris->active_tetromino);
return ret;
}