forked from troglobit/tetris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.c
441 lines (363 loc) · 9.58 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
/* Micro Tetris, based on an obfuscated tetris, 1989 IOCCC Best Game
*
* Copyright (c) 1989 John Tromp <[email protected]>
* Copyright (c) 2009-2021 Joachim Wiberg <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* See the following URLs for more information, first John Tromp's page about
* the game http://homepages.cwi.nl/~tromp/tetris.html then there's the entry
* page at IOCCC http://www.ioccc.org/1989/tromp.hint
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#define clrscr() puts ("\033[2J\033[1;1H")
#define gotoxy(x,y) printf("\033[%d;%dH", y, x)
#define hidecursor() puts ("\033[?25l")
#define showcursor() puts ("\033[?25h")
#define bgcolor(c,s) printf("\033[%dm" s, c ? c + 40 : 0)
#define SIGNAL(signo, cb) \
sigemptyset(&sa.sa_mask); \
sigaddset(&sa.sa_mask, signo); \
sa.sa_flags = 0; \
sa.sa_handler = cb; \
sigaction(signo, &sa, NULL)
/* the board */
#define B_COLS 12
#define B_ROWS 23
#define B_SIZE (B_ROWS * B_COLS)
#define TL -B_COLS-1 /* top left */
#define TC -B_COLS /* top center */
#define TR -B_COLS+1 /* top right */
#define ML -1 /* middle left */
#define MR 1 /* middle right */
#define BL B_COLS-1 /* bottom left */
#define BC B_COLS /* bottom center */
#define BR B_COLS+1 /* bottom right */
/* These can be overridden by the user. */
#define DEFAULT_KEYS "jkl pq"
#define KEY_LEFT 0
#define KEY_RIGHT 2
#define KEY_ROTATE 1
#define KEY_DROP 3
#define KEY_PAUSE 4
#define KEY_QUIT 5
#define HIGH_SCORE_FILE "/var/games/tetris.scores"
#define TEMP_SCORE_FILE "/tmp/tetris-tmp.scores"
static volatile sig_atomic_t running = 1;
static struct termios savemodes;
static int havemodes = 0;
static char *keys = DEFAULT_KEYS;
static int level = 1;
static int points = 0;
static int lines_cleared = 0;
static int board[B_SIZE], shadow[B_SIZE];
static int *peek_shape; /* peek preview of next shape */
static int pcolor;
static int *shape;
static int color;
static int shapes[] = {
7, TL, TC, MR, 2, /* ""__ */
8, TR, TC, ML, 3, /* __"" */
9, ML, MR, BC, 1, /* "|" */
3, TL, TC, ML, 4, /* square */
12, ML, BL, MR, 5, /* |""" */
15, ML, BR, MR, 6, /* """| */
18, ML, MR, 2, 7, /* ---- sticks out */
0, TC, ML, BL, 2, /* / */
1, TC, MR, BR, 3, /* \ */
10, TC, MR, BC, 1, /* |- */
11, TC, ML, MR, 1, /* _|_ */
2, TC, ML, BC, 1, /* -| */
13, TC, BC, BR, 5, /* |_ */
14, TR, ML, MR, 5, /* ___| */
4, TL, TC, BC, 5, /* "| */
16, TR, TC, BC, 6, /* |" */
17, TL, MR, ML, 6, /* |___ */
5, TC, BC, BL, 6, /* _| */
6, TC, BC, 2 * B_COLS, 7, /* | sticks out */
};
static void draw(int x, int y, int c)
{
gotoxy(x, y);
bgcolor(c, " ");
}
static int update(void)
{
int x, y;
#ifdef ENABLE_PREVIEW
static int shadow_preview[B_COLS * 10] = { 0 };
int preview[B_COLS * 10] = { 0 };
const int start = 5;
preview[2 * B_COLS + 1] = pcolor;
preview[2 * B_COLS + 1 + peek_shape[1]] = pcolor;
preview[2 * B_COLS + 1 + peek_shape[2]] = pcolor;
preview[2 * B_COLS + 1 + peek_shape[3]] = pcolor;
for (y = 0; y < 4; y++) {
for (x = 0; x < B_COLS; x++) {
if (preview[y * B_COLS + x] - shadow_preview[y * B_COLS + x]) {
int c = preview[y * B_COLS + x]; /* color */
shadow_preview[y * B_COLS + x] = c;
draw(x * 2 + 26 + 28, start + y, c);
}
}
}
#endif
/* Display board. */
for (y = 1; y < B_ROWS - 1; y++) {
for (x = 0; x < B_COLS; x++) {
if (board[y * B_COLS + x] - shadow[y * B_COLS + x]) {
int c = board[y * B_COLS + x]; /* color */
shadow[y * B_COLS + x] = c;
draw(x * 2 + 28, y, c);
}
}
}
/* Update points and level */
while (lines_cleared >= 10) {
lines_cleared -= 10;
level++;
}
#ifdef ENABLE_SCORE
/* Display current level and points */
gotoxy(26 + 28, 2);
printf("\033[0mLevel : %d", level);
gotoxy(26 + 28, 3);
printf("Points : %d", points);
#endif
#ifdef ENABLE_PREVIEW
gotoxy(26 + 28, 5);
printf("Preview:");
#endif
gotoxy(26 + 28, 10);
printf("Keys:");
fflush(stdout);
return getchar();
}
/* Check if shape fits in the current position */
static int fits_in(int *s, int pos)
{
if (board[pos] || board[pos + s[1]] || board[pos + s[2]] || board[pos + s[3]])
return 0;
return 1;
}
/* place shape at pos with color */
static void place(int *s, int pos, int c)
{
board[pos] = c;
board[pos + s[1]] = c;
board[pos + s[2]] = c;
board[pos + s[3]] = c;
}
static int *next_shape(void)
{
int pos = rand() % 7 * 5;
int *next = peek_shape;
peek_shape = &shapes[pos];
pcolor = peek_shape[4];
if (!next)
return next_shape();
color = next[4];
return next;
}
static void show_high_score(void)
{
#ifdef ENABLE_HIGH_SCORE
FILE *tmpscore;
if ((tmpscore = fopen(HIGH_SCORE_FILE, "a"))) {
char *name = getenv("LOGNAME");
if (!name)
name = "anonymous";
fprintf(tmpscore, "%7d\t %5d\t %3d\t%s\n", points * level, points, level, name);
fclose(tmpscore);
system("cat " HIGH_SCORE_FILE "| sort -rn | head -10 >" TEMP_SCORE_FILE
"&& cp " TEMP_SCORE_FILE " " HIGH_SCORE_FILE);
remove(TEMP_SCORE_FILE);
}
if (!access(HIGH_SCORE_FILE, R_OK)) {
// puts("\nHit RETURN to see high scores, ^C to skip.");
fprintf(stderr, " Score\tPoints\tLevel\tName\n");
system("cat " HIGH_SCORE_FILE);
}
#endif /* ENABLE_HIGH_SCORE */
}
static void show_online_help(void)
{
const int start = 11;
gotoxy(26 + 28, start);
puts("\033[0mj - left");
gotoxy(26 + 28, start + 1);
puts("k - rotate");
gotoxy(26 + 28, start + 2);
puts("l - right");
gotoxy(26 + 28, start + 3);
puts("space - drop");
gotoxy(26 + 28, start + 4);
puts("p - pause");
gotoxy(26 + 28, start + 5);
puts("q - quit");
}
/* Code stolen from http://c-faq.com/osdep/cbreak.html */
static int tty_init(void)
{
struct termios modmodes;
if (tcgetattr(fileno(stdin), &savemodes) < 0)
return -1;
havemodes = 1;
hidecursor();
/* "stty cbreak -echo" */
modmodes = savemodes;
modmodes.c_lflag &= ~ICANON;
modmodes.c_lflag &= ~ECHO;
modmodes.c_cc[VMIN] = 1;
modmodes.c_cc[VTIME] = 0;
return tcsetattr(fileno(stdin), TCSANOW, &modmodes);
}
static int tty_exit(void)
{
if (!havemodes)
return 0;
showcursor();
/* "stty sane" */
return tcsetattr(fileno(stdin), TCSANOW, &savemodes);
}
static void freeze(int enable)
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGALRM);
sigprocmask(enable ? SIG_BLOCK : SIG_UNBLOCK, &set, NULL);
}
static void alarm_handler(int signo)
{
static long h[4];
(void)signo;
/* On init from main() */
if (!signo)
h[3] = 500000;
h[3] -= h[3] / (3000 - 10 * level);
setitimer(0, (struct itimerval *)h, 0);
}
static void exit_handler(int signo)
{
(void)signo;
running = 0;
}
static void sig_init(void)
{
struct sigaction sa;
SIGNAL(SIGINT, exit_handler);
SIGNAL(SIGTERM, exit_handler);
SIGNAL(SIGALRM, alarm_handler);
/* Start update timer. */
alarm_handler(0);
}
int main(void)
{
int c = 0, i, j, *ptr;
int pos = 17;
int *backup;
/* Initialize board, grey border, used to be white(7) */
ptr = board;
for (i = B_SIZE; i; i--)
*ptr++ = i < 25 || i % B_COLS < 2 ? 60 : 0;
srand((unsigned int)time(NULL));
if (tty_init() == -1)
return 1;
/* Set up signals */
sig_init();
clrscr();
show_online_help();
shape = next_shape();
while (running) {
if (c < 0) {
if (fits_in(shape, pos + B_COLS)) {
pos += B_COLS;
} else {
place(shape, pos, color);
++points;
for (j = 0; j < 252; j = B_COLS * (j / B_COLS + 1)) {
for (; board[++j];) {
if (j % B_COLS == 10) {
lines_cleared++;
for (; j % B_COLS; board[j--] = 0)
;
c = update();
for (; --j; board[j + B_COLS] = board[j])
;
c = update();
}
}
}
shape = next_shape();
if (!fits_in(shape, pos = 17))
c = keys[KEY_QUIT];
}
}
if (c == keys[KEY_LEFT]) {
if (!fits_in(shape, --pos))
++pos;
}
if (c == keys[KEY_ROTATE]) {
backup = shape;
shape = &shapes[5 * *shape]; /* Rotate */
/* Check if it fits, if not restore shape from backup */
if (!fits_in(shape, pos))
shape = backup;
}
if (c == keys[KEY_RIGHT]) {
if (!fits_in(shape, ++pos))
--pos;
}
if (c == keys[KEY_DROP]) {
for (; fits_in(shape, pos + B_COLS); ++points)
pos += B_COLS;
}
if (c == keys[KEY_PAUSE] || c == keys[KEY_QUIT]) {
freeze(1);
if (c == keys[KEY_QUIT]) {
clrscr();
gotoxy(0, 0);
printf("\033[0mYour score: %d points x level %d = %d\n\n", points, level, points * level);
show_high_score();
break;
}
for (j = B_SIZE; j--; shadow[j] = 0)
;
while (getchar() - keys[KEY_PAUSE])
;
// puts("\033[H\033[J\033[7m");
freeze(0);
}
place(shape, pos, color);
c = update();
place(shape, pos, 0);
}
clrscr();
if (tty_exit() == -1)
return 1;
return 0;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/