-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtetris.cpp
More file actions
381 lines (339 loc) · 9.61 KB
/
tetris.cpp
File metadata and controls
381 lines (339 loc) · 9.61 KB
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
#include "tetris.hpp"
TetriminoKind kinds[7] = {
Tet_I,
Tet_O,
Tet_S,
Tet_Z,
Tet_J,
Tet_L,
Tet_T
};
BlockKind field[HEIGHT][WIDTH] = {};
BlockKind prevField[HEIGHT][WIDTH] = {};
struct OperateTet opTet;
struct OperateTet nextOpTet;
int i_tet[4][4][2] = {
{{0, 0},
{1, 0},
{2, 0},
{3, 0}},
{{0, 0},
{0, 1},
{0, 2},
{0, 3}},
{{0, 0},
{1, 0},
{2, 0},
{3, 0}},
{{0, 0},
{0, 1},
{0, 2},
{0, 3}},
};
int o_tet[4][4][2] = {
{{0, 0},
{1, 0},
{0, 1},
{1, 1}},
{{0, 0},
{1, 0},
{0, 1},
{1, 1}},
{{0, 0},
{1, 0},
{0, 1},
{1, 1}},
{{0, 0},
{1, 0},
{0, 1},
{1, 1}}
};
int s_tet[4][4][2] = {
{{1, 0},
{2, 0},
{0, 1},
{1, 1}},
{{0, 0},
{0, 1},
{1, 1},
{1, 2}},
{{1, 0},
{2, 0},
{0, 1},
{1, 1}},
{{0, 0},
{0, 1},
{1, 1},
{1, 2}}
};
int z_tet[4][4][2] = {
{{0, 0},
{1, 0},
{1, 1},
{2, 1}},
{{1, 0},
{0, 1},
{1, 1},
{0, 2}},
{{0, 0},
{1, 0},
{1, 1},
{2, 1}},
{{1, 0},
{0, 1},
{1, 1},
{0, 2}}
};
int j_tet[4][4][2] = {
{{0, 0},
{1, 0},
{2, 0},
{2, 1}},
{{1, 0},
{1, 1},
{1, 2},
{0, 2}},
{{0, 0},
{0, 1},
{1, 1},
{2, 1}},
{{0, 0},
{1, 0},
{0, 1},
{0, 2}}
};
int l_tet[4][4][2] = {
{{0, 0},
{1, 0},
{2, 0},
{0, 1}},
{{0, 0},
{1, 0},
{1, 1},
{1, 2}},
{{2, 0},
{0, 1},
{1, 1},
{2, 1}},
{{0, 0},
{0, 1},
{0, 2},
{1, 2}}
};
int t_tet[4][4][2] = {
{{0, 0},
{1, 0},
{2, 0},
{1, 1}},
{{1, 0},
{0, 1},
{1, 1},
{1, 2}},
{{1, 1},
{0, 2},
{1, 2},
{2, 2}},
{{2, 1},
{1, 0},
{1, 1},
{1, 2}}
};
struct Tetrimino *tetriminos(TetriminoKind kind, int rotation_id) {
struct Tetrimino *tetrimino = (struct Tetrimino *)calloc(1, sizeof(struct Tetrimino));
switch(kind) {
case Tet_I:
memcpy(tetrimino->tet, i_tet[rotation_id], sizeof(tetrimino->tet));
return tetrimino;
case Tet_O:
memcpy(tetrimino->tet, o_tet[rotation_id], sizeof(tetrimino->tet));
return tetrimino;
case Tet_S:
memcpy(tetrimino->tet, s_tet[rotation_id], sizeof(tetrimino->tet));
return tetrimino;
case Tet_Z:
memcpy(tetrimino->tet, z_tet[rotation_id], sizeof(tetrimino->tet));
return tetrimino;
case Tet_J:
memcpy(tetrimino->tet, j_tet[rotation_id], sizeof(tetrimino->tet));
return tetrimino;
case Tet_L:
memcpy(tetrimino->tet, l_tet[rotation_id], sizeof(tetrimino->tet));
return tetrimino;
case Tet_T:
memcpy(tetrimino->tet, t_tet[rotation_id], sizeof(tetrimino->tet));
return tetrimino;
default:
return tetrimino;
}
}
void init_field() {
for(int h = 0; h < HEIGHT; h++) {
for(int w = 0; w < WIDTH; w++) {
if(w == 0 || w == WIDTH-1 || h == HEIGHT-1) {
field[h][w] = Wall;
} else {
field[h][w] = Space;
}
}
}
}
void put_tetrimino(int tetrimino[4][2], int x, int y) {
for(int th=0; th < 4; th++) {
int x_ = tetrimino[th][0];
int y_ = tetrimino[th][1];
field[y+y_][x+x_] = Operating;
}
}
void print_field() {
String text = "";
M5.Lcd.setCursor(0, 0);
for(int h = 0; h < HEIGHT; h++) {
for(int w = 0; w < WIDTH; w++) {
if(field[h][w] == Block) {
text.concat("#");
// M5.Lcd.drawString("#", w*2, h*2, 2);
}
else if (field[h][w] == Operating) {
text.concat("!");
// M5.Lcd.drawString("!", w*2, h*2, 2);
} else if (field[h][w] == Wall) {
text.concat("+");
// M5.Lcd.drawString("+", w*2, h*2, 2);
} else {
text.concat("_");
// M5.Lcd.drawString("_", w*2, h*2, 2);
}
}
text.concat("\n");
}
M5.Lcd.print(text);
}
void clear_operated_tetrimino() {
for(int h = 0; h < HEIGHT; h++) {
for(int w = 0; w < WIDTH; w++) {
if(field[h][w] == Operating) {
field[h][w] = Space;
}
}
}
}
bool setable_operated_tet(struct OperateTet opTet) {
struct Tetrimino *tetrimino = tetriminos(opTet.kind, opTet.rotation_id);
int (*tet)[2] = tetrimino->tet;
int x = opTet.x;
int y = opTet.y;
if((field[y+tet[0][1]][x+tet[0][0]] == Block) ||
(field[y+tet[1][1]][x+tet[1][0]] == Block) ||
(field[y+tet[2][1]][x+tet[2][0]] == Block) ||
(field[y+tet[3][1]][x+tet[3][0]] == Block) ||
(field[y+tet[0][1]][x+tet[0][0]] == Wall) ||
(field[y+tet[1][1]][x+tet[1][0]] == Wall) ||
(field[y+tet[2][1]][x+tet[2][0]] == Wall) ||
(field[y+tet[3][1]][x+tet[3][0]] == Wall)
) {
return false;
}
return true;
}
void change_to_block(struct OperateTet opTet) {
struct Tetrimino *tetrimino = tetriminos(opTet.kind, opTet.rotation_id);
int (*tet)[2] = tetrimino->tet;
for(int th = 0; th < 4; th ++) {
int x_ = tet[th][0];
int y_ = tet[th][1];
field[opTet.y+y_][opTet.x+x_] = Block;
}
}
void move_all_block(int pivot, int step) {
for(int h = HEIGHT; h > 0; h--) {
for(int w = 0; w < WIDTH; w++) {
if(field[h][w] == Block && h < pivot) {
field[h][w] = Space;
field[h + step][w] = Block;
}
}
}
}
void delete_line(int line_num) {
for(int w = 0; w < WIDTH; w++) {
if(field[line_num][w] == Block) {
field[line_num][w] = Space;
}
}
}
bool is_full_line(int line_num) {
int ret = 0;
for(int w = 0; w < WIDTH; w++) {
if(field[line_num][w] == Block)
ret++;
}
if(ret == 10) return true;
return false;
}
void reset_game() {
for(int h = 0; h < HEIGHT; h++) {
for(int w = 0; w < WIDTH; w++) {
if(field[h][w] != Wall) {
field[h][w] = Space;
}
}
}
}
void reset_operated_tetrimino() {
opTet.x = 5;
opTet.y = 0;
opTet.kind = nextOpTet.kind;
opTet.rotation_id = 0;
int nextOpKindIdx = rand()%7;
nextOpTet.kind = kinds[nextOpKindIdx];
struct timespec ts;
ts.tv_sec = 1;
ts.tv_nsec = 0;
if(!setable_operated_tet(opTet)) {
M5.Lcd.drawString("gameover!!", 40, 20);
delay(1000);
reset_game();
}
}
// 左入力処理
void left_proc() {
opTet.x--;
if(!setable_operated_tet(opTet)) {
opTet.x++;
}
}
// 右入力処理
void right_proc() {
opTet.x++;
if(!setable_operated_tet(opTet)) {
opTet.x--;
}
}
void remove_line_proc() {
for(int h = 0; h < HEIGHT; h++) {
if(is_full_line(h)) {
delete_line(h);
move_all_block(h, 1);
}
}
}
// テトリミノの落下処理と床当たり判定処理
void fall_proc() {
opTet.y ++;
if(!setable_operated_tet(opTet)) {
opTet.y--;
change_to_block(opTet);
remove_line_proc();
reset_operated_tetrimino();
}
}
// 上入力処理
void up_proc() {
while(setable_operated_tet(opTet)) {
opTet.y++;
}
opTet.y--;
change_to_block(opTet);
remove_line_proc();
reset_operated_tetrimino();
}