-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
636 lines (536 loc) · 17.7 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
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "pico/stdlib.h"
#include "libraries/pico_graphics/pico_graphics.hpp"
#include "galactic_unicorn.hpp"
#include "util.hpp"
#include "hardware/adc.h"
using namespace pimoroni;
PicoGraphics_PenRGB888 graphics(53, 11, nullptr);
GalacticUnicorn galactic_unicorn;
#define ROWS 53 // you can change height and width of table with ROWS and COLS
//#define ROWS 23 // you can change height and width of table with ROWS and COLS
#define COLS 11
char Table[ROWS][COLS];
int score;
bool playing;
bool paused = false;
bool autoplay = false;
bool exit_autoplay = false;
int next_shape_index = -1;
const uint32_t min_speed = 200;
const uint32_t start_speed = 400; // decrease this to make it faster
uint32_t speed;
int speed_step = 10;
typedef struct {
char **array;
int width, row, col;
} Shape;
Shape current;
#define NUM_SHAPES 7
const Shape ShapesArray[NUM_SHAPES]= {
{(char *[]){(char []){0,1,1},(char []){1,1,0}, (char []){0,0,0}}, 3}, //S shape
{(char *[]){(char []){1,1,0},(char []){0,1,1}, (char []){0,0,0}}, 3}, //Z shape
{(char *[]){(char []){0,1,0},(char []){1,1,1}, (char []){0,0,0}}, 3}, //T shape
{(char *[]){(char []){0,0,1},(char []){1,1,1}, (char []){0,0,0}}, 3}, //L shape
{(char *[]){(char []){1,0,0},(char []){1,1,1}, (char []){0,0,0}}, 3}, //flipped L shape
{(char *[]){(char []){1,1},(char []){1,1}}, 2}, //square shape
{(char *[]){(char []){0,0,0,0}, (char []){1,1,1,1}, (char []){0,0,0,0}, (char []){0,0,0,0}}, 4} //long bar shape
// you can add any shape like it's done above. Don't be naughty.
};
char shape_colors[NUM_SHAPES] = {0}; // starting colour for each different shape, randomly assigned on startup
bool check_key() {
return galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_A) ||
galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_B) ||
galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_C) ||
galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_D);
}
void key_animate() {
static uint8_t y = 0, old_y = 0;
static uint32_t last_check_ms = 0;
graphics.set_pen(0, 0, 0);
graphics.pixel(Point(0, old_y));
graphics.set_pen(255, 255, 255);
graphics.pixel(Point(0, y));
if (last_check_ms + 500 > millis()) {
return;
}
old_y = y;
y+=2;
if (y > 5) y = 0;
last_check_ms = millis();
}
void wait_key_animate() {
while(true) {
key_animate();
galactic_unicorn.update(&graphics);
for(int i = 0; i < 50; i++) {
if (check_key()) return;
sleep_ms(10);
}
}
}
Shape CopyShape(Shape shape){
Shape new_shape = shape;
char **copyshape = shape.array;
new_shape.array = (char**)malloc(new_shape.width*sizeof(char*));
int i, j;
for(i = 0; i < new_shape.width; i++) {
new_shape.array[i] = (char*)malloc(new_shape.width*sizeof(char));
for(j=0; j < new_shape.width; j++) {
new_shape.array[i][j] = copyshape[i][j];
}
}
return new_shape;
}
void DeleteShape(Shape shape){
for(int i = 0; i < shape.width; i++) free(shape.array[i]);
free(shape.array);
}
bool CheckPosition(Shape shape){ // Check the position of the copied shape
char **array = shape.array;
int i, j;
for(i = 0; i < shape.width; i++) {
for(j = 0; j < shape.width; j++){
if((shape.col+j < 0 || shape.col+j >= COLS || shape.row+i >= ROWS)) { //Out of borders
if(array[i][j] > 0) //but is it just a phantom?
return false;
}
else if(Table[shape.row+i][shape.col+j] > 0 && array[i][j] > 0) return false;
}
}
return true;
}
int ChooseRandomShape() {
static int old_shape_index = -1;
int shape_index;
/*
if (old_shape_index == -1) {
shape_index = 5;
} else if (old_shape_index == 5) {
shape_index = 6;
} else {
shape_index = 5;
}
old_shape_index = shape_index;
return shape_index;
*/
do {
shape_index = rand() % NUM_SHAPES;
} while (shape_index == old_shape_index);
old_shape_index = shape_index;
return shape_index;;
}
void SetNewRandomShape(){ //updates [current] with new shape
if (next_shape_index == -1) {
next_shape_index = ChooseRandomShape();
}
int shape_index = next_shape_index;
next_shape_index = ChooseRandomShape();
Shape new_shape = CopyShape(ShapesArray[shape_index]);
for(int i = 0; i < new_shape.width; i++) {
for(int j = 0; j < new_shape.width; j++) {
if(new_shape.array[i][j]) {
new_shape.array[i][j] = shape_colors[shape_index];
}
}
}
new_shape.col = (COLS-new_shape.width+1) / 2;
new_shape.row = 0;
DeleteShape(current);
current = new_shape;
if(!CheckPosition(current)) playing = false;
}
void RotateShape(Shape shape){ //rotates clockwise
Shape temp = CopyShape(shape);
int i, j, k, width;
width = shape.width;
for(i = 0; i < width ; i++){
for(j = 0, k = width-1; j < width; j++, k--){
shape.array[i][j] = temp.array[k][i];
}
}
DeleteShape(temp);
}
void WriteToTable(){
for(int i = 0; i < current.width; i++) {
for(int j = 0; j < current.width; j++) {
if (current.array[i][j] > 0) Table[current.row+i][current.col+j] = current.array[i][j];
}
}
}
void RemoveFullRowsAndUpdateScore() {
int count = 0;
for(int i = 0; i < ROWS; i++){
int sum = 0;
for(int j = 0; j < COLS; j++) sum += Table[i][j] > 0;
if(sum == COLS) {
count++;
int k;
for(int k = i; k >= 1; k--)
for(int l = 0; l < COLS; l++)
Table[k][l]=Table[k-1][l];
for(int l = 0; l < COLS; l++) Table[k][l]=0;
speed -= speed_step;
if (speed < min_speed) speed = min_speed;
}
}
score += 100*count;
}
void PrintTable() {
char Buffer[ROWS][COLS] = {0};
for(int i = 0; i < current.width ;i++){
for(int j = 0; j < current.width ; j++){
if(current.array[i][j] > 0)
Buffer[current.row+i][current.col+j] = current.array[i][j];
}
}
graphics.set_pen(0, 0, 0);
graphics.clear();
// print next shape
if (next_shape_index > -1) {
uint8_t r, g, b;
rgb_from_byte(shape_colors[next_shape_index], &r, &g, &b);
r >>= 2;
g >>= 2;
b >>= 2;
graphics.set_pen(r, g, b);
const Shape *next = &ShapesArray[next_shape_index];
int eff_height = next->width;
int start_height = -1;
bool had_blocks = false;
for(int i = 0; i < next->width; i++) {
bool has_blocks = false;
for (int j = 0; j < next->width; j++)
if (next->array[i][j] > 0) {
has_blocks = true;
had_blocks = true;
break;
}
if (!has_blocks && !had_blocks) eff_height--;
else if (start_height == -1) start_height = i;
}
for(int i = start_height; i < eff_height; i++) {
for (int j = 0; j < next->width; j++)
if (next->array[i][j] > 0) graphics.pixel(Point(ROWS - (i - start_height) - eff_height, COLS - next->width + j));
}
}
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLS; j++){
char val = Table[i][j] + Buffer[i][j];
if (val == 0) continue;
pen_from_byte(val);
graphics.pixel(Point(ROWS - i, j)); // flip top-bottom
}
}
if (autoplay) key_animate();
galactic_unicorn.update(&graphics);
}
void ClearTable() {
for(int i = 0; i < ROWS; i++) for(int j = 0; j < COLS; j++) Table[i][j] = 0;
}
bool ManipulateCurrent(int action){
Shape temp = CopyShape(current);
switch(action){
case 's':
temp.row++; //move down
if(CheckPosition(temp))
current.row++;
else {
DeleteShape(temp);
WriteToTable();
RemoveFullRowsAndUpdateScore();
SetNewRandomShape();
return true;
}
break;
case 'd':
temp.col++; //move right
if(CheckPosition(temp))
current.col++;
break;
case 'a':
temp.col--; //move left
if(CheckPosition(temp))
current.col--;
break;
case 'w':
RotateShape(temp); // rotate clockwise
if(CheckPosition(temp))
RotateShape(current);
break;
}
DeleteShape(temp);
PrintTable();
return false;
}
static bool do_auto_light = true;
void auto_adjust_brightness() {
float light_level = ((float)galactic_unicorn.light())/4095.0f;
galactic_unicorn.set_brightness(light_level + 0.15f);
if (paused) galactic_unicorn.update(&graphics);
}
bool light_timer_callback(struct repeating_timer *t) {
if (do_auto_light) auto_adjust_brightness();
return true;
}
bool SolveCurrent(int *NumRotations, int *Row, int *Column){
Shape temp = CopyShape(current);
int good_col = -1;
int good_rot = 0;
int max_full_rows = 0; // most important
int min_holes_below = COLS * ROWS; // second most important
int max_fill_cols = 0; // third most important
int max_row = temp.row; // least important
// TODO max_row calculation is wrong, counts from the top so an upright rotation will always use compared to the flat version
// After it's fixed it could be the most important factor in terms of preventing incomplete rows at the bottom
bool valid = false;
for(int rot_index = 0; rot_index < 4; rot_index++) {
for (int col_index = 0; col_index < COLS; col_index++) {
bool had_valid = false;
temp.col = col_index;
// if we start checking from the lowest row and go up we can't get into a situation where we can't move down
for (int i = temp.row; i < ROWS; i++) {
temp.row = i;
if (!CheckPosition(temp)) break;
had_valid = true;
}
if (!had_valid) continue; // try next column
temp.row--; // our position isn't valid, move one row up
int full_rows = 0, fill_cols = 0;
char tableCopy[ROWS][COLS]; // copy the table: value is 1 for already filled, 0 for empty, 2 for shape
for (int i = 0; i < ROWS; i++) for (int j = 0; j < COLS; j++) tableCopy[i][j] = Table[i][j] > 0 ? 1 : 0;
// check maximum fill in table
for (int i = 0; i < temp.width; i++) {
for (int j = 0; j < temp.width; j++) if (temp.array[i][j] > 0) tableCopy[temp.row + i][temp.col + j] = temp.array[i][j] > 0 ? 2 : 0;
int sum = 0, shape_sum = 0, nonshape_sum = 0;
// for every row, we check if it's full
// we also calculate how many cells are filled by the shape in this position, but we ignore if the row only has cells filled by the shape and the rest is empty
for (int j = 0; j < COLS; j++) {
sum += tableCopy[temp.row+i][j] > 0;
nonshape_sum += tableCopy[temp.row+i][j] == 1;
shape_sum += tableCopy[temp.row+i][j] == 2;
}
if (sum == COLS) full_rows++;
if (shape_sum > 0 && nonshape_sum == 0) {
// if the row is only filled by the shape, we don't count it
continue;
}
fill_cols += shape_sum + nonshape_sum;
}
int holes_below = 0;
for (int i = temp.row; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
if (tableCopy[i][j] == 0) holes_below++;
// printf("[rot:%d] full rows:%d fill_cols:%d holes:%d\n", rot_index, full_rows, fill_cols, holes_below);
if (full_rows > max_full_rows ||
(full_rows == max_full_rows && holes_below < min_holes_below) ||
(full_rows == max_full_rows && holes_below == min_holes_below && fill_cols > max_fill_cols) ||
(full_rows == max_full_rows && holes_below == min_holes_below && fill_cols == max_fill_cols && temp.row > max_row)) {
max_full_rows = full_rows;
max_fill_cols = fill_cols;
min_holes_below = holes_below;
max_row = temp.row;
good_col = temp.col;
good_rot = rot_index;
valid = true;
// printf("Solve candidate: Row:%d Col:%d Rot:%d // Full:%d Fill:%d Holes:%d\n", max_row, good_col, good_rot, full_rows, fill_cols, holes_below);
}
}
temp.col = current.col;
temp.row = current.row;
RotateShape(temp);
}
DeleteShape(temp);
*NumRotations = good_rot;
*Row = max_row;
*Column = good_col;
printf("Solved: Row:%d Col:%d Rot:%d Full:%d Fill:%d Holes:%d valid:%c\n\n", max_row, good_col, good_rot, max_full_rows, max_fill_cols, min_holes_below, valid ? 'Y' : 'N');
return valid;
}
bool loop_things(bool paused_check = true) {
bool brightness_up = galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_BRIGHTNESS_UP);
bool brightness_down = galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_BRIGHTNESS_DOWN);
if (brightness_up && brightness_down) {
galactic_unicorn.set_brightness(0.0f);
do_auto_light = true;
sleep_ms(100);
auto_adjust_brightness();
printf("Auto-brightness re-enabled\n");
sleep_ms(500);
return false;
} else if (brightness_up) {
galactic_unicorn.adjust_brightness(+0.01f);
if (paused) galactic_unicorn.update(&graphics);
do_auto_light = false;
sleep_ms(200);
} else if (brightness_down) {
galactic_unicorn.adjust_brightness(-0.01f);
if (paused) galactic_unicorn.update(&graphics);
do_auto_light = false;
sleep_ms(200);
}
if (galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_SLEEP)) {
paused = !paused;
sleep_ms(300);
}
if (galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_VOLUME_UP)) {
speed -= speed_step;
if (speed < min_speed) speed = min_speed;
else sleep_ms(150);
} else if (galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_VOLUME_DOWN)) {
speed += speed_step;
if (speed > start_speed) speed = start_speed;
else sleep_ms(150);
}
if (autoplay && check_key()) {
exit_autoplay = true;
}
if (paused_check) {
while(paused) {
loop_things(false);
galactic_unicorn.update(&graphics);
sleep_ms(10);
}
}
return exit_autoplay;
}
bool loopy_sleep(int ms) {
int i;
for (i = 0; i < ms; i+=10) {
if (loop_things()) return true;
sleep_ms(10);
}
return false;
}
void auto_play(bool recursed = false) {
int num_rotations, row, col;
if (!SolveCurrent(&num_rotations, &row, &col)) return;
const int start_moves_after_rows = 5;
int moves = num_rotations + abs(current.col - col);
int row_diff = row - current.row;
if (!recursed && moves > 0 && row_diff > moves + start_moves_after_rows) {
if (row_diff > start_moves_after_rows) row_diff = start_moves_after_rows;
for(int i=0;i<row_diff;i++) {
if (ManipulateCurrent('s')) return;
if (loopy_sleep(100)) return;
}
}
for (int i = 0; i < num_rotations; i++) {
if (ManipulateCurrent('w')) return;
if (loopy_sleep(250)) return;
}
int max_col_moves = abs(current.col - col) + 1;
while (current.col != col && max_col_moves > 0) {
if (current.col < col) {
if (ManipulateCurrent('d')) return;
} else {
if (ManipulateCurrent('a')) return;
}
if (loopy_sleep(100)) return;
max_col_moves--;
}
if (ManipulateCurrent('s')) return;
if (loopy_sleep(50)) return;
auto_play(true); // solve again
// while (current.row != row) {
// if (ManipulateCurrent('s')) return;
// if (loopy_sleep(50)) return;
// }
}
void rnd_seed() {
adc_init();
adc_gpio_init(GalacticUnicorn::SWITCH_SLEEP); // Sleep button is on the ADC... so we do this before initializing the GU.
adc_select_input(1);
uint32_t total = 0;
for(int i = 0; i < 16; i++) {
uint16_t val = adc_read();
if (i % 2 == 1) total += ((val & 2) << i); // last 2 bits... positional
}
srand(total);
}
int main() {
stdio_init_all();
rnd_seed();
galactic_unicorn.init();
init_hue_map();
auto_adjust_brightness();
graphics.set_pen(0, 0, 0);
graphics.clear();
galactic_unicorn.update(&graphics);
outline_text(" T E T R I S", true, random_color());
galactic_unicorn.update(&graphics);
sleep_ms(1000);
struct repeating_timer light_timer;
add_repeating_timer_ms(1000, light_timer_callback, NULL, &light_timer);
for(int i=0;i<NUM_SHAPES;i++) shape_colors[i] = random_color(shape_colors, i);
autoplay = true;
while(true) {
pen_from_byte(0);
graphics.clear();
char main_colour = random_color();
outline_text(autoplay ? "DEMO" : "PLAY!", true, main_colour);
galactic_unicorn.update(&graphics);
uint32_t last_update = millis();
sleep_ms(700);
score = 0;
next_shape_index = -1;
speed = start_speed;
playing = true;
paused = false;
ClearTable();
SetNewRandomShape();
PrintTable();
while (playing) {
loop_things();
if (autoplay) {
auto_play();
if (exit_autoplay) break;
} else {
// use a vi-like keymap
if (galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_A)) {
ManipulateCurrent('a');
sleep_ms(200);
} else if (galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_D)) {
ManipulateCurrent('d');
sleep_ms(200);
} else if (galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_C)) {
ManipulateCurrent('w');
sleep_ms(300); // delay more for rotate
} else if (galactic_unicorn.is_pressed(GalacticUnicorn::SWITCH_B)) {
ManipulateCurrent('s');
sleep_ms(100); // less for down
}
}
if (!paused && millis() - last_update > speed) {
ManipulateCurrent('s');
last_update = millis();
}
}
graphics.clear();
if (autoplay && exit_autoplay) {
exit_autoplay = false;
autoplay = false;
continue;
}
outline_text("Game Over!", true, 0b01100101);
galactic_unicorn.update(&graphics);
sleep_ms(1000);
// rainbow_text("Score", 500);
graphics.clear();
outline_text("Score", true, main_colour);
galactic_unicorn.update(&graphics);
sleep_ms(500);
graphics.clear();
outline_text(std::to_string(score));
galactic_unicorn.update(&graphics);
if (autoplay) {
sleep_ms(1000);
continue;
}
wait_key_animate();
// rainbow_text(std::to_string(score), 0, check_key);
} // while(true)
return 0;
}