-
Notifications
You must be signed in to change notification settings - Fork 3
/
TetrisAI.c
1708 lines (1505 loc) · 46.7 KB
/
TetrisAI.c
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <conio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
const char *const INFO_AUTHOR = "iBug";
const char *const INFO_VERSION = "1.10 c";
// Blocks
enum {
TETRIS_I = 0,
TETRIS_T,
TETRIS_L,
TETRIS_J,
TETRIS_Z,
TETRIS_S,
TETRIS_O
};
// =============================================================================
// Rotation states
static const uint16_t gs_uTetrisTable[7][4] = {
{0x00F0U, 0x2222U, 0x00F0U, 0x2222U}, // I
{0x0072U, 0x0262U, 0x0270U, 0x0232U}, // T
{0x0223U, 0x0074U, 0x0622U, 0x0170U}, // L
{0x0226U, 0x0470U, 0x0322U, 0x0071U}, // J
{0x0063U, 0x0264U, 0x0063U, 0x0264U}, // Z
{0x006CU, 0x0462U, 0x006CU, 0x0462U}, // S
{0x0660U, 0x0660U, 0x0660U, 0x0660U} // O
};
// =============================================================================
// Initial pool state
// From 0 (top row) to 28 (bottom row)
// More 1's for collision detection
// Pool width = 16 - 2 * 2 = 12
// 0xFFFFU = Full row
static const uint16_t gs_uInitialTetrisPool[28] = {
0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U,
0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U,
0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U,
0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xFFFFU, 0xFFFFU
};
#define COL_BEGIN 2
#define COL_END 14
#define ROW_BEGIN 4
#define ROW_END 26
// =============================================================================
typedef struct TetrisManager {
uint16_t pool[28];
int8_t x;
int8_t y;
int8_t type[3];
int8_t orientation[3];
} TetrisManager;
// =============================================================================
typedef struct TetrisControl {
int8_t color[28][16];
bool dead;
bool pause;
bool clockwise;
int8_t direction;
bool model;
uint16_t frameRate;
unsigned score;
unsigned erasedCount[4]; // How many rows are erased at once
unsigned erasedTotal;
unsigned tetrisCount[7];
unsigned tetrisTotal;
} TetrisControl;
HANDLE hConsoleOutput;
struct {
int8_t model;
int8_t benchmark;
double startTime;
bool resetFpsCounter;
} general;
unsigned long fpsRate = 800;
unsigned short lastInsertedHeight = 0;
// =============================================================================
// Easter Eggs:
// [0]
// [1]
// [2] Turbo Boost
// [3]
#define easterNum 4
int8_t easterEgg[easterNum] = {0};
// =============================================================================
// Function board
bool isWindowsTerminal();
void autoRun(TetrisManager *manager, TetrisControl *control);
void benchmark(TetrisManager *manager, TetrisControl *control);
signed long benchmarkRun(TetrisManager *manager, TetrisControl *control);
signed long calcFPS(void);
bool checkCollision(const TetrisManager *manager);
bool checkErasing(TetrisManager *manager, TetrisControl *control);
void clrscr(void);
double getTime(void);
void dropDownTetris(TetrisManager *manager, TetrisControl *control);
bool enableDebugPrivilege();
void flushPrint();
void giveTetris(TetrisManager *manager, TetrisControl *control);
void gotoxyInPool(short x, short y);
void gotoxyWithFullwidth(short x, short y);
bool horzMoveTetris(TetrisManager *manager, TetrisControl *control);
void initGame(TetrisManager *manager, TetrisControl *control, bool model);
void insertTetris(TetrisManager *manager);
bool keydownControl(TetrisManager *manager, TetrisControl *control, int key);
int mainMenu(void);
bool moveDownTetris(TetrisManager *manager, TetrisControl *control);
void pause(void);
void printCurrentTetris(const TetrisManager *manager,
const TetrisControl *control);
void printNextTetris(const TetrisManager *manager);
void printPoolBorder();
void printPrompting(const TetrisControl *control);
void printScore(const TetrisManager *manager, const TetrisControl *control);
void printTetrisPool(const TetrisManager *manager,
const TetrisControl *control);
void removeTetris(TetrisManager *manager);
bool rotateTetris(TetrisManager *manager, TetrisControl *control);
void runGame(TetrisManager *manager, TetrisControl *control);
void setPoolColor(const TetrisManager *manager, TetrisControl *control);
// =============================================================================
// iBug's C-Sync technology!
CHAR_INFO outputBuffer[25][80];
const SMALL_RECT outputRegion = {0, 0, 79, 24};
const COORD outputBufferSize = {80, 25}, zeroPosition = {0, 0};
COORD outputCursorPosition = {0, 0};
WORD outputAttribute = 0x07;
void IncrementOutputCursorPosition();
char charBuf[128];
#define buffer_printf(args...)\
{\
snprintf(charBuf, sizeof(charBuf), args);\
buffer_print(charBuf);\
}
void buffer_SetConsoleCursorPosition(HANDLE, COORD Pos);
void buffer_SetConsoleTextAttribute(HANDLE, WORD Attribute);
void buffer_print(LPCSTR String);
// =============================================================================
int main(int argc, char *argv[]) {
// Restart with conhost.exe if running in WT.
if (IsWindowsTerminal()) {
char currentPath[MAX_PATH];
GetModuleFileNameA(NULL, currentPath, MAX_PATH);
char command[MAX_PATH + 15];
sprintf_s(command, "conhost.exe %s", currentPath);
system(command);
return 0;
}
// In case there's any cmdline arguments
if (argc > 1) {
char optionEx[128];
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '/' || argv[i][0] == '-') {
argv[i]++;
// Convert to lowercase
char *a = argv[i];
while (*a != '\0' && *a != ':') {
if (*a >= 'A' && *a <= 'Z') {
*a += 0x20;
}
a++;
}
if (*a == ':') {
*a = '\0';
a++;
strcpy(optionEx, a);
} else {
memset(optionEx, 0, sizeof(optionEx));
}
if (strcmp(argv[i], "c16") == 0) {
easterEgg[1] = 1;
} else if (strcmp(argv[i], "boost") == 0) {
easterEgg[2] = 1;
} else if (strcmp(argv[i], "fpsrate") == 0) {
long newfps = atol(optionEx);
if (newfps > 0) {
fpsRate = newfps;
if (fpsRate > 5000) {
fpsRate = 5000;
}
easterEgg[3] = 1;
}
} else if (strcmp(argv[i], "turbo") == 0) {
enableDebugPrivilege();
HANDLE thisproc = GetCurrentProcess();
HANDLE thisthread = GetCurrentThread();
int procprio, threadprio;
procprio = REALTIME_PRIORITY_CLASS;
threadprio = THREAD_PRIORITY_TIME_CRITICAL;
SetPriorityClass(thisproc, procprio);
SetThreadPriority(thisthread, threadprio);
} else {
printf("Error: Unknown command line option \"%s\"\n", argv[i]);
return 1;
}
}
}
}
// Initialize Console
TetrisManager tetrisManager;
TetrisControl tetrisControl;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo = {1, FALSE};
CONSOLE_SCREEN_BUFFER_INFO screenInfo;
GetConsoleScreenBufferInfo(hConsoleOutput, &screenInfo);
COORD bufferSize;
/*
bufferSize.X = 1 + screenInfo.srWindow.Right;
bufferSize.Y = 1 + screenInfo.srWindow.Bottom;
*/
bufferSize.X = 80;
bufferSize.Y = 25;
SMALL_RECT consoleWindowPos = {0, 0, 79, 24};
SetConsoleWindowInfo(hConsoleOutput, TRUE, &consoleWindowPos);
SetConsoleScreenBufferSize(hConsoleOutput, bufferSize);
clrscr();
SetConsoleCursorInfo(hConsoleOutput, &cursorInfo);
snprintf(charBuf, sizeof(charBuf), "Tetris with AI ver %s", INFO_VERSION);
SetConsoleTitleA(charBuf);
DeleteMenu(GetSystemMenu(GetConsoleWindow(), 0), SC_MAXIMIZE, MF_DISABLED);
do {
general.model = 0;
general.model = mainMenu();
if (general.model != 2) {
general.benchmark = 0;
} else {
general.benchmark = 1;
}
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x07);
clrscr();
initGame(&tetrisManager, &tetrisControl, general.model == 0);
printPrompting(&tetrisControl);
printPoolBorder();
int8_t prevEE2;
switch (general.model) {
case 0:
tetrisControl.frameRate = 450;
runGame(&tetrisManager, &tetrisControl);
break;
case 1:
autoRun(&tetrisManager, &tetrisControl);
break;
case 2:
prevEE2 = easterEgg[2];
easterEgg[2] = 1;
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x07);
clrscr();
initGame(&tetrisManager, &tetrisControl, 0);
printPrompting(&tetrisControl);
printPoolBorder();
signed long mark = benchmarkRun(&tetrisManager, &tetrisControl);
clrscr();
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x0F);
gotoxyWithFullwidth(14, 5);
buffer_print("┏━━━━━━━━━━┓");
gotoxyWithFullwidth(14, 6);
buffer_print("┃ Tetris with AI ┃");
gotoxyWithFullwidth(14, 7);
buffer_print("┃ Benchmark Mode ┃");
gotoxyWithFullwidth(14, 8);
buffer_print("┗━━━━━━━━━━┛");
gotoxyWithFullwidth(14, 9);
buffer_printf("Author: %s", INFO_AUTHOR);
gotoxyWithFullwidth(15, 11);
buffer_printf("Score: %6ld", mark);
flushPrint();
pause();
easterEgg[2] = prevEE2;
continue;
break;
default:
return -1;
}
buffer_SetConsoleTextAttribute(hConsoleOutput, 0xF0);
gotoxyWithFullwidth(12, 9);
buffer_print(" ");
gotoxyWithFullwidth(12, 10);
buffer_print(easterEgg[1] ? " Stack Overflow " : " Press any key. ");
gotoxyWithFullwidth(12, 11);
buffer_print(" ");
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x07);
flushPrint();
pause();
clrscr();
} while (1);
gotoxyWithFullwidth(0, 0);
// CloseHandle(hConsoleOutput);
return 0;
}
// =============================================================================
BOOL IsWindowsTerminal()
{
HWND hwndConsole = GetForegroundWindow();
if (hwndConsole != NULL)
{
DWORD dwConsoleProcessId;
GetWindowThreadProcessId(hwndConsole, &dwConsoleProcessId);
HANDLE hConsoleProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, dwConsoleProcessId);
if (hConsoleProcess != NULL)
{
WCHAR processPath[MAX_PATH];
DWORD dwSize = MAX_PATH;
if (QueryFullProcessImageNameW(hConsoleProcess, 0, processPath, &dwSize) != 0)
{
WCHAR* fileName = wcsrchr(processPath, L'\\');
if (fileName != NULL)
{
std::wcout<<fileName;
if (_wcsicmp(fileName + 1, L"WindowsTerminal.exe") == 0)
{
CloseHandle(hConsoleProcess);
return TRUE;
}
}
}
CloseHandle(hConsoleProcess);
}
}
return FALSE;
}
// =============================================================================
void initGame(TetrisManager *manager, TetrisControl *control, bool model) {
memset(manager, 0, sizeof(TetrisManager));
memcpy(manager->pool, gs_uInitialTetrisPool, sizeof(uint16_t[28]));
if (general.benchmark) {
srand(2014530982);
} else {
srand((unsigned)time(NULL));
}
// Next and the next after next
manager->type[1] = rand() % 7;
manager->orientation[1] = rand() & 3;
manager->type[2] = rand() % 7;
manager->orientation[2] = rand() & 3;
if (general.benchmark) {
manager->type[1] = 0;
manager->type[2] = 1;
}
memset(control, 0, sizeof(TetrisControl));
control->model = model;
giveTetris(manager, control);
setPoolColor(manager, control);
printScore(manager, control);
printTetrisPool(manager, control);
general.startTime = getTime();
general.resetFpsCounter = true;
}
// =============================================================================
void giveTetris(TetrisManager *manager, TetrisControl *control) {
static uint16_t tetris;
static uint16_t num;
manager->type[0] = manager->type[1];
manager->orientation[0] = manager->orientation[1];
manager->type[1] = manager->type[2];
manager->orientation[1] = manager->orientation[2];
num = rand();
/*
if (general.model==1)
{
if (lastInsertedHeight<=16 && num%7==0)
{
manager->type[2] = 1+num%6;
}
else if (num >= 40000-lastInsertedHeight*300)
{
manager->type[2] = 0;
}
else
{
manager->type[2] = num%7;
}
}
*/
if (num >= 32500) {
manager->type[2] = 0;
} else {
manager->type[2] = num % 7;
}
if (general.benchmark) {
manager->type[2] = (manager->type[1] + 1) % 7;
}
manager->orientation[2] = rand() & 3;
tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
if (tetris & 0xF000) {
manager->y = 0;
} else {
manager->y = (tetris & 0xFF00) ? 1 : 2;
}
manager->x = 6;
if (checkCollision(manager)) {
control->dead = true;
} else {
insertTetris(manager);
}
++control->tetrisTotal;
++control->tetrisCount[manager->type[0]];
printNextTetris(manager);
}
// =============================================================================
bool checkCollision(const TetrisManager *manager) {
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
uint16_t dest = 0U;
dest |= (((manager->pool[manager->y + 0] >> manager->x) << 0x0) & 0x000F);
dest |= (((manager->pool[manager->y + 1] >> manager->x) << 0x4) & 0x00F0);
dest |= (((manager->pool[manager->y + 2] >> manager->x) << 0x8) & 0x0F00);
dest |= (((manager->pool[manager->y + 3] >> manager->x) << 0xC) & 0xF000);
return ((dest & tetris) != 0);
}
// =============================================================================
void insertTetris(TetrisManager *manager) {
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
manager->pool[manager->y + 0] |= (((tetris >> 0x0) & 0x000F) << manager->x);
manager->pool[manager->y + 1] |= (((tetris >> 0x4) & 0x000F) << manager->x);
manager->pool[manager->y + 2] |= (((tetris >> 0x8) & 0x000F) << manager->x);
manager->pool[manager->y + 3] |= (((tetris >> 0xC) & 0x000F) << manager->x);
}
// =============================================================================
void removeTetris(TetrisManager *manager) {
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
manager->pool[manager->y + 0] &= ~(((tetris >> 0x0) & 0x000F) << manager->x);
manager->pool[manager->y + 1] &= ~(((tetris >> 0x4) & 0x000F) << manager->x);
manager->pool[manager->y + 2] &= ~(((tetris >> 0x8) & 0x000F) << manager->x);
manager->pool[manager->y + 3] &= ~(((tetris >> 0xC) & 0x000F) << manager->x);
}
// =============================================================================
void setPoolColor(const TetrisManager *manager, TetrisControl *control) {
int8_t i, x, y;
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
for (i = 0; i < 16; ++i) {
y = (i >> 2) + manager->y;
if (y > ROW_END) {
break;
}
x = (i & 3) + manager->x;
if ((tetris >> i) & 1) {
control->color[y][x] = (manager->type[0] | 8);
}
}
}
// =============================================================================
bool rotateTetris(TetrisManager *manager, TetrisControl *control) {
int8_t ori = manager->orientation[0];
removeTetris(manager);
manager->orientation[0] =
(control->clockwise) ? ((ori + 1) & 3) : ((ori + 3) & 3);
if (checkCollision(manager)) {
manager->orientation[0] = ori;
insertTetris(manager);
return false;
} else {
insertTetris(manager);
setPoolColor(manager, control);
if (!easterEgg[2] || control->model == 0) {
printCurrentTetris(manager, control);
}
return true;
}
}
// =============================================================================
bool horzMoveTetris(TetrisManager *manager, TetrisControl *control) {
int x = manager->x;
removeTetris(manager);
control->direction == 0 ? (--manager->x) : (++manager->x);
if (checkCollision(manager)) {
manager->x = x;
insertTetris(manager);
return false;
} else {
insertTetris(manager);
setPoolColor(manager, control);
if (!easterEgg[2] || !control->model) {
printCurrentTetris(manager, control);
}
return true;
}
}
// =============================================================================
bool moveDownTetris(TetrisManager *manager, TetrisControl *control) {
int8_t y = manager->y;
removeTetris(manager);
++manager->y;
if (checkCollision(manager)) {
manager->y = y;
insertTetris(manager);
if (checkErasing(manager, control)) {
if (control->frameRate > 0) {
Sleep(2 * control->frameRate);
}
printTetrisPool(manager, control);
}
return false;
} else {
insertTetris(manager);
setPoolColor(manager, control);
printCurrentTetris(manager, control);
return true;
}
}
// =============================================================================
void dropDownTetris(TetrisManager *manager, TetrisControl *control) {
removeTetris(manager);
for (; manager->y < ROW_END; ++manager->y) {
if (checkCollision(manager)) {
break;
}
}
lastInsertedHeight = --manager->y;
insertTetris(manager);
setPoolColor(manager, control);
printTetrisPool(manager, control);
if (checkErasing(manager, control) && control->frameRate > 0) {
Sleep(2 * control->frameRate);
printTetrisPool(manager, control);
}
}
// =============================================================================
bool checkErasing(TetrisManager *manager, TetrisControl *control) {
static const unsigned scores[5] = {0, 10, 30, 90, 150};
static const unsigned scoreRatios[5] = {0, 1, 5, 15, 25};
int8_t lowest = 0;
int8_t count = 0;
int8_t k = 0, y = manager->y + 3;
do {
if (y < ROW_END && manager->pool[y] == 0xFFFFU) {
count++;
lowest = ROW_END - y;
memmove(manager->pool + 1, manager->pool, sizeof(uint16_t) * y);
memmove(control->color[1], control->color[0], sizeof(int8_t[16]) * y);
} else {
--y;
++k;
}
} while (y >= manager->y && k < 4);
lowest--;
control->erasedTotal += count;
control->score += scores[count] + lowest * scoreRatios[count] + 1;
if (count > 0) {
++control->erasedCount[count - 1];
}
giveTetris(manager, control);
setPoolColor(manager, control);
printScore(manager, control);
return (count > 0);
}
// =============================================================================
bool keydownControl(TetrisManager *manager, TetrisControl *control, int key) {
static signed char lastAction;
static bool ret = false;
if (general.model == 0 && lastAction >= 0) {
gotoxyWithFullwidth(27, 10 + lastAction);
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x0B);
buffer_print("□");
}
if (key == 13) { // Pause/Unpause
lastAction = 6;
control->pause = !control->pause;
gotoxyWithFullwidth(27, 10 + lastAction);
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x0B);
if (control->pause) {
buffer_print("■");
} else {
buffer_print("□");
}
}
if (control->pause) {
return false;
}
switch (key) {
case 'w':
case 'W':
case 72:
lastAction = 3;
control->clockwise = true;
ret = rotateTetris(manager, control);
break;
case 'a':
case 'A':
case 75:
lastAction = 0;
control->direction = 0;
ret = horzMoveTetris(manager, control);
break;
case 'd':
case 'D':
case 77:
lastAction = 1;
control->direction = 1;
ret = horzMoveTetris(manager, control);
break;
case 's':
case 'S':
case 80:
lastAction = 2;
ret = moveDownTetris(manager, control);
break;
case ' ':
lastAction = 5;
dropDownTetris(manager, control);
ret = true;
break;
case 'x':
case 'X':
case '0':
lastAction = 4;
control->clockwise = false;
ret = rotateTetris(manager, control);
break;
default:
lastAction = -1;
break;
}
if (general.model == 0 && lastAction >= 0) {
gotoxyWithFullwidth(27, 10 + lastAction);
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x0B);
buffer_print("■");
}
return ret;
}
// =============================================================================
inline void gotoxyWithFullwidth(short x, short y) {
static COORD cd;
cd.X = (short)(x << 1);
cd.Y = y;
buffer_SetConsoleCursorPosition(hConsoleOutput, cd);
}
// =============================================================================
int mainMenu() {
static int indexTotal = 3;
if (easterEgg[3]) {
indexTotal = 2; // Hide benchmark mode under debugging mode
}
static const char *const modelItem[] = {
"1. Play Now", "2. iBug's Marvel", "3. Benchmark mode"
};
#define secretNum 3
static const char *const secretCode[secretNum] = {"xzsyw", "sososo", "boost"};
static char secretKey[1024];
signed long int index = 0, secretIndex = 0, ch;
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x0F);
gotoxyWithFullwidth(14, 5);
buffer_print("┏━━━━━━━━━━┓");
gotoxyWithFullwidth(14, 6);
buffer_print("┃ Tetris with AI ┃");
gotoxyWithFullwidth(14, 7);
buffer_printf("┃ Version: %s", INFO_VERSION);
gotoxyWithFullwidth(25, 7);
buffer_print("┃");
gotoxyWithFullwidth(14, 8);
buffer_print("┗━━━━━━━━━━┛");
gotoxyWithFullwidth(14, 9);
buffer_printf("Author: %s", INFO_AUTHOR);
buffer_SetConsoleTextAttribute(hConsoleOutput, 0xF0);
for (int i = 0; i < indexTotal; i++) {
gotoxyWithFullwidth(14, 14 + 2 * i);
buffer_printf("%2s%s%2s", "", modelItem[i], "");
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x0F);
}
do {
flushPrint();
ch = _getch();
switch (ch) {
/*
// For cheat keys
case 'w': case 'W': case '8': case 72: // Up
case 'a': case 'A': case '4': case 75: // Left
case 'd': case 'D': case '6': case 77: // Right
case 's': case 'S': case '2': case 80: // Down
*/
//===========================================
case 72:
case 75: // U/L
case 77:
case 80: // D/R
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x0F);
gotoxyWithFullwidth(14, 14 + 2 * index);
buffer_printf("%2s%s%2s", "", modelItem[index], "");
if (ch == 72 || ch == 75) {
index--;
if (index < 0) {
index += indexTotal;
}
}
if (ch == 77 || ch == 80) {
index++;
if (index >= indexTotal) {
index -= indexTotal;
}
}
buffer_SetConsoleTextAttribute(hConsoleOutput, 0xF0);
gotoxyWithFullwidth(14, 14 + 2 * index);
buffer_printf("%2s%s%2s", "", modelItem[index], "");
flushPrint();
break;
case ' ':
case 13: // Space/Enter
return index;
case 27: // Esc
exit(0);
return -1;
case 8: // Backspace
memset(secretKey, 0, sizeof(secretKey));
secretIndex = 0;
break;
default:
if (ch >= 'A' && ch <= 'Z') {
ch += 0x20;
}
if (ch < 'a' || ch > 'z') {
break;
}
secretKey[secretIndex] = ch;
secretIndex++;
for (int i = 0; i < secretNum; i++) {
if (strcmp(secretKey, secretCode[i]) == 0) {
easterEgg[i] = !easterEgg[i];
memset(secretKey, 0, sizeof(secretKey));
secretIndex = 0;
}
}
break;
}
} while (1);
}
// =============================================================================
void printPoolBorder() {
int8_t y;
buffer_SetConsoleTextAttribute(hConsoleOutput, 0xF0);
for (y = ROW_BEGIN; y < ROW_END; ++y) {
gotoxyWithFullwidth(10, y - 3);
buffer_print(" ");
gotoxyWithFullwidth(23, y - 3);
buffer_print(" ");
}
gotoxyWithFullwidth(10, y - 3);
buffer_print(" ");
flushPrint();
}
inline void gotoxyInPool(short x, short y) {
gotoxyWithFullwidth(x + 9, y - 3);
}
// =============================================================================
// iBug's "Sync" implementation
static uint16_t ppool[16][28] = {{0}};
void printTetrisPool(const TetrisManager *manager,
const TetrisControl *control) {
int8_t x, y;
printNextTetris(manager);
for (y = ROW_BEGIN; y < ROW_END; ++y) {
for (x = COL_BEGIN; x < COL_END; ++x) {
if ((manager->pool[y] >> x) & 1) {
if (control->color[y][x] == ppool[x][y])
continue;
gotoxyInPool(x, y);
buffer_SetConsoleTextAttribute(hConsoleOutput, control->color[y][x]);
buffer_print("■");
ppool[x][y] = control->color[y][x];
} else {
if (ppool[x][y] == 0)
continue;
// SetConsoleTextAttribute(hConsoleOutput, 0);
gotoxyInPool(x, y);
buffer_print(" ");
ppool[x][y] = 0;
}
}
}
flushPrint();
}
// =============================================================================
void printCurrentTetris(const TetrisManager *manager,
const TetrisControl *control) {
int8_t x, y;
y = (manager->y > ROW_BEGIN) ? (manager->y - 1) : ROW_BEGIN;
for (; y < ROW_END && y < manager->y + 4; ++y) {
x = (manager->x > COL_BEGIN) ? (manager->x - 1) : COL_BEGIN;
for (; x < COL_END && x < manager->x + 5; ++x) {
gotoxyInPool(x, y);
if ((manager->pool[y] >> x) & 1) {
buffer_SetConsoleTextAttribute(hConsoleOutput, control->color[y][x]);
buffer_print("■");
ppool[x][y] = control->color[y][x];
} else {
if (ppool[x][y] == 0)
continue;
// SetConsoleTextAttribute(hConsoleOutput, 0);
buffer_print(" ");
ppool[x][y] = 0;
}
}
}
flushPrint();
}
// =============================================================================
void printNextTetris(const TetrisManager *manager) {
int8_t i;
uint16_t tetris;
tetris = gs_uTetrisTable[manager->type[1]][manager->orientation[1]];
buffer_SetConsoleTextAttribute(hConsoleOutput, manager->type[1] | 8);
for (i = 0; i < 16; ++i) {
gotoxyWithFullwidth((i & 3) + 27, (i >> 2) + 2);
((tetris >> i) & 1) ? buffer_print("■") : buffer_print(" ");
}
if (general.benchmark) {
// Don't show the second one under benchmark mode
return;
} else {
// The second one shouldn't have color
tetris = gs_uTetrisTable[manager->type[2]][manager->orientation[2]];
buffer_SetConsoleTextAttribute(hConsoleOutput, 8);
for (i = 0; i < 16; ++i) {
gotoxyWithFullwidth((i & 3) + 32, (i >> 2) + 2);
((tetris >> i) & 1) ? buffer_print("■") : buffer_print(" ");
}
}
}
// =============================================================================
void printScore(const TetrisManager *manager, const TetrisControl *control) {
int8_t i;
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x0E);
gotoxyWithFullwidth(5, 6);
buffer_printf("%u", control->score);
for (i = 0; i < 4; ++i) {
gotoxyWithFullwidth(6, 10 + i);
buffer_printf("%u", control->erasedCount[i]);
}
gotoxyWithFullwidth(6, 8);
buffer_printf("%u", control->erasedTotal);
for (i = 0; i < 7; ++i) {
gotoxyWithFullwidth(6, 17 + i);
buffer_printf("%u", control->tetrisCount[i]);
}
gotoxyWithFullwidth(6, 15);
buffer_printf("%u", control->tetrisTotal);
}
// =============================================================================
void printPrompting(const TetrisControl *control) {
static const char *const modelName[] = {"Play Now",
"iBug's Marvel"
};
static const char *const easterEggName = "Master's Spell";
static const char *const tetrisName = "ITLJZSO";
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x0E);
gotoxyWithFullwidth(1, 1);
if (general.benchmark) {
buffer_print("■Benchmark Mode");
} else {
if (easterEgg[0]) {
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x0C);
buffer_print("■%s");
buffer_print(easterEggName);
buffer_SetConsoleTextAttribute(hConsoleOutput, 0x0E);
}
buffer_print("■");
buffer_print(control->model ? modelName[0] : modelName[1]);
}
gotoxyWithFullwidth(1, 3);
buffer_print("□[Esc] Exit");
gotoxyWithFullwidth(1, 6);
buffer_printf("■Score %u", control->score);
gotoxyWithFullwidth(1, 8);
buffer_printf("■Erased: %u", control->erasedTotal);
int8_t i;
for (i = 0; i < 4; ++i) {
gotoxyWithFullwidth(2, 10 + i);
buffer_printf("□%dL: %u", i + 1, control->erasedCount[i]);
}
gotoxyWithFullwidth(1, 15);
buffer_printf("■Blocks: %u", control->tetrisTotal);
for (i = 0; i < 7; ++i) {
gotoxyWithFullwidth(2, 17 + i);
buffer_printf("□%c: %u", tetrisName[i], control->tetrisCount[i]);
}
buffer_SetConsoleTextAttribute(hConsoleOutput, 0xF);
if (general.benchmark) {
// Don't show the second one under benchmark mode