-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.cpp
276 lines (227 loc) · 8.4 KB
/
tetris.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
//
// The following code is a derivative work of the code from OneLoneCoder.com - Command Line Tetris,
// which is licensed GPLv3. This code therefore is also licensed under the terms
// of the GNU Public License, verison 3.
//
#include <iostream>
#include <ncurses.h>
#include <thread>
#include <vector>
#include <cstdio>
std::string tetromino[7];
int nFieldWidth = 12;
int nFieldHeight = 18;
unsigned char *pField = nullptr;
unsigned char *screen = nullptr;
int nScreenWidth = 80;
int nScreenHeight = 30;
WINDOW *win;
int Rotate(int px, int py, int r)
{
switch (r % 4)
{
case 0: return py * 4 + px; // 0 degrees
case 1: return 12 + py - (px * 4); // 90 degrees
case 2: return 15 - (py * 4) - px; // 180 degrees
case 3: return 3 - py + (px * 4); // 270 degrees
}
return 0;
}
bool DoesPieceFit(int nTetromino, int nRotation, int nPosX, int nPosY)
{
for (int px = 0; px < 4; px++)
for (int py = 0; py <4; py++ )
{
// get index into piece
int pi = Rotate(px, py, nRotation);
// get index into field
int fi = (nPosY + py) * nFieldWidth + (nPosX + px);
if (nPosX + px >= 0 && nPosX + px < nFieldWidth)
{
if (nPosY + py >= 0 && nPosY + py < nFieldHeight)
{
if (tetromino[nTetromino][pi] == 'X' && pField[fi] != 0)
return false;
}
}
}
return true;
}
WINDOW* InitBoard(){
WINDOW *w;
w = newwin(nScreenHeight, nScreenWidth, 0, 0);
if (w == NULL) {
addstr("unable to create window");
refresh();
return w;
}
// box(w,'|','=');
noecho();
return w;
}
void DisplayFrame(){
for (int y = 1; y < nScreenHeight; y++)
for (int x = 1; x < nScreenWidth; x++)
mvwaddch(win,y,x,screen[y * nScreenWidth + x]);
wrefresh(win);
}
int main() {
initscr();
// Create assets
tetromino[0].append("..X.");
tetromino[0].append("..X.");
tetromino[0].append("..X.");
tetromino[0].append("..X.");
tetromino[1].append("..X.");
tetromino[1].append(".XX.");
tetromino[1].append(".X..");
tetromino[1].append("....");
tetromino[2].append(".X..");
tetromino[2].append(".XX.");
tetromino[2].append("..X.");
tetromino[2].append("....");
tetromino[3].append("....");
tetromino[3].append(".XX.");
tetromino[3].append(".XX.");
tetromino[3].append("....");
tetromino[4].append("..X.");
tetromino[4].append(".XX.");
tetromino[4].append("..X.");
tetromino[4].append("....");
tetromino[5].append("....");
tetromino[5].append(".XX.");
tetromino[5].append("..X.");
tetromino[5].append("..X.");
tetromino[6].append("....");
tetromino[6].append(".XX.");
tetromino[6].append(".X..");
tetromino[6].append(".X..");
screen = new unsigned char [nScreenWidth * nScreenHeight];
for (int i = 0; i < nScreenWidth * nScreenHeight; i++)
screen[i] = ' ';
pField = new unsigned char [nFieldWidth * nFieldHeight]; // Create board
for (int x = 0; x < nFieldWidth; x++) // board boundary
for (int y = 0; y < nFieldHeight; y++)
pField[y * nFieldWidth + x] = (x == 0 || x == nFieldWidth - 1 || y == nFieldHeight - 1) ? 9 : 0;
// game logic stuff
bool bGameOver = false;
int ch;
int nCurrentPiece = 0;
int nCurrentRotation = 0;
int nCurrentX = nFieldWidth / 2;
int nCurrentY = 0;
int nSpeed = 20;
int nSpeedCounter = 0;
bool bForceDown = false;
int nPieceCount = 0;
int nScore = 0;
std::vector<int> vLines;
win = InitBoard();
keypad(win, TRUE);
while ( !bGameOver)
{
// Game timing =====================================================
std::this_thread::sleep_for(std::chrono::milliseconds(50));
nSpeedCounter++;
bForceDown = (nSpeedCounter == nSpeed);
// INPUT ==========================================================
nodelay(win,TRUE);
ch = wgetch(win);
// Game logic =====================================================
switch(ch){
case KEY_DOWN:
if (DoesPieceFit(nCurrentPiece, nCurrentRotation, nCurrentX, nCurrentY + 1))
nCurrentY++;
break;
case KEY_LEFT:
if ( DoesPieceFit(nCurrentPiece, nCurrentRotation, nCurrentX - 1, nCurrentY))
nCurrentX--;
break;
case KEY_RIGHT:
if ( DoesPieceFit(nCurrentPiece, nCurrentRotation, nCurrentX + 1, nCurrentY))
nCurrentX++;
break;
case '\n':
if ( DoesPieceFit(nCurrentPiece, nCurrentRotation + 1, nCurrentX, nCurrentY))
nCurrentRotation++;
break;
case 'q':
bGameOver = true;
break;
}
if (bForceDown)
{
nSpeedCounter = 0;
if (DoesPieceFit(nCurrentPiece, nCurrentRotation, nCurrentX, nCurrentY + 1))
nCurrentY++;
else {
// Lock the current piece in the field
for (int px = 0; px < 4; px++)
for (int py = 0; py < 4; py++ )
if (tetromino[nCurrentPiece][Rotate(px, py, nCurrentRotation)] != '.')
pField[(nCurrentY + py) * nFieldWidth + (nCurrentX + px)] = nCurrentPiece + 1;
nPieceCount++;
if (nPieceCount % 10 == 0)
if (nSpeed >= 10)
nSpeed--;
// Check we have got any lines
for (int py = 0; py < 4; py++)
if (nCurrentY + py < nFieldHeight - 1)
{
bool bLine = true;
for (int px = 1; px < nFieldWidth - 1; px++)
bLine &= (pField[(nCurrentY + py) * nFieldWidth + px]) != 0;
if (bLine)
{
// Remove line set to =
for (int px = 1; px < nFieldWidth - 1; px++)
pField[(nCurrentY + py) * nFieldWidth + px] = 8;
vLines.push_back(nCurrentY + py);
}
}
nScore+=25;
if (!vLines.empty())
nScore+=(1 << vLines.size()) * 100;
// Choose next piece
nCurrentX = nFieldWidth / 2;
nCurrentY = 0;
nCurrentRotation = 0;
nCurrentPiece = rand() % 7;
// if piece do not fit
bGameOver = !DoesPieceFit(nCurrentPiece, nCurrentRotation, nCurrentX, nCurrentY);
}
}
// Draw field ===============================================
for (int y = 0; y < nFieldHeight; y++)
for (int x = 0; x < nFieldWidth; x++)
screen[(y + 2) * nScreenWidth + (x + 2)] = " ABCDEFG=#"[pField[y * nFieldWidth + x]];
// Draw current piece
for (int px = 0; px < 4; px++)
for (int py = 0; py < 4; py++ )
if (tetromino[nCurrentPiece][Rotate(px, py, nCurrentRotation)] == 'X')
screen[(nCurrentY + py + 2) * nScreenWidth + (nCurrentX + px + 2)] = nCurrentPiece + 65;
// Draw score
sprintf((char *)screen+(2 * nScreenWidth + nFieldWidth + 6), "SCORE: %d", nScore);
if (!vLines.empty())
{
DisplayFrame();
std::this_thread::sleep_for(std::chrono::milliseconds(400));
for (auto &v : vLines)
for (int px = 1; px < nFieldWidth - 1; px++)
{
for (int py = v; py > 0; py--)
pField[py * nFieldWidth + px] = pField[(py - 1) * nFieldWidth + px];
pField[px] = 0;
}
vLines.clear();
}
// Display frame
DisplayFrame();
}
mvwprintw(win,1,18,"*** GAME OVER ***");
mvwprintw(win,2,18,"SCORE: %d", nScore);
nodelay(win,FALSE);
ch = wgetch(win);
endwin();
return 0;
};