-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_1010.cpp
596 lines (531 loc) · 11.2 KB
/
game_1010.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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#include "game_1010.h"
// Game related
#define BOARD_LINES 10
#define MIN_TAIL_LENGTH 4
#define NEXT_X_OFFSET 64
#define NEXT_Y_OFFSET 40
#define NEXT_SIDE 22
#define NEXT_BUSY 128
#define SIDE_L 1
#define SIDE_R 2
#define SIDE_U 4
#define SIDE_D 8
#define FILL_EMPTY 0
#define FILL_CHECKERD 1
#define FILL_SOLID 2
static Arduboy2 *gr;
struct point_t {
int8_t x;
int8_t y;
};
struct data_t {
uint8_t gameOn;
uint32_t score;
uint32_t hiScore;
uint16_t screen[BOARD_LINES];
int8_t x;
int8_t y;
int8_t current;
uint8_t next[3];
};
static struct data_t *data;
static unsigned long button_wait_time = 0;;
#define NUM_TILES 19
static const uint8_t tiles[NUM_TILES][5] = {
// Test case all set
// {
// 0b11111,
// 0b11111,
// 0b11111,
// 0b11111,
// 0b11111
// },
// SQ1x1
{
0b00000,
0b00000,
0b00100,
0b00000,
0b00000
},
// SQ2x2
{
0b00000,
0b00000,
0b01100,
0b01100,
0b00000
},
// SQ3x3
{
0b00000,
0b01110,
0b01110,
0b01110,
0b00000
},
// D2H
{
0b00000,
0b00000,
0b01100,
0b00000,
0b00000
},
// D2V
{
0b00000,
0b00000,
0b01000,
0b01000,
0b00000
},
// D3H
{
0b00000,
0b00000,
0b01110,
0b00000,
0b00000
},
// D3V
{
0b00000,
0b01000,
0b01000,
0b01000,
0b00000
},
// D4H
{
0b00000,
0b00000,
0b01111,
0b00000,
0b00000
},
// D4V
{
0b00000,
0b01000,
0b01000,
0b01000,
0b01000
},
// D5H
{
0b00000,
0b00000,
0b11111,
0b00000,
0b00000
},
// D5V
{
0b01000,
0b01000,
0b01000,
0b01000,
0b01000
},
// L2R1
{
0b00000,
0b00000,
0b01100,
0b01000,
0b00000
},
// L2R2
{
0b00000,
0b00000,
0b00110,
0b00010,
0b00000
},
// L2R3
{
0b00000,
0b00000,
0b00010,
0b00110,
0b00000
},
// L2R4
{
0b00000,
0b00000,
0b01000,
0b01100,
0b00000
},
// L3R1
{
0b00000,
0b01110,
0b01000,
0b01000,
0b00000
},
// L3R2
{
0b00000,
0b01110,
0b00010,
0b00010,
0b00000
},
// L3R3
{
0b00000,
0b00010,
0b00010,
0b01110,
0b00000
},
// L3R3
{
0b00000,
0b01000,
0b01000,
0b01110,
0b00000
}
};
static uint8_t get_pixel(int8_t ox, int8_t oy, int8_t x, int8_t y, uint8_t color, uint8_t mask) {
return bitRead(data->screen[oy + y], ox + x) == color;
} // end of get_pixel
static uint8_t set_pixel(int8_t ox, int8_t oy, int8_t x, int8_t y, uint8_t color, uint8_t mask) {
data->score++;
bitWrite(data->screen[oy + y], ox + x, color);
return 0;
} // end of set_pixel
static uint8_t set_tile(int8_t x, int8_t y, uint8_t item[5], uint8_t color, uint8_t (*draw)(int8_t ox, int8_t oy, int8_t x, int8_t y, uint8_t color, uint8_t mask)) {
for (uint8_t i = 0; i < 25; i++) {
int8_t py = i / 5;
int8_t px = i % 5;
uint8_t mask = 0;
if(py > 0 && bitRead(item[py - 1], px)) mask |= SIDE_U;
if(py < 5 && bitRead(item[py + 1], px)) mask |= SIDE_D;
if(px > 0 && bitRead(item[py], px - 1)) mask |= SIDE_L;
if(px < 5 && bitRead(item[py], px + 1)) mask |= SIDE_R;
if (bitRead(item[py], px)) {
if (draw(x, y, px, py, color, mask)) {
return 0;
}
}
}
return 1;
}
static void display_square(uint8_t x, uint8_t y, uint8_t w, uint8_t mask, uint8_t color, uint8_t fill) {
uint8_t rmask = ~mask;
// Top
if(rmask & SIDE_U) {
gr->drawFastHLine(x, y, w, color);
}
// Left
if(rmask & SIDE_L) {
gr->drawFastVLine(x, y, w, color);
}
// Bottom
if(rmask & SIDE_D) {
gr->drawFastHLine(x, y + w - 1, w, color);
}
// Right
if(rmask & SIDE_R) {
gr->drawFastVLine(x + w - 1, y, w, color);
}
// Top
if(mask & SIDE_U) {
gr->drawPixel(x, y, color);
gr->drawPixel(x + w - 1, y, color);
gr->drawPixel(x, y - 1, color);
gr->drawPixel(x + w - 1, y - 1, color);
}
// Left
if(mask & SIDE_L) {
gr->drawPixel(x, y, color);
gr->drawPixel(x, y + w - 1, color);
gr->drawPixel(x - 1, y, color);
gr->drawPixel(x - 1, y + w - 1, color);
}
// Bottom
if(mask & SIDE_D) {
gr->drawPixel(x, y + w - 1, color);
gr->drawPixel(x + w - 1, y + w - 1, color);
gr->drawPixel(x, y + w, color);
gr->drawPixel(x + w - 1, y + w, color);
}
// Right
if(mask & SIDE_R) {
gr->drawPixel(x + w - 1, y, color);
gr->drawPixel(x + w - 1, y + w - 1, color);
gr->drawPixel(x + w, y, color);
gr->drawPixel(x + w, y + w - 1, color);
}
if(!fill || w < 3) return;
if(fill == FILL_SOLID) {
gr->fillRect(x, y, w, w, color);
return;
}
for (int fx = x; fx < x + w; fx++) {
for (int fy = y; fy < y + w; fy++) {
if((fx + !!(fy %2)) % 2) {
gr->drawPixel(fx, fy, color);
}
}
}
}
static void display_background(void) {
gr->drawRect(0, 0, 63, 63, WHITE);
gr->drawBitmap(64+29, 1, cupBmp, 8, 8, WHITE);
}
static void display_board(void) {
if(data->hiScore < data->score) {
data->hiScore = data->score;
}
gr->fillRect(65, 2, 28, 5, BLACK);
drawNumber(gr, 65, 2, data->score, WHITE, 7);
gr->fillRect(102, 2, 26, 5, BLACK);
drawNumber(gr, 102, 2, data->hiScore, WHITE, 0);
for (int x = 0; x < BOARD_LINES; x++) {
int sx = x * 6 + 2;
for (int y = 0; y < BOARD_LINES; y++) {
int sy = y * 6 + 2;
if (get_pixel(0, 0, x, y, 1, 0)) {
display_square(sx, sy, 5, 0, WHITE, FILL_EMPTY);
} else {
display_square(sx, sy, 5, 0, BLACK, FILL_EMPTY);
}
}
}
}
static void remove_lines(void) {
uint8_t x, y;
uint8_t lines = 0;
uint8_t vlines[5];
uint8_t vline = 0;
uint8_t vcount;
for (x = 0; x < BOARD_LINES; x++) {
vcount = 0;
for (y = 0; y < BOARD_LINES; y++) {
if (get_pixel(0, 0, x, y, WHITE, 0)) {
vcount++;
}
}
if(vcount == BOARD_LINES) {
vlines[vline] = x;
vline++;
lines++;
}
}
for (y = 0; y < BOARD_LINES; y++) {
if (data->screen[y] == 0b1111111111) {
data->screen[y] = 0;
data->score += 10;
lines++;
}
}
for (x = 0; x < vline; x++) {
for (y = 0; y < BOARD_LINES; y++) {
set_pixel(0, 0, vlines[x], y, BLACK, 0);
}
}
// Combo reward
if(lines > 1) {
data->score += 10 * (lines - 1);
}
display_board();
}
static uint8_t display_next_square(int8_t ox, int8_t oy, int8_t x, int8_t y, uint8_t color, uint8_t mask) {
display_square(ox + 4 * x, oy + 4 * y, 3, 0, color, FILL_SOLID);
return 0;
}
static uint8_t check_current_square(int8_t ox, int8_t oy, int8_t x, int8_t y, uint8_t color, uint8_t mask) {
uint8_t rx = ox + 6 * x + 2;
uint8_t ry = oy + 6 * y + 2;
return rx > 60 || ry > 60 || rx < 0 || ry < 0;
}
static uint8_t display_current_square(int8_t ox, int8_t oy, int8_t x, int8_t y, uint8_t color, uint8_t mask) {
uint8_t rx = ox + 6 * x + 2;
uint8_t ry = oy + 6 * y + 2;
if(rx > 60 || ry > 60) return 0;
display_square(rx, ry, 5, mask, color, get_pixel(ox / 6, oy / 6, x, y, WHITE, 0) ? FILL_CHECKERD : FILL_SOLID);
return 0;
}
static void display_next(void) {
gr->fillRect(NEXT_X_OFFSET, NEXT_Y_OFFSET, 3 * NEXT_SIDE, NEXT_SIDE + 1, BLACK);
for (int i = 0; i < 3; i++) {
if(data->next[i] >= 0 && data->next[i] < NUM_TILES) {
set_tile(NEXT_SIDE * i + NEXT_X_OFFSET, NEXT_Y_OFFSET + 2, tiles[data->next[i]], 1, display_next_square);
}
}
}
static void next_random(void) {
data->next[0] = random(NUM_TILES);
data->next[1] = random(NUM_TILES);
data->next[2] = random(NUM_TILES);
data->current = -1;
}
static uint8_t pick_tile(void) {
int8_t direction;
uint8_t cursor = 1;
while(data->next[cursor] & NEXT_BUSY) cursor = (cursor + 1) % 3;
for (;;) {
if (!gr->nextFrame()) {
continue;
}
gr->pollButtons();
if (gr->justPressed(B_BUTTON)) {
return 1;
}
if (gr->justPressed(A_BUTTON)) {
data->current = cursor;
data->next[cursor] |= NEXT_BUSY;
break;
}
direction = gr->justPressed(RIGHT_BUTTON) - gr->justPressed(LEFT_BUTTON);
do {
cursor = (cursor + 3 + direction) % 3;
} while(data->next[cursor] & NEXT_BUSY);
display_next();
gr->drawFastHLine(NEXT_SIDE * cursor + NEXT_X_OFFSET, NEXT_Y_OFFSET, NEXT_SIDE - 3, WHITE);
gr->drawFastHLine(NEXT_SIDE * cursor + NEXT_X_OFFSET, NEXT_Y_OFFSET + NEXT_SIDE, NEXT_SIDE - 3, WHITE);
gr->display();
gr->idle();
}
return 0;
}
static uint8_t next_fit(void) {
int8_t t, x, y, ox, oy;
uint8_t pass = 0;
for(t = 0;t < 3;t++) {
// Skip already placed tile
if(data->next[t] & NEXT_BUSY) {
continue;
}
// Locate offset Left
for(x = 0;x > -5;x--) {
if( ! set_tile(x * 6, 0, tiles[data->next[t]], WHITE, check_current_square)) {
ox = x + 1;
break;
}
}
// Locate offset top
for(y = 0;y > -5;y--) {
if( ! set_tile(0, y * 6, tiles[data->next[t]], WHITE, check_current_square)) {
oy = y + 1;
break;
}
}
for(x = 0;x < BOARD_LINES;x++) {
for(y = 0;y < BOARD_LINES;y++) {
if( ! set_tile((x + ox) * 6, (y + oy) * 6, tiles[data->next[t]], WHITE, check_current_square)) {
continue;
}
if(set_tile(x + ox, y + oy, tiles[data->next[t]], WHITE, get_pixel)) {
x = y = BOARD_LINES;
pass++;
}
}
}
}
return pass != 0;
}
static void place_tile(void) {
int8_t dx, dy;
if(data->current < 0 || data->current > 2) {
return;
}
data->x = 18;
data->y = 18;
display_next();
display_board();
set_tile(data->x, data->y, tiles[data->next[data->current] & ~NEXT_BUSY], WHITE, display_current_square);
gr->display();
for (;;) {
if (!gr->nextFrame()) {
continue;
}
gr->pollButtons();
set_tile(data->x, data->y, tiles[data->next[data->current] & ~NEXT_BUSY], BLACK, display_current_square);
// display_board();
if (gr->justPressed(B_BUTTON)) {
data->next[data->current] &= ~NEXT_BUSY;
data->current = -1;
return;
}
if (gr->justPressed(A_BUTTON)) {
if( ! set_tile(data->x / 6, data->y / 6, tiles[data->next[data->current] & ~NEXT_BUSY], WHITE, get_pixel)) {
continue;
}
set_tile(data->x / 6, data->y / 6, tiles[data->next[data->current] & ~NEXT_BUSY], WHITE, set_pixel);
data->current = -1;
display_board();
return;
}
if((gr->pressed(LEFT_BUTTON) ||
gr->pressed(RIGHT_BUTTON) ||
gr->pressed(UP_BUTTON) ||
gr->pressed(DOWN_BUTTON)
) && (millis() - button_wait_time > 250)) {
if(gr->justPressed(LEFT_BUTTON) ||
gr->justPressed(RIGHT_BUTTON) ||
gr->justPressed(UP_BUTTON) ||
gr->justPressed(DOWN_BUTTON)) {
button_wait_time = millis();
}
dx = (gr->pressed(RIGHT_BUTTON) - gr->pressed(LEFT_BUTTON)) * 6;
dy = (gr->pressed(DOWN_BUTTON) - gr->pressed(UP_BUTTON)) * 6;
if(set_tile(data->x + dx, data->y + dy, tiles[data->next[data->current] & ~NEXT_BUSY], WHITE, check_current_square)) {
data->x += dx;
data->y += dy;
display_board();
set_tile(data->x, data->y, tiles[data->next[data->current] & ~NEXT_BUSY], WHITE, display_current_square);
gr->display();
}
}
gr->idle();
}
}
static void game_new(void) {
data->gameOn = 1;
data->score = 0;
memset(data->screen, 0, sizeof data->screen);
next_random();
}
static void game_on(void) {
gr->clear();
display_background();
display_board();
gr->display();
for (;;) {
if(data->next[0] & NEXT_BUSY && data->next[1] & NEXT_BUSY && data->next[2] & NEXT_BUSY) {
next_random();
}
// Game over?
if( ! next_fit()) {
data->gameOn = 0;
return;
}
if(pick_tile()) {
return;
}
place_tile();
remove_lines();
}
}
void game1010(Arduboy2 *sgr, uint8_t *gdat, uint8_t menu, uint8_t *gameOn, uint32_t *score, uint32_t *hiScore) {
gr = sgr;
data = (struct data_t *)gdat;
if (menu == MENU_NEW) {
game_new();
}
if (menu != MENU_EXIT) {
game_on();
}
*gameOn = data->gameOn;
*score = data->score;
*hiScore = data->hiScore;
}