-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.c
544 lines (482 loc) · 11.1 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <ncurses.h>
enum {
block_up = 8,
block_right = 4,
block_down = 2,
block_left = 1,
block_empty = -1,
block_row = 2,
block_col = 4,
block_count = 7,
box_height = 20,
box_width = 20,
gameover_width = 95,
gameover_heigth = 14,
key_escape = 27,
KEY_SPACE = 32,
};
char g_char[] =
" _______n"
" / _____/n"
"/ /_____ n"
"| ___ |n"
"| |___| |n"
"\\_______| ";
char a_char[] =
" ____ n"
" / __ |n"
" / / | |n"
" / /__| |n"
" / ____ |n"
"/_/ |_| ";
char m_char[] =
" __ __ n"
"| \\ / |n"
"| \\ / |n"
"| |\\ \\/ /| |n"
"| | \\__/ | |n"
"|_| |_| ";
char e_char[] =
"________n"
"| _____/n"
"| |___ n"
"| ___/ n"
"| |____ n"
"|______|";
char o_char[] =
" _______ n"
"/ ___ \\n"
"| | | |n"
"| | | |n"
"| |___| |n"
"\\_______/";
char v_char[] =
"__ _n"
"\\ \\ | |n"
" \\ \\ | |n"
" \\ \\ | |n"
" \\ \\| |n"
" \\___|";
char r_char[] =
" ______ n"
"| __ \\n"
"| |__| | n"
"| __ / n"
"| | \\ \\ n"
"|_| \\_\\";
char *gameover_chars[] = {
g_char,
a_char,
m_char,
e_char,
o_char,
v_char,
e_char,
r_char,
};
struct curinfo {
int x;
int y;
int box_y;
int box_x;
};
typedef void (*Rotate)(int, int, int*, int*);
typedef char Block[block_row][block_col];
Block i_block = {
{11, 10, 10, 14},
{-1, -1, -1, -1},
};
Block t_block = {
{ 0, 13, 0, -1},
{11, 2, 14, -1},
};
Block j_block = {
{13, 0, 0, -1},
{ 3, 10, 14, -1},
};
Block l_block = {
{0, 0, 13, -1},
{11, 10, 6, -1},
};
Block s_block = {
{ 0, 9, 14, -1},
{11, 6, 0, -1},
};
Block z_block = {
{11, 12, 0, -1},
{ 0, 3, 14, -1},
};
Block o_block = {
{9, 12, -1, -1},
{ 3, 6, -1, -1},
};
Block *blocks[] = {
&i_block,
&t_block,
&j_block,
&l_block,
&s_block,
&z_block,
&o_block,
};
void degree_0(int cr, int cc, int *r, int *c)
{
*r = cr;
*c = cc;
}
void degree_90(int cr, int cc, int *r, int *c)
{
*r = (block_row - 1) - cr;
*c = cc;
}
void degree_180(int cr, int cc, int *r, int *c)
{
*r = (block_row - 1) - cr;
*c = (block_col - 1) - cc;
}
void degree_270(int cr, int cc, int *r, int *c)
{
*r = cr;
*c = (block_col - 1) - cc;
}
typedef enum {
dg0 = 0,
dg90,
dg180,
dg270,
} Degree;
Rotate rotate[] = {
degree_0,
degree_90,
degree_180,
degree_270,
};
void show_block(Block block, Degree dg, int y, int x)
{
int r, c, ty, tx, flag;
flag = ty = tx = 0;
if(dg != dg0 && dg != dg180)
flag = 1;
for(int i = 0; i < block_row; i++) {
if(flag) ty = 0;
else tx = 0;
for(int l = 0; l < block_col; l++) {
(rotate[dg])(i, l, &r, &c);
move(y + ty, x + tx);
if(block[r][c] == block_empty)
continue;
if(block[r][c])
addstr("[]");
if(flag) ty += 1;
else tx += 2;
}
if(flag) tx += 2;
else ty += 1;
}
refresh();
}
void hide_block(Block block, Degree dg, int y, int x)
{
int r, c, ty, tx, flag;
flag = ty = tx = 0;
if(dg != dg0 && dg != dg180)
flag = 1;
for(int i = 0; i < block_row; i++) {
if(flag) ty = 0;
else tx = 0;
for(int l = 0; l < block_col; l++) {
(rotate[dg])(i, l, &r, &c);
move(y + ty, x + tx);
if(block[r][c] == block_empty)
continue;
if(block[r][c])
addstr(" ");
if(flag) ty += 1;
else tx += 2;
}
if(flag) tx += 2;
else ty += 1;
}
refresh();
}
void move_block(Block block, Degree dg, struct curinfo *cur, int y, int x)
{
hide_block(block, dg, cur->y, cur->x);
cur->y += y;
cur->x += x;
show_block(block, dg, cur->y, cur->x);
}
static int identify_side(Degree dg, int block_side)
{
int side = 0;
switch(dg) {
case dg0:
side = block_side;
break;
case dg90:
side = block_side << 1;
if(side > 8) side = 1;
break;
case dg180:
side = block_side << 2;
if(side > 16) side = 2;
if(side > 8) side = 1;
break;
case dg270:
side = block_side >> 1;
if(side < 1) side = 8;
break;
}
return side;
}
int check_block(Block block, Degree dg, int block_side,
struct curinfo *cur, int y, int x)
{
int r, c, bl, ty, tx, flag, side;
flag = ty = tx = 0;
side = identify_side(dg, block_side);
if(dg != dg0 && dg != dg180)
flag = 1;
for(int i = 0; i < block_row; i++) {
if(flag) ty = 0;
else tx = 0;
for(int l = 0; l < block_col; l++) {
(rotate[dg])(i, l, &r, &c);
bl = block[r][c];
if(bl == block_empty)
continue;
if(bl & side) {
if(mvinch(cur->y + y + ty, cur->x + x + tx) != ' ')
return 1;
}
if(flag) ty += 1;
else tx += 2;
}
if(flag) tx += 2;
else ty += 1;
}
return 0;
}
int is_empty(Block block, Degree dg, struct curinfo *cur)
{
int bl, r, c, ty, tx, flag;
if(dg != dg0 && dg != dg180)
flag = 1;
for(int i = 0; i < block_row; i++) {
if(flag) ty = 0;
else tx = 0;
for(int l = 0; l < block_col; l++) {
(rotate[dg])(i, l, &r, &c);
bl = block[r][c];
if(bl == block_empty)
continue;
if(bl) {
if(mvinch(cur->y + ty, cur->x + tx) != ' ')
return 0;
}
if(flag) ty += 1;
else tx += 2;
}
if(flag) tx += 2;
else ty += 1;
}
return 1;
}
void remove_line(int line, int x)
{
for(int i = 1; i < box_width + 1; i++) {
move(line, x + i);
addch(' ');
}
refresh();
}
void down_line(int line, int x)
{
int ch;
for(int i = 1; i < box_width + 1; i++) {
ch = mvinch(line - 1, x + i);
move(line, x + i);
addch(ch);
}
refresh();
}
void check_line(struct curinfo *cur, int *line)
{
int i, l;
for(i = box_height; i > 1; i--) {
int count = 0;
for(l = 1; l < box_width; l += 2) {
if(mvinch(i + cur->box_y, cur->box_x + l) != ' ')
count++;
}
if(count == 10) {
for(int h = i + cur->box_y; h > cur->box_y + 1; h--) {
remove_line(h, cur->box_x);
down_line(h, cur->box_x);
}
*line += 1;
i += 1;
}
}
}
void block_control(int key, Block *block, Degree *dg,
struct curinfo *cur, int *score)
{
switch(key) {
case KEY_UP:
hide_block(*block, *dg, cur->y, cur->x);
*dg = *dg + 1;
if(*dg > dg270)
*dg = dg0;
//if(!is_empty(*block, *dg, cur))
// *dg = *dg != dg0 ? *dg- 1 : dg270;
show_block(*block, *dg, cur->y, cur->x);
break;
case KEY_DOWN:
if(check_block(*block, *dg, block_down, cur, 1, 0))
break;
move_block(*block, *dg, cur, 1, 0);
*score += 1;
break;
case KEY_LEFT:
if(check_block(*block, *dg, block_left, cur, 0, -2))
break;
move_block(*block, *dg, cur, 0, -2);
break;
case KEY_RIGHT:
if(check_block(*block, *dg, block_right, cur, 0, 2))
break;
move_block(*block, *dg, cur, 0, 2);
break;
case KEY_SPACE:
while(!check_block(*block, *dg, block_down, cur, 1, 0)) {
move_block(*block, *dg, cur, 1, 0);
*score += 1;
}
break;
}
}
void print_box(struct curinfo *cur)
{
int i;
move(cur->box_y, cur->box_x-1);
addch('|');
for(i = 1; i < box_height + 1; i++) {
move(cur->box_y + i, cur->box_x - 1);
addstr("||");
}
move(cur->box_y + i, cur->box_x - 1);
addch('|');
for(i = 0; i < box_width + 2; i++) {
move(cur->box_y, cur->box_x + i);
addch('=');
}
move(cur->box_y, cur->box_x + i);
addch('|');
for(i = 1; i < box_height + 1; i++) {
move(cur->box_y + i, cur->box_x + box_width + 1);
addstr("||");
}
move(cur->box_y + i, cur->box_x + box_width + 1);
addch('|');
for(i = 0; i < box_width + 2; i++) {
move(cur->box_y + box_height + 1, cur->box_x + i);
addch('=');
}
move(cur->box_y + box_height + 1, cur->box_x + i);
addch('|');
}
void print_gameover(int y, int x)
{
int ty, tx;
char *cur_char;
for(int i = 0; i < 8; i++) {
cur_char = gameover_chars[i];
ty = tx = 0;
if(i == 4) {
y += gameover_heigth / 2;
x -= gameover_width / 2;
}
for(int l = 0; cur_char[l] != '\0'; l++) {
if(cur_char[l] == 'n') {
ty += 1;
tx = 0;
continue;
}
mvaddch(y + ty, x + tx++, cur_char[l]);
}
x += tx + 2;
}
refresh();
}
void print_line(int line, struct curinfo *cur)
{
char linestr[12];
sprintf(linestr, "%d", line);
move(cur->box_y - 2, cur->box_x);
addstr("Line: ");
addstr(linestr);
refresh();
}
void print_score(int score, struct curinfo *cur)
{
char scorestr[12];
sprintf(scorestr, "%d", score);
move(cur->box_y - 2, cur->box_x + (box_width - sizeof("Score: ")));
addstr("Score: ");
addstr(scorestr);
}
int main()
{
struct curinfo cur;
int rowsrc, colsrc, n;
int key = 0;
int line = 0;
int score = 0;
Block *block;
Degree degree;
initscr();
cbreak();
keypad(stdscr, 1);
noecho();
curs_set(0);
getmaxyx(stdscr, rowsrc, colsrc);
cur.y = 0;
cur.x = 0;
cur.box_y = (rowsrc - box_height) / 2;
cur.box_x = (colsrc - box_width) / 2;
timeout(500);
srand(time(NULL));
print_box(&cur);
while(key != key_escape) {
cur.y = cur.box_y + 1;
cur.x = cur.box_x + (box_width - block_col - 1) / 2;
n = rand() % block_count;
block = blocks[n];
degree = dg0;
print_line(line, &cur);
print_score(score, &cur);
show_block(*block, degree, cur.y, cur.x);
while((key = getch()) != key_escape) {
block_control(key, block, °ree, &cur, &score);
if(check_block(*block, degree, block_down, &cur, 1, 0))
break;
move_block(*block, degree, &cur, 1, 0);
}
check_line(&cur, &line);
if(cur.y == cur.box_y + 1) {
clear();
print_gameover((rowsrc - gameover_heigth) / 2,
(colsrc - (gameover_width / 2)) / 2);
getchar();
endwin();
return 0;
}
}
endwin();
return 0;
}