-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
769 lines (726 loc) · 22.5 KB
/
main.cpp
File metadata and controls
769 lines (726 loc) · 22.5 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
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
#include "cmath"
#include "headers/animate.h"
#include "headers/constant.h"
#include "headers/database.h"
#include "headers/gui.h"
#include "headers/sound.h"
#define RAYGUI_IMPLEMENTATION
#include "headers/raygui.h"
#include "map"
#include "raylib.h"
#define GUI_MENU_IMPLEMENTATION
#include "headers/gui_menu.h"
#include <bits/stdc++.h>
#include <ios>
#include <iostream>
#include <string>
#include <utility>
using namespace std;
string board[8][8];
Color colorBoard[8][8];
vector<vector<Color>> selectedBoard(8, vector<Color>(8));
int score = 0;
string score_str = "0";
int menu_time = 0;
int max_score = 0;
// Need to add empassant, Castling, Check, checkmate, oawn promotion
string UpdateTime(int time_f) {
int hours = time_f / 3600;
int minutes = (time_f % 3600) / 60;
int seconds = time_f % 60;
ostringstream oss;
oss << setfill('0') << setw(2) << hours << ":" << setfill('0')
<< setw(2) << minutes << ":" << setfill('0') << setw(2)
<< seconds;
return oss.str();
}
class Piece {
string name;
public:
Piece(string name) { this->name = name; }
void virtual ViewMoves() = 0;
};
static bool inBounds(Vector2 pos) {
if (pos.x < 8 && pos.y < 8 && pos.x > -1 && pos.y > -1)
return true;
return false;
}
class HandlePiece {
public:
static void Pawn(string name, Vector2 pos) {
char enemy = name[0] == 'w' ? 'b' : 'w';
bool homeRow = ((name[0] == 'w') ? 7 - pos.y : pos.y) == 1;
int mag = homeRow ? 2 : 1;
// Direction
int dir = name[0] == 'w' ? 1 : -1;
// If dir + color the board from pos.y += dir for mag
for (int i = 1; i <= mag; i++) {
if (inBounds(Vector2{pos.x, pos.y - i * dir})) {
if (board[(int)pos.y - i * dir][(int)pos.x] != "")
break;
// selectedBoard[(int)pos.y - i * dir][(int)pos.x] = BLUE;
ExpandAnimation *anim = new ExpandAnimation(
{(float)pos.x, (float)pos.y - i * dir}, 0.2, &selectedBoard, BLUE);
AnimationManager::addAnimation(anim);
}
}
// Check 1 diagonal
vector<int> dy = {-1, -1};
vector<int> dx = {-1, +1};
for (int y : dy) {
for (int x : dx) {
int valx = pos.x + x;
int valy = pos.y + dir * y;
if (valx > -1 && valx < 8 && valy > -1 && valy < 8)
if (board[valy][valx][0] == enemy) {
// selectedBoard[valy][valx] = RED;
ExpandAnimation *anim = new ExpandAnimation(
{(float)valx, (float)valy}, 0.2, &selectedBoard, RED);
AnimationManager::addAnimation(anim);
}
}
}
}
static void Bishop(string name, Vector2 pos) {
char enemy = name[0] == 'w' ? 'b' : 'w';
char friendly = name[0] == 'w' ? 'w' : 'b';
// Highlight all the diagonals if in bounds
// Top Right
for (int i = 1; i < 8; i++) {
Vector2 newPos = {pos.x + i, pos.y - i};
if (inBounds(Vector2{pos.x + i, pos.y - i})) {
// If it is a piece of the same color break
if (board[(int)newPos.y][(int)newPos.x][0] == friendly)
break;
Color color =
board[(int)pos.y - i][(int)pos.x + i][0] == enemy ? RED : BLUE;
ExpandAnimation *anim =
new ExpandAnimation(newPos, 0.2, &selectedBoard, color);
AnimationManager::addAnimation(anim);
if (board[(int)pos.y - i][(int)pos.x + i] != "")
break;
}
}
// Top Left
for (int i = 1; i < 8; i++) {
Vector2 newPos = {pos.x - i, pos.y - i};
if (inBounds(Vector2{pos.x - i, pos.y - i})) {
if (board[(int)newPos.y][(int)newPos.x][0] == friendly)
break;
Color color =
board[(int)pos.y - i][(int)pos.x - i][0] == enemy ? RED : BLUE;
ExpandAnimation *anim =
new ExpandAnimation(newPos, 0.2, &selectedBoard, color);
AnimationManager::addAnimation(anim);
if (board[(int)pos.y - i][(int)pos.x - i] != "")
break;
}
}
// Bottom Right
for (int i = 1; i < 8; i++) {
Vector2 newPos = {pos.x + i, pos.y + i};
if (inBounds(newPos)) {
if (board[(int)newPos.y][(int)newPos.x][0] == friendly)
break;
Color color =
board[(int)newPos.y][(int)newPos.x][0] == enemy ? RED : BLUE;
// selectedBoard[(int)newPos.y][(int)newPos.x] = color;
ExpandAnimation *anim =
new ExpandAnimation(newPos, 0.2, &selectedBoard, color);
AnimationManager::addAnimation(anim);
if (board[(int)newPos.y][(int)newPos.x] != "")
break;
}
}
// Bottom Left
for (int i = 1; i < 8; i++) {
Vector2 newPos = {pos.x - i, pos.y + i};
if (inBounds(Vector2{pos.x - i, pos.y + i})) {
if (board[(int)newPos.y][(int)newPos.x][0] == friendly)
break;
Color color =
board[(int)pos.y + i][(int)pos.x - i][0] == enemy ? RED : BLUE;
ExpandAnimation *anim =
new ExpandAnimation(newPos, 0.2, &selectedBoard, color);
AnimationManager::addAnimation(anim);
if (board[(int)pos.y + i][(int)pos.x - i] != "")
break;
}
}
}
static void Rook(string name, Vector2 pos) {
// Highlight all the rows if in bounds
char enemy = name[0] == 'w' ? 'b' : 'w';
// Go Horizontal Right
for (int i = pos.x + 1; i < 8; i++) {
if (board[(int)pos.y][i] == "") {
// selectedBoard[(int)pos.y][i] = BLUE;
ExpandAnimation *anim =
new ExpandAnimation({(float)i, pos.y}, 0.2, &selectedBoard, BLUE);
AnimationManager::addAnimation(anim);
} else if (board[(int)pos.y][i][0] == enemy) {
// selectedBoard[(int)pos.y][i] = RED;
ExpandAnimation *anim =
new ExpandAnimation({(float)i, pos.y}, 0.2, &selectedBoard, RED);
AnimationManager::addAnimation(anim);
break;
} else {
break;
}
}
// Go Horizontal Left
for (int i = pos.x - 1; i > -1; i--) {
if (board[(int)pos.y][i] == "") {
// selectedBoard[(int)pos.y][i] = BLUE;
ExpandAnimation *anim =
new ExpandAnimation({(float)i, pos.y}, 0.2, &selectedBoard, BLUE);
AnimationManager::addAnimation(anim);
} else if (board[(int)pos.y][i][0] == enemy) {
// selectedBoard[(int)pos.y][i] = RED;
ExpandAnimation *anim =
new ExpandAnimation({(float)i, pos.y}, 0.2, &selectedBoard, RED);
AnimationManager::addAnimation(anim);
break;
} else {
break;
}
}
// Go Vertical Up
for (int i = pos.y - 1; i > -1; i--) {
if (board[i][(int)pos.x] == "") {
// selectedBoard[i][(int)pos.x] = BLUE;
ExpandAnimation *anim =
new ExpandAnimation({pos.x, (float)i}, 0.2, &selectedBoard, BLUE);
AnimationManager::addAnimation(anim);
} else if (board[i][(int)pos.x][0] == enemy) {
// selectedBoard[i][(int)pos.x] = RED;
ExpandAnimation *anim =
new ExpandAnimation({pos.x, (float)i}, 0.2, &selectedBoard, RED);
AnimationManager::addAnimation(anim);
break;
} else {
break;
}
}
// Go Vertical Down
for (int i = pos.y + 1; i < 8; i++) {
if (board[i][(int)pos.x] == "") {
// selectedBoard[i][(int)pos.x] = BLUE;
ExpandAnimation *anim =
new ExpandAnimation({pos.x, (float)i}, 0.2, &selectedBoard, BLUE);
AnimationManager::addAnimation(anim);
} else if (board[i][(int)pos.x][0] == enemy) {
// selectedBoard[i][(int)pos.x] = RED;
ExpandAnimation *anim =
new ExpandAnimation({pos.x, (float)i}, 0.2, &selectedBoard, RED);
AnimationManager::addAnimation(anim);
break;
} else {
break;
}
}
}
void static Knight(string name, Vector2 pos) {
// Highlight all the rows if in bounds
char enemy = name[0] == 'w' ? 'b' : 'w';
int delrow[8] = {+2, -1, +1, -2, +2, +1, -1, -2};
int delcol[8] = {+1, +2, +2, +1, -1, -2, -2, -1};
for (int i = 0; i < 8; i++) {
Vector2 newP = {pos.x + delrow[i], pos.y + delcol[i]};
if (inBounds(newP)) {
if (board[(int)newP.y][(int)newP.x] == "") {
// selectedBoard[(int)newP.y][(int)newP.x] = BLUE;
ExpandAnimation *anim =
new ExpandAnimation(newP, 0.2, &selectedBoard, BLUE);
AnimationManager::addAnimation(anim);
}
if (board[(int)newP.y][(int)newP.x][0] == enemy) {
// selectedBoard[(int)newP.y][(int)newP.x] = RED;
ExpandAnimation *anim =
new ExpandAnimation(newP, 0.2, &selectedBoard, RED);
AnimationManager::addAnimation(anim);
}
}
}
}
static void King(string name, Vector2 pos) {
char enemy = name[0] == 'w' ? 'b' : 'w';
int dx[] = {0, 1, -1};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int x = pos.x + dx[i];
int y = pos.y + dx[j];
if (x < 8 && y < 8 && x > -1 && y > -1) {
if (board[y][x][0] == enemy) {
// selectedBoard[y][x] = RED;
ExpandAnimation *anim = new ExpandAnimation(
{(float)x, (float)y}, 0.2, &selectedBoard, RED);
AnimationManager::addAnimation(anim);
}
if (board[y][x] == "") {
// selectedBoard[y][x] = BLUE;
ExpandAnimation *anim = new ExpandAnimation(
{(float)x, (float)y}, 0.2, &selectedBoard, BLUE);
AnimationManager::addAnimation(anim);
}
}
}
}
}
static void Queen(string name, Vector2 pos) {
Bishop(name, pos);
Rook(name, pos);
}
static bool PieceEval(string name, Vector2 pos) {
if (name.length() < 2)
return 0;
char piece = name[1];
switch (piece) {
case 'P':
Pawn(name, pos);
break;
case 'N':
Knight(name, pos);
break;
case 'B':
Bishop(name, pos);
break;
case 'R':
Rook(name, pos);
break;
case 'Q':
Queen(name, pos);
break;
case 'K':
King(name, pos);
break;
default:
cout << "Piece not found";
break;
}
return true;
}
private:
};
class Board {
map<string, Texture2D> texs;
Vector2 start = {-1, -1}, end = {-1, -1};
double timeStarted;
bool set = false;
vector<pair<Vector2, Vector2>> moves;
int moveCount = 0;
// 0 is NPC 1 is player
char playerColor = 'w';
// bool enPassant[2];
// bool whiteKing[2];
// King & Queen
bool castleB[2];
bool castleW[2];
GAME_STATE gameState = MENU;
public:
void Reset() {
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++) {
Color squareColor = ((i + j) % 2 == 0) ? BLACK : WHITE;
colorBoard[i][j] = squareColor;
board[i][j] = "";
selectedBoard[i][j] = WHITE;
}
moves.erase(moves.begin(), moves.end());
moveCount = 0;
timeStarted = GetTime();
set = false;
}
void setGameState(GAME_STATE state) { gameState = state; }
Board() {
Reset();
// Load textures for each pieces
string pieces[] = {"wP", "wN", "wB", "wR", "wQ", "wK",
"bP", "bN", "bB", "bR", "bQ", "bK"};
for (string name : pieces) {
string texName = "./assets/" + name + ".png";
texs[name] = LoadTexture(texName.c_str());
}
castleB[0] = castleB[1] = false;
castleW[0] = castleW[1] = false;
NewGame();
}
void ParseMoves(string moveSet) {
// Break the string into pieces
string move;
vector<string> movesS = breakBySpace(moveSet);
for (string m : movesS) {
Vector2 start = {((float)m[0] - 'a'), 7 - ((float)m[1] - '1')};
Vector2 end = {(float)m[2] - 'a', 7 - ((float)m[3] - '1')};
pair<Vector2, Vector2> p = {start, end};
moves.push_back(p);
}
}
void setBoard(string FEN) {
Reset();
int i = 0, j = 0;
for (char c : FEN) {
if (c == ' ') {
break;
}
if (c == '/') {
i++;
j = 0;
continue;
}
if (c <= '9' && c >= '1') {
j += (c - '0');
}
if (c != '/' && (c > '9' || c < '1')) {
// This character represents a piece
string tex_name = "";
if (c <= 'z' && c >= 'a') {
tex_name += "b";
c = c - 'a' + 'A';
} else
tex_name += "w";
tex_name += c;
board[i][j] = tex_name;
j++;
}
}
string FEN_add =
FEN.substr(FEN.find(' ') + 1, FEN.length() - FEN.find(' '));
vector<string> FEN_addPiece = breakBySpace(FEN_add);
playerColor = (FEN_addPiece[0][0] == 'b') ? 'w' : 'b';
// Castling setting
for (char c : FEN_addPiece[1]) {
switch (c) {
case 'k':
castleB[0] = true;
break;
case 'q':
castleB[1] = true;
break;
case 'K':
castleW[0] = true;
break;
case 'Q':
castleW[1] = true;
break;
}
}
}
void Draw() {
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
// Draw the square with color from colorBoard
DrawRectangle(x * SQUARE_SIZE, y * SQUARE_SIZE, SQUARE_SIZE,
SQUARE_SIZE, colorBoard[y][x]);
}
}
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
// Draw colored square on selectedBoard if the color is not WHITE
if (!CompareColor(selectedBoard[y][x], WHITE)) {
DrawRectangle(x * SQUARE_SIZE + gap, y * SQUARE_SIZE + gap,
SQUARE_SIZE - gap * 2, SQUARE_SIZE - gap * 2,
selectedBoard[y][x]);
}
}
}
AnimationManager::Draw();
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
// Draw the piece if the board cell is not empty
if (board[y][x] != "") {
DrawTexturePro(texs[board[y][x]], Rectangle{0, 0, 16, 16},
Rectangle{(float)x * SQUARE_SIZE,
(float)y * SQUARE_SIZE, SQUARE_SIZE,
SQUARE_SIZE},
Vector2{0, 0}, 0, WHITE);
}
}
}
}
void HandleClick() {
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
Vector2 pos = MousePos();
cout << pos.y << " " << pos.x << endl;
// Find the start position
// cout << pos.y << " " << pos.x << endl;
if (set) {
// Means that it has selected a piece, now verify this position to be
// 1. Blue
// 2. That of an enemy piece / empty
// cout << "START: " << start.y << " " << start.x << endl;
if (inBounds(pos) && ValidMove(pos) && VerifyMove(pos)) {
// Make this move
PlayMove();
if (moveCount < moves.size())
EnemyMove();
else {
UpdateScore();
NewGame();
}
}
// cout << ValidMove(pos) << " " << VerifyMove(pos) << endl;
set = false;
start = {-1, -1};
// Verify that the next move was in the moveset
// then play enemies turn
}
ResetBoardColor();
if (inBounds(pos) && board[(int)pos.y][(int)pos.x][0] == playerColor) {
set = HandlePiece::PieceEval(board[(int)pos.y][(int)pos.x], pos);
}
if (set)
start = pos;
else
start = {-1, -1};
}
}
void PrintMoves() {
if (moves.size() == 0) {
return;
}
for (pair<Vector2, Vector2> move : moves) {
cout << "(" << (int)move.first.y << ", " << (int)move.first.x << ") -> ("
<< (int)move.second.y << ", " << (int)move.second.x << ")" << endl;
}
}
void EnemyMove() {
Vector2 enemyStart = moves[moveCount].first;
Vector2 enemyEnd = moves[moveCount].second;
board[(int)enemyEnd.y][(int)enemyEnd.x] =
board[(int)enemyStart.y][(int)enemyStart.x];
board[(int)enemyStart.y][(int)enemyStart.x] = "";
MovePieceAnim *move = new MovePieceAnim(
&texs[board[(int)enemyEnd.y][(int)enemyEnd.x]], enemyStart, enemyEnd);
AnimationManager::addAnimation(move);
PlaySound(SoundMap::soundMap[Move]);
moveCount++;
}
bool VerifyMove(Vector2 pos) {
Vector2 correctStart = moves[moveCount].first;
Vector2 correctEnd = moves[moveCount].second;
if (start.x == correctStart.x && start.y == correctStart.y &&
pos.x == correctEnd.x && pos.y == correctEnd.y)
return true;
// If one shot mode then the game has ended
if (gameState == ONE_SHOT) {
cout << "YOU LOST: " << score << endl;
NewGame();
// Reset SCORE
score = 0;
score_str = to_string(score);
timeStarted = GetTime();
gameState = MENU;
}
return false;
}
void PlayMove() {
Vector2 correctStart = moves[moveCount].first;
Vector2 correctEnd = moves[moveCount].second;
board[(int)correctEnd.y][(int)correctEnd.x] =
board[(int)correctStart.y][(int)correctStart.x];
board[(int)correctStart.y][(int)correctStart.x] = "";
MovePieceAnim *move =
new MovePieceAnim(&texs[board[(int)correctEnd.y][(int)correctEnd.x]],
correctStart, correctEnd);
AnimationManager::addAnimation(move);
PlaySound(SoundMap::soundMap[Move]);
moveCount++;
}
void Cheat() {
PlayMove();
if (moveCount < moves.size())
EnemyMove();
else {
UpdateScore();
NewGame();
}
}
void UpdateScore() {
double detalTime = GetTime() - timeStarted - menu_time;
if (detalTime >= 2 * 60)
score += 100;
else if (detalTime >= 60)
score += 200;
else if (detalTime >= 30)
score += 300;
else if (detalTime >= 20)
score += 400;
else if (detalTime >= 10)
score += 500;
else
score += 1000;
// Make an animation for the score
score_str = to_string(score);
PlaySound(SoundMap::soundMap[SCORE]);
TextAnim *scoreAnim =
new TextAnim(score_str, Vector2{650, 800}, Vector2{650, 700});
AnimationManager::addAnimation(scoreAnim);
}
void NewGame() {
pair<string, string> Board_Moves = Filer::ParseLine();
cout << Board_Moves.first << endl;
cout << Board_Moves.second << endl;
setBoard(Board_Moves.first);
ParseMoves(Board_Moves.second);
cout << "THE MOVES -------------->" << endl;
PrintMoves();
EnemyMove();
timeStarted = GetTime();
}
private:
vector<string> breakBySpace(string str) {
vector<string> tokens;
istringstream iss(str);
for (string s; iss >> s;) {
tokens.push_back(s);
}
return tokens;
}
void ResetBoardColor() {
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++) {
Color squareColor = ((i + j) % 2 == 0) ? BLACK : WHITE;
colorBoard[i][j] = squareColor;
selectedBoard[i][j] = WHITE;
}
AnimationManager::clearBoard();
}
Vector2 MousePos() {
Vector2 mouse = GetMousePosition();
Vector2 pos = {floor(mouse.x / SQUARE_SIZE), floor(mouse.y / SQUARE_SIZE)};
return pos;
}
bool ValidMove(Vector2 pos) {
// cout << "pos: " << pos.y << " " << pos.x << endl;
if (!CompareColor(selectedBoard[(int)pos.y][(int)pos.x], BLUE) &&
!CompareColor(selectedBoard[(int)pos.y][(int)pos.x], RED))
return false;
// Check if the piece is opp to the start and or empty
if (board[(int)pos.y][(int)pos.x] == "")
return true;
char startPiece = board[(int)start.y][(int)start.x][0];
char endPiece = board[(int)pos.y][(int)pos.x][0];
if (startPiece != endPiece)
return true;
return false;
}
bool CompareColor(Color a, Color b) {
Vector3 a_V = ColorToHSV(a);
Vector3 b_V = ColorToHSV(b);
return a_V.x == b_V.x && a_V.y == b_V.y && a_V.z == b_V.z;
}
};
unordered_multiset<Animation *> AnimationManager::animations;
map<SoundType, Sound> SoundMap::soundMap;
class Game {
Board b;
Label Time;
Label ScoreLabel;
string time = "00:00:00";
SmallButton Button;
BigButton OneShot;
BigButton Timed;
BigButton No_Limit;
GAME_STATE gameState = MENU;
Texture2D GuiSheet = LoadTexture("./assets/GUI.png");
GuiMenuState state = InitGuiMenu();
Texture2D bg;
public:
void setState(GAME_STATE state) { gameState = state; }
Game()
: Button(Vector2{0, 800}, &GuiSheet),
OneShot({SCREEN_WIDTH / 2.0 - 48 * 3 / 2.0, 300}, "ONE SHOT", &GuiSheet,
ONE_SHOT, &gameState),
Timed({SCREEN_WIDTH / 2.0 - 48 * 3 / 2.0, 400}, "TIMED", &GuiSheet,
TIMED, &gameState),
No_Limit({SCREEN_WIDTH / 2.0 - 48 * 3 / 2.0, 500}, "NO LIMIT",
&GuiSheet, NO_LIMIT, &gameState),
ScoreLabel(Vector2{650, 800}, &score_str),
Time(Vector2{400 - 48 * 3 / 2, 800}, &time) {
bg = LoadTexture("./assets/background.png");
SoundMap::Init();
// Make a function that updates the string time
}
void HANDLE_GAME_STATE() {
string max_score_str = "Max Score: " + to_string(max_score);
switch (gameState) {
case MENU:
// Draw the MENU
DrawTexture(bg, 0, 0, WHITE);
DrawText(max_score_str.c_str(),
SCREEN_WIDTH / 2 - MeasureText(max_score_str.c_str(), 20) / 2,
100 + 10, 20, WHITE);
// GuiMenu(&state, gameState);
OneShot.Draw();
OneShot.Update();
Timed.Draw();
Timed.Update();
No_Limit.Draw();
No_Limit.Update();
break;
case ONE_SHOT:
// Draw for ONE_SHOT
time = UpdateTime((GetTime() - menu_time));
if (IsKeyPressed(KEY_RIGHT))
b.Cheat();
b.Draw();
Button.Update();
Time.Draw();
ScoreLabel.Draw();
Button.Draw();
AnimationManager::Update();
b.HandleClick();
break;
case TIMED:
// Draw for TIMED
// Timer for 5 minutes
time = UpdateTime(1 * 60 - (GetTime() - menu_time));
if (1 * 60 - (GetTime() - menu_time) <= 0) {
gameState = MENU;
}
if (IsKeyPressed(KEY_RIGHT))
b.Cheat();
b.Draw();
Button.Update();
Time.Draw();
ScoreLabel.Draw();
Button.Draw();
AnimationManager::Update();
b.HandleClick();
break;
case NO_LIMIT:
time = UpdateTime((GetTime() - menu_time));
if (IsKeyPressed(KEY_RIGHT))
b.Cheat();
b.Draw();
Button.Update();
Time.Draw();
ScoreLabel.Draw();
Button.Draw();
AnimationManager::Update();
b.HandleClick();
break;
}
b.setGameState(gameState);
if (gameState == MENU)
menu_time = GetTime();
max_score = max(max_score, score);
}
};
int main() {
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Chess Grid");
InitAudioDevice();
SetTargetFPS(60);
Game g;
// GuiLoadStyle("./assets/Ashes.rgs");
while (!WindowShouldClose()) // Detect window close button or ESC key
{
BeginDrawing();
ClearBackground(BLACK);
g.HANDLE_GAME_STATE();
EndDrawing();
}
CloseWindow();
return 0;
}