-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris.h
281 lines (233 loc) · 6.36 KB
/
tetris.h
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
#ifndef __TETRIS_H
#define __TETRIS_H
#include <stdlib.h>
#include <inttypes.h>
#include "graphics.h"
#include "timer.h"
#define TETROMINO_COUNT 7
#define PILE_HIDDEN_ROWS 3
#define uint4_pack(a, b) (a << 4 | (b & 0b1111))
#define uint4_left(i) (i >> 4)
#define uint4_right(i) (i & 0b1111)
#define int3_enc(i) (i < 0 ? 0b1000 | (-i & 0b111) : i & 0b0111)
#define int3_dec(i) ((i & 0b1000 ? -1 : 1) * (i & 0b111))
#define srsOffset(x, y) uint4_pack(int3_enc(x), int3_enc(y))
#define srsOffsetX(i) int3_dec(uint4_left(i))
#define srsOffsetY(i) int3_dec(uint4_right(i))
#define minoCount(tetrominoWidth, tetrominoHeight) (tetrominoWidth * tetrominoHeight)
#define minoX(tetrominoX, tetrominoWidth, minoIdx) (tetrominoX + minoIdx % tetrominoWidth)
#define minoY(tetrominoY, tetrominoWidth, minoIdx) (tetrominoY + minoIdx / tetrominoWidth)
enum TetrisEvent {
LevelUp, RowsCompleted, GameOver
};
typedef void (*tetrisListener) (TetrisEvent event, uint8_t data);
class Tetromino {
public:
enum Type {
I, J, L, O, S, T, Z, _
};
enum Rotation {
Rot0, RotR, Rot2, RotL
};
static uint8_t* colorOf(Type type);
Type type;
Rotation rotation;
int8_t x;
int8_t y;
Tetromino();
uint8_t* getColor();
uint8_t getWidth();
uint8_t getHeight();
bool isMino(uint8_t idx);
uint8_t getSRSOffsetCount();
uint8_t getSRSOffset(Rotation rotation, uint8_t idx);
void spawn(Type type);
void draw(canvas canvas, uint8_t* color);
private:
static constexpr uint8_t _srsOffsets[] = {
// J, L, S, T, Z
5, // number of offsets per rotation, this is index 0
/* Rot0 */ srsOffset(0, 0), srsOffset(0, 0), srsOffset(0, 0), srsOffset(0, 0), srsOffset(0, 0),
/* RotR */ srsOffset(0, 0), srsOffset(1, 0), srsOffset(1, -1), srsOffset(0, 2), srsOffset(1, 2),
/* Rot2 */ srsOffset(0, 0), srsOffset(0, 0), srsOffset(0, 0), srsOffset(0, 0), srsOffset(0, 0),
/* RotL */ srsOffset(0, 0), srsOffset(-1, 0), srsOffset(-1, -1), srsOffset(0, 2), srsOffset(-1, 2),
// I
5, // number of offsets per rotation, this is index 21
/* Rot0 */ srsOffset(0, 0), srsOffset(-1, 0), srsOffset(2, 0), srsOffset(-1, 0), srsOffset(2, 0),
/* RotR */ srsOffset(-1, 0), srsOffset(0, 0), srsOffset(0, 0), srsOffset(0, 1), srsOffset(0, -2),
/* Rot2 */ srsOffset(-1, 1), srsOffset(1, 1), srsOffset(-2, 1), srsOffset(1, 0), srsOffset(-2, 0),
/* RotL */ srsOffset(0, 1), srsOffset(0, 1), srsOffset(0, 1), srsOffset(0, -1), srsOffset(0, 2),
// O
1, // number of offsets per rotation, this is index 42
/* Rot0 */ srsOffset(0, 0),
/* RotR */ srsOffset(0, -1),
/* Rot2 */ srsOffset(-1, -1),
/* RotL */ srsOffset(-1, 0)
};
struct _Data {
uint8_t color[3]; // r, g, b
uint8_t dimensions; // first 4 bits are width, last 4 bits are height
uint8_t spawnCoords; // first 4 bits are x, last 4 bits are -y
uint8_t minos[4][3]; // indexed by rotation, 3 bytes of data, left to right, top to bottom
uint8_t srsOffsets; // index within srsOffsets array
};
static constexpr _Data _data[] {
{ // I
{ 0, 255, 160 },
uint4_pack(5, 5),
uint4_pack(2, 3),
{
{ 0b00000000, 0b00011110, 0b00000000 },
{ 0b00000001, 0b00001000, 0b01000010 },
{ 0b00000000, 0b00111100, 0b00000000 },
{ 0b00100001, 0b00001000, 0b01000000 }
},
21
},
{ // J
{ 0, 0, 255 },
uint4_pack(3, 3),
uint4_pack(3, 2),
{
{ 0b10011100, 0b00000000, 0b00000000 },
{ 0b01101001, 0b00000000, 0b00000000 },
{ 0b00011100, 0b10000000, 0b00000000 },
{ 0b01001011, 0b00000000, 0b00000000 }
},
0
},
{ // L
{ 255, 114, 0 },
uint4_pack(3, 3),
uint4_pack(3, 2),
{
{ 0b00111100, 0b00000000, 0b00000000 },
{ 0b01001001, 0b10000000, 0b00000000 },
{ 0b00011110, 0b00000000, 0b00000000 },
{ 0b11001001, 0b00000000, 0b00000000 }
},
0
},
{ // O
{ 255, 255, 0 },
uint4_pack(3, 3),
uint4_pack(3, 2),
{
{ 0b01101100, 0b00000000, 0b00000000 },
{ 0b00001101, 0b10000000, 0b00000000 },
{ 0b00011011, 0b00000000, 0b00000000 },
{ 0b11011000, 0b00000000, 0b00000000 }
},
42
},
{ // S
{ 0, 255, 0 },
uint4_pack(3, 3),
uint4_pack(3, 2),
{
{ 0b01111000, 0b00000000, 0b00000000 },
{ 0b01001100, 0b10000000, 0b00000000 },
{ 0b00001111, 0b00000000, 0b00000000 },
{ 0b10011001, 0b00000000, 0b00000000 }
},
0
},
{ // T
{ 153, 0, 255 },
uint4_pack(3, 3),
uint4_pack(3, 2),
{
{ 0b01011100, 0b00000000, 0b00000000 },
{ 0b01001101, 0b00000000, 0b00000000 },
{ 0b00011101, 0b00000000, 0b00000000 },
{ 0b01011001, 0b00000000, 0b00000000 }
},
0
},
{ // Z
// red
{ 255, 0, 0 },
uint4_pack(3, 3),
uint4_pack(3, 2),
{
{ 0b11001100, 0b00000000, 0b00000000 },
{ 0b00101101, 0b00000000, 0b00000000 },
{ 0b00011001, 0b10000000, 0b00000000 },
{ 0b01011010, 0b00000000, 0b00000000 }
},
0
}
};
};
class Pile {
public:
Pile(uint8_t width, uint8_t height);
~Pile();
bool isOccupied(uint8_t x, int8_t y);
void merge(Tetromino* tetromino);
uint8_t clearCompleteRows();
void draw(canvas canvas);
void truncate();
private:
uint8_t _width;
uint8_t _height;
uint8_t* _data;
uint8_t _cellCount();
uint8_t _memSize();
uint8_t _cell(uint8_t x, int8_t y);
Tetromino::Type _get(uint8_t x, int8_t y);
void _set(uint8_t x, int8_t y, Tetromino::Type);
};
class Bag {
public:
Bag();
Tetromino::Type peek();
Tetromino::Type pop();
void shuffle();
private:
Tetromino::Type _sequence[TETROMINO_COUNT];
uint8_t _index;
};
class Tetris {
public:
Tetris(uint8_t width, uint8_t height, tetrisListener _listener);
~Tetris();
void reset();
bool isGameOver();
bool isPaused();
void setPaused(bool paused);
uint32_t getScores();
uint16_t getRowsCompleted();
uint8_t getLevel();
Tetromino::Type preview();
bool moveLeft();
bool moveRight();
bool moveDown();
bool rotateClockWise();
bool rotateCounterClockWise();
void setClearBackground(bool clearBackground);
void setGhostEnabled(bool ghostEnabled);
void update();
void draw(canvas canvas);
private:
uint8_t _width;
uint8_t _height;
tetrisListener _listener;
Tetromino* _tetromino;
Pile* _pile;
Bag* _bag;
uint32_t _scores;
uint16_t _rowsCompleted;
uint8_t _level;
bool _gameOver;
bool _paused;
bool _clearBackground;
bool _ghostEnabled;
Timer* _updateTimer;
Timer* _ghostTimer;
bool _checkTetromino();
bool _move(int8_t x, int8_t y);
bool _rotate(Tetromino::Rotation to);
bool _setDifficulty();
};
#endif