forked from neildick/ASCII-Tetris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.c
305 lines (275 loc) · 7.6 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "ARMCM0plus.h"
#include "tetris.h"
#include "randoms.h"
#include <stdbool.h>
#include <stdlib.h>
#include "SEGGER_RTT.h"
#define ASSERT(x) \
do { \
if (!(x)) { \
SEGGER_RTT_printf(0, "assert failed! %s %d\n", __FUNCTION__, __LINE__); \
while (1) \
; \
} \
} while (0)
/* Some cursor functions */
// static inline void move_cursor_up(int n) { printf("\033[%dA", n); }
// static inline void move_cursor_down(int n) { printf("\033[%dB", n); }
// static inline void move_cursor_left(int n) { printf("\033[%dC", n); }
// static inline void move_cursor_right(int n) { printf("\033[%dD", n); }
// move the cursor to line L column C
static inline void
move_cursor(int L, int C)
{
SEGGER_RTT_printf(0, "\033[%d;%dH", L, C);
}
static inline void
clear_screen(void)
{
SEGGER_RTT_WriteString(0, "\033[2J");
}
#define COLOR_STR "\033[%dm"
#define END_FMT "\033[0m"
struct tetris_level
{
int score;
int nsec;
};
struct tetris_block blocks[] = {
{ { "##", "##" }, 2, 2, NONE }, { { " X ", "XXX" }, 3, 2, NONE },
{ { "@@@@" }, 4, 1, NONE }, { { "OO", "O ", "O " }, 2, 3, NONE },
{ { "&&", " &", " &" }, 2, 3, NONE }, { { "ZZ ", " ZZ" }, 3, 2, NONE }
};
struct tetris_level levels[] = { { 0, 1200000 }, { 1500, 900000 },
{ 8000, 700000 }, { 20000, 500000 },
{ 40000, 400000 }, { 75000, 300000 },
{ 100000, 200000 } };
#define TETRIS_PIECES (sizeof(blocks) / sizeof(struct tetris_block))
#define TETRIS_LEVELS (sizeof(levels) / sizeof(struct tetris_level))
void
tetris_init(struct tetris* t, int w, int h)
{
int x, y;
t->level = 1;
t->score = 0;
t->gameover = 0;
t->w = w;
t->h = h;
for (x = 0; x < w; x++) {
for (y = 0; y < h; y++)
t->game[x][y] = (tile){ ' ', NONE };
}
}
void
tetris_print(struct tetris* t)
{
int x, y;
move_cursor(0, 0);
SEGGER_RTT_printf(0, "[LEVEL: %d | SCORE: %d]\n", t->level, t->score);
for (y = 0; y < t->h; y++) {
SEGGER_RTT_WriteString(0, "!");
for (x = 0; x < t->w; x++) {
if (x >= t->x && y >= t->y && x < (t->x + t->current.w) &&
y < (t->y + t->current.h) &&
t->current.data[y - t->y][x - t->x] != ' ')
SEGGER_RTT_printf(0, COLOR_STR "[]" END_FMT, t->current.color);
else {
if (t->game[x][y].val != ' ') {
ASSERT(t->game[x][y].color != NONE);
SEGGER_RTT_printf(0, COLOR_STR "[]" END_FMT, t->game[x][y].color);
} else {
SEGGER_RTT_WriteString(0, " ");
}
}
}
SEGGER_RTT_WriteString(0, "!\n");
}
for (x = 0; x < 2 * t->w + 2; x++)
SEGGER_RTT_WriteString(0, "~");
SEGGER_RTT_WriteString(0, "\n");
}
int
tetris_hittest(struct tetris* t)
{
int x, y, X, Y;
struct tetris_block b = t->current;
for (x = 0; x < b.w; x++)
for (y = 0; y < b.h; y++) {
X = t->x + x;
Y = t->y + y;
if (X < 0 || X >= t->w)
return 1;
if (b.data[y][x] != ' ') {
if ((Y >= t->h) || (X >= 0 && X < t->w && Y >= 0 &&
t->game[X][Y].val != ' ')) {
return 1;
}
}
}
return 0;
}
void
tetris_new_block(struct tetris* t)
{
t->current = blocks[RANDOM()];
t->x = (t->w / 2) - (t->current.w / 2);
t->y = 0;
t->current.color = RED + RANDOM();
ASSERT(t->current.color < NONE);
if (tetris_hittest(t)) {
t->gameover = 1;
}
}
void
tetris_print_block(struct tetris* t)
{
int x, y;
struct tetris_block b = t->current;
for (x = 0; x < b.w; x++)
for (y = 0; y < b.h; y++) {
if (b.data[y][x] != ' ')
t->game[t->x + x][t->y + y] = (tile){ b.data[y][x], b.color };
}
}
void
tetris_rotate(struct tetris* t)
{
struct tetris_block b = t->current;
struct tetris_block s = b;
int x, y;
b.w = s.h;
b.h = s.w;
for (x = 0; x < s.w; x++)
for (y = 0; y < s.h; y++) {
b.data[x][y] = s.data[s.h - y - 1][x];
}
x = t->x;
y = t->y;
t->x -= (b.w - s.w) / 2;
t->y -= (b.h - s.h) / 2;
t->current = b;
if (tetris_hittest(t)) {
t->current = s;
t->x = x;
t->y = y;
}
}
void
tetris_gravity(struct tetris* t)
{
t->y++;
if (tetris_hittest(t)) {
t->y--;
tetris_print_block(t);
tetris_new_block(t);
}
}
void
tetris_fall(struct tetris* t, int l)
{
int x, y;
for (y = l; y > 0; y--) {
for (x = 0; x < t->w; x++)
t->game[x][y] = t->game[x][y - 1];
}
for (x = 0; x < t->w; x++)
t->game[x][0].val = ' ';
}
void
tetris_check_lines(struct tetris* t)
{
int x, y, l;
int p = 100;
for (y = t->h - 1; y >= 0; y--) {
l = 1;
for (x = 0; x < t->w && l; x++) {
if (t->game[x][y].val == ' ') {
l = 0;
}
}
if (l) {
t->score += p;
p *= 2;
tetris_fall(t, y);
y++;
}
}
}
static volatile int count;
// called by the Systick timer.
void
SysTick_Handler(void)
{
count ++;
}
int
tetris_level(struct tetris* t)
{
for (size_t i = 0; i < TETRIS_LEVELS; i++) {
if (t->score >= levels[i].score) {
t->level = i + 1;
} else
break;
}
return levels[t->level - 1].nsec;
}
#define BUF_SIZE 512
void
tetris_run(int w, int h)
{
struct tetris t;
int count = 0;
bool moved = false;
SEGGER_RTT_Init();
__enable_irq();
SysTick_Config(10000);
tetris_init(&t, w, h);
tetris_new_block(&t);
clear_screen();
while (!t.gameover) {
__WFI();
if (count == 100 || count == 200) {
if (moved) {
tetris_print(&t);
moved = false;
}
}
if (count == 300) {
tetris_gravity(&t);
tetris_check_lines(&t);
__disable_irq();
count = 0;
__enable_irq();
}
if (SEGGER_RTT_HasData(0)) {
char buf[BUF_SIZE];
size_t n = SEGGER_RTT_Read(0, &buf, BUF_SIZE);
for (size_t i = 0; i < n; i++) {
switch (buf[i]) {
case 'a':
moved = true;
t.x--;
if (tetris_hittest(&t))
t.x++;
break;
case 'd':
moved = true;
t.x++;
if (tetris_hittest(&t))
t.x--;
break;
case 's':
moved = true;
tetris_gravity(&t);
break;
case 'w':
moved = true;
tetris_rotate(&t);
break;
}
}
}
}
tetris_print(&t);
SEGGER_RTT_WriteString(0, "*** GAME OVER ***\n");
}