-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
413 lines (364 loc) · 10.8 KB
/
main.cpp
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
// main.cpp
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <iostream>
using namespace std;
#include "Config.h"
#include "Window.h"
#include "Board.h"
#include "Tetromino.h"
// Declare termios attributes
struct itimerval tv;
// Game state variables
bool isGameOver;
bool isPieceActive;
bool pcFlag;
int rotState;
int holding;
// Holds the current piece and swaps the current piece with the held piece
int Hold(int holding, Board* field, Board* currentPiece)
{
// Find active piece ID and wipe currentPiece
int active;
for (int yPos = 0; yPos < F_HEIGHT+2; yPos++) {
for (int xPos = 0; xPos < F_WIDTH; xPos++) {
// Get ID value (not pivot)
if (currentPiece->Get(yPos,xPos) != 0) {
if (currentPiece->Get(yPos,xPos) > 7) {
active = currentPiece->Get(yPos,xPos)-5;
} else {
active = currentPiece->Get(yPos,xPos);
}
}
// Delete cell
currentPiece->Set(yPos,xPos,0);
}
}
// Check for existing piece in hold
if (holding == 9) {
// No piece in hold, get new piece
isPieceActive = false;
} else {
// Existing piece in hold, get held piece
Tetromino::GetNewPiece(holding-1, field, currentPiece);
}
rotState = 0;
pcFlag = false;
return active;
}
// Prints the current frame
void Update(Board* field, Board* currentPiece, int* bag1, int* bag2, int order, int level, int score, int lines, int combo)
{
// Top indent
Window::Format(0);
cout << endl;
// TODO: This is disgusting, but it works
for (int yPos = 0; yPos < F_HEIGHT+2; yPos++) {
// Side indent
Window::Format(0);
cout << " ";
for (int xPos = 0; xPos < F_WIDTH+8; xPos++) {
// Default formatting
Window::Format(0);
if (xPos == 0 || xPos == F_WIDTH+1) {
// Render field walls
Window::Format(7);
cout << " ";
} else if ((bag1[order] < 2) && (xPos == F_WIDTH+2 && yPos >= 2 && yPos < 4)) {
// Center preview I, O pieces
Window::Format(0);
cout << " ";
} else if ((bag1[order] >= 2) && (xPos == F_WIDTH+2 && yPos >= 2 && yPos < 4)) {
// Center preview J, L, S, T, Z pieces
Window::Format(0);
cout << " ";
} else if ((holding-1 < 2) && (xPos == F_WIDTH+2 && yPos >= 7 && yPos < 9)) {
// Center hold I, O pieces
Window::Format(0);
cout << " ";
} else if ((holding-1 >= 2) && (xPos == F_WIDTH+2 && yPos >= 7 && yPos < 9)) {
// Center hold J, L, S, T, Z pieces
Window::Format(0);
cout << " ";
} else if (yPos == 12 && xPos == F_WIDTH+3) {
// Line clear display
switch (lines) {
case 2: {cout << " DOUBLE "; break;}
case 3: {cout << " TRIPLE "; break;}
case 4: {cout << " TETRIS!"; break;}
case 8: {cout << " PERFECT"; break;}
}
} else if (yPos == 13 && xPos == F_WIDTH+3) {
switch (lines) {
case 8: {cout << " CLEAR "; break;}
}
} else if (yPos == 15 && xPos == F_WIDTH+3) {
if (combo > 1)
cout << " x" << combo;
} else if (yPos == 17 && xPos == F_WIDTH+3) {
cout << "LEVEL " << level;
} else if (yPos == 19 && xPos == F_WIDTH+3) {
cout << "SCORE: ";
} else if (yPos == 20 && xPos == F_WIDTH+3) {
cout << score;
} else if ((yPos == 0 && xPos < F_WIDTH) || xPos == F_WIDTH+2) {
// Render field top and vertical divider
Window::Format(0);
cout << " ";
} else if ((yPos == F_HEIGHT+1 && xPos < F_WIDTH+1) ||
(yPos == 0 && xPos > F_WIDTH+2) ||
(yPos == F_HEIGHT-15 && xPos > F_WIDTH+2) ||
(yPos == F_HEIGHT-10 && xPos > F_WIDTH+2)) {
// Render field bottom and horizontal dividers
Window::Format(7);
cout << " ";
} else if (xPos >= F_WIDTH+3 && xPos < F_WIDTH+7 && yPos >= 2 && yPos < 4) {
// Render preview
Tetromino::GetPieceColor(tetromino[bag1[order]][yPos-2][xPos-(F_WIDTH+3)]);
cout << " ";
} else if (xPos >= F_WIDTH+3 && xPos < F_WIDTH+7 && yPos >= 7 && yPos < 9 && holding != 9) {
// Render hold
Tetromino::GetPieceColor(tetromino[holding-1][yPos-7][xPos-(F_WIDTH+3)]);
cout << " ";
} else if (currentPiece->Get(yPos-1,xPos-1) != 0) {
// Render current piece
Tetromino::GetPieceColor(currentPiece->Get(yPos-1,xPos-1));
cout << " ";
} else {
// Render field
Tetromino::GetPieceColor(field->Get(yPos-1,xPos-1));
cout << " ";
}
}
Window::Format(0);
cout << endl;
}
}
// Keypress event handler
int GetKeyInput(Board* field, Board* currentPiece)
{
int keyPress = getchar();
switch(keyPress)
{
case KEY_LEFT: {Tetromino::MovePiece(0,1, field, currentPiece); break;}
case KEY_RIGHT: {Tetromino::MovePiece(0,-1, field, currentPiece); break;}
case KEY_DOWN: {Tetromino::MovePiece(-1,0, field, currentPiece); break;}
case KEY_UP:
case KEY_ROTATE_CW: {rotState = Tetromino::Rotate(1, rotState, field, currentPiece); break;}
case KEY_ROTATE_CCW: {rotState = Tetromino::Rotate(-1, rotState, field, currentPiece); break;}
case KEY_ROTATE_180: {
rotState = Tetromino::Rotate(1, rotState, field, currentPiece);
rotState = Tetromino::Rotate(1, rotState, field, currentPiece);
break;
}
case KEY_HARD_DROP: {Tetromino::HardDrop(field, currentPiece); break;}
case KEY_HOLD: {holding = Hold(holding, field, currentPiece); break;}
case KEY_EXIT: {isGameOver = true; break;}
}
return keyPress;
}
// Executes on exit
void Quit(int score, struct termios oldAttr)
{
Window::ShowCursor(true);
tcsetattr(0, TCSANOW, &oldAttr);
cout << endl << " Game over! Your score: " << score << endl << endl;
return;
}
// SIGINT handler, code from: https://stackoverflow.com/questions/1641182/how-can-i-catch-a-ctrl-c-event
void SignalHandler(int signal)
{
switch(signal) {
case SIGTERM:
case SIGINT:
case SIGSEGV:
isGameOver = true;
break;
case SIGALRM:
// Increase game speed by 0.1% every tick
tv.it_value.tv_usec -= tv.it_value.tv_usec / 10000;
setitimer(0, &tv, NULL);
break;
}
}
int main(int argc, char* argv[])
{
// Initialise signal handler
struct sigaction siga;
sigemptyset(&siga.sa_mask);
siga.sa_flags = 0;
siga.sa_handler = SignalHandler;
sigaction(SIGALRM, &siga, NULL);
sigaction(SIGTERM, &siga, NULL);
sigaction(SIGINT, &siga, NULL);
sigaction(SIGSEGV, &siga, NULL);
// Dummy SIGALRM handler to interrupt getchar() call
// https://stackoverflow.com/questions/5322143/add-a-timeout-for-getchar
tv.it_value.tv_usec = FRAME_USEC;
SignalHandler(SIGALRM);
// Initialise terminal attributes
struct termios oldAttr;
struct termios newAttr;
tcgetattr(STDIN_FILENO, &oldAttr);
newAttr = oldAttr;
newAttr.c_lflag &= ~(ICANON|ECHO);
newAttr.c_cc[VTIME] = 0;
newAttr.c_cc[VMIN] = 0;
tcsetattr(0, TCSANOW, &newAttr);
Window::ShowCursor(false);
// Seed rand()
srand(time(NULL));
// Initialise board
Board* field = new Board(0);
Board* currentPiece = new Board(1);
// Initialise game state
int debugTick = 0, moveTick = 0, holdTick = 0, lockTick = 0, textDelay = 0;
int score = 0, level = 1, currentLines = 0, linesCleared = 0, lastClear = 0, combo = 0;
int bagID = 0;
int *bag1 = Board::GenerateBag();
int *bag2 = Board::GenerateBag();
holding = 9;
pcFlag = false;
// Clear terminal output
cout << "\033[2J";
// Game loop
while (!isGameOver)
{
// Game tick (1000000 = 1s)
debugTick++;
moveTick++;
textDelay++;
// Game logic
// Refresh game states
isPieceActive = false;
for (int yPos = 0; yPos < F_HEIGHT+2; yPos++) {
for (int xPos = 0; xPos < F_WIDTH; xPos++) {
if (currentPiece->Get(yPos,xPos) != 0) {
isPieceActive = true;
}
}
}
// Generate new piece if no piece is active
if (!isPieceActive && bagID < 7) {
// Game over if no piece can spawn
isGameOver = Tetromino::GetNewPiece(bag1[bagID], field, currentPiece);
isPieceActive = true;
rotState = 0;
// Get lines cleared and calculate score
linesCleared = Board::ClearLine(field, pcFlag);
// Make perfect clear available
pcFlag = true;
currentLines += linesCleared;
lastClear = linesCleared;
switch (linesCleared) {
case 1: {score += 80*(level+combo); break;} // Single
case 2: {score += 200*(level+combo); break;} // Double
case 3: {score += 600*(level+combo); break;} // Triple
case 4: {score += 2400*(level+combo); break;} // Tetris
case 8: {score += 7600*(level+combo); break;} // Perfect clear
}
// Calculate combo
if (lastClear > 0 && linesCleared > 0){
combo++;
} else {
combo = 0;
}
// Change level every 20 lines
if (currentLines > 20) {
level++;
// Speed up by 5%
tv.it_value.tv_usec -= tv.it_value.tv_usec / 20;
currentLines = 0;
}
// Next piece
bagID++;
}
// Generate new bag on end of bag sequence
if (bagID == 7) {
// Swap bag
delete bag1;
int *bag1 = new int[7];
for (int i = 0; i < 7; i++)
bag1[i] = bag2[i];
// Get new bag
delete bag2;
int *bag2 = Board::GenerateBag();
bagID = 0;
}
// Update
cout << "\033[H";
Update(field, currentPiece, bag1, bag2, bagID, level, score, lastClear, combo);
// Input handler
int key = GetKeyInput(field, currentPiece);
// Gravity
if (moveTick == 50) {
(Tetromino::MovePiece(-1,0, field, currentPiece)) ? lockTick = 0 : lockTick++;
if (lockTick == 2)
Tetromino::SetPiece(field, currentPiece);
moveTick = 0;
}
// Line clear text timeout
if (textDelay == 250) {
lastClear = 0;
textDelay = 0;
}
// Debug info
if (argv[1]) {
cout << endl << " ----- DEBUG INFO -----" << endl;
cout << " Tick: " << debugTick << endl;
for (int y = 0; y < F_HEIGHT; y++) {
cout << " ";
// Field values
for (int x = 0; x < F_WIDTH; x++) {
if (field->Get(y,x) > 7) {
cout << field->Get(y,x)-5;
} else {
cout << field->Get(y,x);
}
}
cout << " ";
// currentPiece values
for (int x = 0; x < F_WIDTH; x++) {
if (currentPiece->Get(y,x) > 7) {
cout << currentPiece->Get(y,x)-5;
} else {
cout << currentPiece->Get(y,x);
}
}
cout << endl;
}
cout << endl << " B1 sequence: ";
for (int i = 0; i < 7; i++) {
cout << bag1[i];
}
cout << endl << " B2 sequence: ";
for (int i = 0; i < 7; i++) {
cout << bag2[i];
}
cout << endl;
cout << " Keypress: " << key << endl;
cout << " rotState: " << rotState << endl;
cout << " holding: " << holding << endl;
cout << " holdTick: " << holdTick << endl;
cout << " pcFlag: " << pcFlag << endl;
cout << " level: " << level << endl;
} else {
cout << endl << " How to play:" << endl;
cout << " [←] Move left [→] Move right" << endl;
cout << " [↓] Soft drop [_] Hard drop" << endl;
cout << " [Z] Rotate CCW [X] Rotate CW" << endl;
cout << " [A] Rotate 180° [C] Hold" << endl;
cout << " [Q] Quit game" << endl << endl;
cout << " < Tetris++ by samillwong >" << endl;
cout << " < https://github.com/SamillWong >" << endl;
}
}
delete bag1;
delete bag2;
delete field;
delete currentPiece;
Quit(score, oldAttr);
return 0;
}