-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris.c
650 lines (601 loc) · 15.7 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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#define PADDING_TOP 2
#define PADDING_LEFT 4
void clean_stdin()
{
int c;
do {
c = getchar();
} while (c != '\n' && c != EOF);
}
void clearScreen()
{
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void sleepMs(int milliseconds)
{
#ifdef _WIN32
Sleep(milliseconds);
#else
usleep(milliseconds * 1000);
#endif
}
void moveCursor(int x, int y)
{
#ifdef _WIN32
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
#else
printf("\033[%d;%dH", y, x);
#endif
}
char getChar()
{
#ifdef _WIN32
return _getch();
#else
system("stty raw");
system("stty -echo");
char ch = getchar();
system("stty cooked");
system("stty echo");
return ch;
#endif
}
char ch_async;
char ch_last;
void asyncKeyThread(int* isGameRunning)
{
while (*isGameRunning)
{
char ch = getChar();
ch_async = ch;
ch_last = ch;
}
}
char getAsyncKey()
{
if (ch_async)
{
char ch = ch_async;
ch_async = 0;
return ch;
}
return 0;
}
typedef struct
{
char shape[4][4];
int x;
int y;
int rotation;
} Tetromino;
Tetromino getRandomBlock(Tetromino tetrominos[7], int width)
{
Tetromino block = tetrominos[rand() % 7];
block.x = width / 2 - 2;
block.y = -1;
return block;
}
int collisionCheck(Tetromino *block, int height, int width, char board[height][width], int delta_x, int delta_y)
{
int i;
int j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (block->shape[i][j] == '*')
{
if (block->x + j + delta_x < 0 || block->x + j + delta_x >= width)
{
return 1;
}
if (block->y + i + delta_y >= height)
{
return 1;
}
if (board[block->y + i + delta_y][block->x + j + delta_x] == '*')
{
return 1;
}
}
}
}
return 0;
}
void renderBoard(int height, int width, char board[height][width], int level, int score, int maxScore, Tetromino block, Tetromino nextBlock)
{
clearScreen();
/* render board */
moveCursor(PADDING_LEFT, PADDING_TOP);
printf("┌");
int i;
int j;
for (i = 0; i < width; i++)
printf("─");
printf("┐\n");
for (i = 0; i < height; i++)
{
moveCursor(PADDING_LEFT, PADDING_TOP + i + 1);
printf("│");
for (j = 0; j < width; j++)
{
printf("%c", board[i][j]);
}
printf("│\n");
}
moveCursor(PADDING_LEFT, PADDING_TOP + height + 1);
printf("└");
for (i = 0; i < width; i++)
printf("─");
printf("┘\n");
/* render shadow of the block */
Tetromino shadow = block;
while (!collisionCheck(&shadow, height, width, board, 0, 1))
shadow.y++;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (shadow.shape[i][j] == '*' && board[shadow.y + i][shadow.x + j] != '*' && shadow.y != block.y)
{
moveCursor(PADDING_LEFT + shadow.x + j + 1, PADDING_TOP + shadow.y + i + 1);
printf("◌");
}
}
}
/* render block */
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (block.shape[i][j] == '*')
{
moveCursor(PADDING_LEFT + block.x + j + 1, PADDING_TOP + block.y + i + 1);
printf("*");
}
}
}
/* render next block */
moveCursor(PADDING_LEFT + width + 4, PADDING_TOP + 1);
printf("Next block:");
moveCursor(PADDING_LEFT + width + 3, PADDING_TOP + 2);
printf("┌");
for (i = 0; i < 4; i++)
printf("─");
printf("┐\n");
for (i = 1; i < 3; i++)
{
moveCursor(PADDING_LEFT + width + 3, PADDING_TOP + 2 + i);
printf("│");
for (j = 0; j < 4; j++)
{
if (nextBlock.shape[i][j] == '*')
{
printf("*");
}
else
{
printf(" ");
}
}
printf("│\n");
}
moveCursor(PADDING_LEFT + width + 3, PADDING_TOP + 5);
printf("└");
for (i = 0; i < 4; i++)
printf("─");
printf("┘\n");
/* render level */
moveCursor(PADDING_LEFT + width + 4, PADDING_TOP + 7);
printf("Level: %d", level);
/* render score */
moveCursor(PADDING_LEFT + width + 4, PADDING_TOP + 8);
printf("Max score: %d", maxScore);
moveCursor(PADDING_LEFT + width + 4, PADDING_TOP + 9);
printf("Score: %d", score);
/* render controls */
moveCursor(PADDING_LEFT + width + 4, PADDING_TOP + 11);
printf("Controls:");
moveCursor(PADDING_LEFT + width + 4, PADDING_TOP + 12);
printf("←a →d ↑w ↓space Xq");
moveCursor(0, 0);
fflush(stdout);
}
void rotateBlock(Tetromino *block, int height, int width, char board[height][width])
{
/* rotates the tetromino */
int rotation = block->rotation;
rotation++;
rotation %= 4;
block->rotation = rotation;
char temp[4][4];
int i;
for (i = 0; i < 4; i++)
{
temp[i][0] = block->shape[3][i];
temp[i][1] = block->shape[2][i];
temp[i][2] = block->shape[1][i];
temp[i][3] = block->shape[0][i];
}
for (i = 0; i < 4; i++)
{
block->shape[i][0] = temp[i][0];
block->shape[i][1] = temp[i][1];
block->shape[i][2] = temp[i][2];
block->shape[i][3] = temp[i][3];
}
if (!collisionCheck(block, height, width, board, 0, 0))
{
return;
}
if (!collisionCheck(block, height, width, board, -1, 0))
{
block->x--;
return;
}
if (!collisionCheck(block, height, width, board, 1, 0))
{
block->x++;
return;
}
if (!collisionCheck(block, height, width, board, 0, -1))
{
block->y--;
return;
}
if (!collisionCheck(block, height, width, board, -2, 0))
{
block->x -= 2;
return;
}
if (!collisionCheck(block, height, width, board, 2, 0))
{
block->x += 2;
return;
}
}
int main()
{
int isGameRunning = 1;
int score = 0;
int maxScore = 0;
int level = 1;
int height;
int width;
/* set random seed */
srand(time(NULL));
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
char banner[] = {
" _____ _ _ ____\n"
"|_ _|__| |_ _ __(_)___ / ___| __ _ _ __ ___ ___\n"
" | |/ _ \\ __| '__| / __| | | _ / _` | '_ ` _ \\ / _ \\\n"
" | | __/ |_| | | \\__ \\ | |_| | (_| | | | | | | __/\n"
" |_|\\___|\\__|_| |_|___/ \\____|\\__,_|_| |_| |_|\\___|\n"
" 1- Scores: byEnsarGok\n"
" - 100 points for 1 line\n"
" - 250 points for 2 lines\n"
" - 500 points for 3 lines\n"
" - 1000 points for 4 lines\n"
" 2- Controls:\n"
" - ←a Move left\n"
" - →d Move right\n"
" - ↑w Rotate block\n"
" - ↓space Drop\n"
" - Xq Quit\n"
" 3- Score 1000 points to level up\n"
" 4- Press q to quit (eg. 15x10)\n"
" Enter height and width to start: "
};
printf("%s", banner);
scanf("%dx%d", &height, &width);
Tetromino tetrominos[7] =
{
{
.shape = {
{' ', ' ', ' ', ' '},
{'*', '*', '*', '*'},
{' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' '}
},
.x = 0,
.y = 0,
.rotation = 0
},
{
.shape = {
{' ', ' ', ' ', ' '},
{'*', ' ', ' ', ' '},
{'*', '*', '*', ' '},
{' ', ' ', ' ', ' '}
},
.x = 0,
.y = 0,
.rotation = 0
},
{
.shape = {
{' ', ' ', ' ', ' '},
{' ', ' ', '*', ' '},
{'*', '*', '*', ' '},
{' ', ' ', ' ', ' '}
},
.x = 0,
.y = 0,
.rotation = 0
},
{
.shape = {
{' ', ' ', ' ', ' '},
{' ', '*', '*', ' '},
{' ', '*', '*', ' '},
{' ', ' ', ' ', ' '}
},
.x = 0,
.y = 0,
.rotation = 0
},
{
.shape = {
{' ', ' ', ' ', ' '},
{'*', '*', '*', ' '},
{' ', '*', ' ', ' '},
{' ', ' ', ' ', ' '}
},
.x = 0,
.y = 0,
.rotation = 0
},
{
.shape = {
{' ', ' ', ' ', ' '},
{' ', '*', '*', ' '},
{'*', '*', ' ', ' '},
{' ', ' ', ' ', ' '}
},
.x = 0,
.y = 0,
.rotation = 0
},
{
.shape = {
{' ', ' ', ' ', ' '},
{' ', '*', '*', ' '},
{' ', ' ', '*', '*'},
{' ', ' ', ' ', ' '}
},
.x = 0,
.y = 0,
.rotation = 0
}
};
int isRowFull[height];
int i;
for (i = 0; i < height; i++)
isRowFull[i] = 0;
char board[height][width];
int j;
for (i = 0; i < height; i++)
for (j = 0; j < width; j++)
board[i][j] = ' ';
Tetromino block = getRandomBlock(tetrominos, width);
Tetromino nextBlock = getRandomBlock(tetrominos, width);
int x = block.x;
int y = block.y;
int rotation = block.rotation;
renderBoard(height, width, board, level, score, maxScore, block, nextBlock);
pthread_t keyThread;
pthread_create(&keyThread, NULL, (void*)&asyncKeyThread, (void*)&isGameRunning);
int framesPassed = 0;
int quit = 0;
while(!quit)
{
while (isGameRunning)
{
/* sleep the thread for 1/60 seconds */
sleepMs(1000 / 60);
/* if enough frame has passed, drop the block */
if (framesPassed >= 60 / level)
{
framesPassed = 0;
if (!collisionCheck(&block, height, width, board, 0, 1))
block.y++;
}
else
{
framesPassed++;
}
char ch;
ch = getAsyncKey();
switch (ch)
{
case 'a':
if (!collisionCheck(&block, height, width, board, -1, 0))
block.x--;
break;
case 'd':
if (!collisionCheck(&block, height, width, board, 1, 0))
block.x++;
break;
case 'w':
rotateBlock(&block, height, width, board);
break;
case 'q':
isGameRunning = 0;
break;
case ' ':
while (!collisionCheck(&block, height, width, board, 0, 1))
{
block.y++;
}
break;
default:
break;
}
/* if block is on the ground */
if (collisionCheck(&block, height, width, board, 0, 1))
{
if (ch_last)
{
switch (ch_last)
{
case 'a':
if (!collisionCheck(&block, height, width, board, -1, 0))
block.x--;
break;
case 'd':
if (!collisionCheck(&block, height, width, board, 1, 0))
block.x++;
break;
case 'w':
rotateBlock(&block, height, width, board);
break;
case 'q':
isGameRunning = 0;
break;
}
ch_last = 0;
}
/* add block to board */
for (i = 0; i < 4; i++)
{
for (j = 0; j< 4; j++)
{
if (block.shape[i][j] == '*')
{
board[block.y + i][block.x + j] = '*';
}
}
}
block = nextBlock;
/* if next block collapses, game over */
if (collisionCheck(&block, height, width, board, 0, 0))
{
isGameRunning = 0;
}
nextBlock = getRandomBlock(tetrominos, width);
}
/* check if a row is full */
int fullRowCount = 0;
int i;
int j;
int k;
for (i = 0; i < height; i++)
{
int is_full = 1;
for (j = 0; j < width; j++)
{
if (board[i][j] != '*')
{
is_full = 0;
/* break; */
}
}
if (is_full)
{
isRowFull[i] = 1;
fullRowCount++;
/* move all rows above down */
for (k = i; k > 0; k--)
{
for (j = 0; j < width; j++)
{
board[k][j] = board[k - 1][j];
}
}
}
}
switch (fullRowCount)
{
case 1:
score += 100;
break;
case 2:
score += 250;
break;
case 3:
score += 500;
break;
case 4:
score += 1000;
break;
default:
break;
}
if (score >= level * 1000)
{
level++;
}
/* check if top row is full */
for (j = 0; j < width; j++)
{
if (board[0][j] == '*')
{
isGameRunning = 0;
}
}
/* RENDER */
if (block.y != y || block.x != x || block.rotation != rotation)
{
renderBoard(height, width, board, level, score, maxScore, block, nextBlock);
x = block.x;
y = block.y;
rotation = block.rotation;
}
}
/* game over */
if (score > maxScore)
maxScore = score;
moveCursor(PADDING_LEFT + width + 4, PADDING_TOP + 11);
printf("Game over!");
moveCursor(PADDING_LEFT + width + 4, PADDING_TOP + 12);
printf(" ");
moveCursor(PADDING_LEFT + width + 4, PADDING_TOP + 12);
printf("Play again? (y/n): ");
char ch;
fflush(stdin);
scanf("%c", &ch);
if (ch == 'y')
{
isGameRunning = 1;
score = 0;
level = 1;
for (i = 0; i < height; i++)
for (j = 0; j < width; j++)
board[i][j] = ' ';
for (i = 0; i < height; i++)
isRowFull[i] = 0;
block = getRandomBlock(tetrominos, width);
nextBlock = getRandomBlock(tetrominos, width);
}
else
{
quit = 1;
}
}
#ifndef _WIN32
system("stty cooked");
system("stty echo");
#endif
/* press any key to exit */
moveCursor(0, PADDING_TOP + height + 2);
printf("Press enter to continue...");
clean_stdin();
getchar();
return 0;
}