-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckers.cpp
More file actions
1516 lines (1326 loc) · 60.7 KB
/
Copy pathcheckers.cpp
File metadata and controls
1516 lines (1326 loc) · 60.7 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
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
// @file checkers.cpp
// @brief contains the full code for the checkers game
// @author Firaol Feyisa
// @date 2024-08-01
// @version 1.0
// @bug No known bugs. Rport bugs to flexflash09@gmail.com or @firaflash via all platform
// @rules you can know the rules of the movments by taping the button " help " in the game or go to the "help " function
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include "raylib.h"
using namespace std;
const int BOARD_WIDTH = 800;
const int BOARD_HEIGHT = 800;
const int CELL_SIZE = 100;
const int CELL_CENTER_POS = CELL_SIZE / 2;
const int QORKI_SIZE = 30;
enum cellType{
//You might want one more cell type.
emptyCell,
player1Qorki,
player2Qorki,
player1KingQorki,
player2KingQorki
};
struct Cell{
int row;
int col;
int cellType;
int cellSize = CELL_SIZE;
};
struct Qorki{
Color qorkiColor;
int qorkiSize = QORKI_SIZE;
int qorkiOrientation;
};
struct Board{
int boardWidth;
int boardHeight;
Color boardColor;
};
struct Game{
Board board;
Cell cellInfo[8][8];
std::string playerOneName;
std::string playerTwoName;
int p1;
int p2;
bool turn;
int winner;
};
struct Button
{
Rectangle rect;
Color color;
};
/// @brief initializes the game
/// @param game the game to initialize
void initGame(Game& game);
/// @brief initializes the board
/// @param board the board to initialize
void initBoard(Board& board);
/// @brief draws the board based on board properties
/// @param board the board to draw
void drawBoard(Board board);
/// @brief gets the cell type based on the row and col on initialization
/// @param row,col the row and col to get the cell type
int initCellType(int row, int col);
/// @brief draw the cells based on their type
/// @param cell,board the cells to draw and the board in which the cells will be drawn
void drawCellsOnBoard(Cell cell[8][8]);
/// @brief getCellColor based on the cell type
/// @param cell the cell to get the color
Color getCellColor(Cell cell);
/// @brief draw qorki based on the cell type
/// @param cell the cell to draw the qorki on
void drawQorki(Cell cell[8][8]);
/// @brief returns qokri color based on cell type.
/// @param cell the cell to get the cell type from and determine the qorki color.
Color getQorkiColor(Cell cell);
/// @brief updates the game cellInfo array based on user click
/// @param cells the cell infos of the current game.
void updateGame(Cell cells[8][8], Game& game, Sound& move);
/// @brief returns the cell the user clicked on
/// @param x,y the x and y coordinates of the click
Cell getCell(int x, int y);
/// @brief moves the qorki from the selected cell to the target cell
/// @param selectedCell,targetCell,cells the selected cell, target cell and the game cell info array
void moveQorki(Cell selectedCell, Cell targetCell, Cell cells[8][8], Sound& move);
/// @brief handles the qorki actions
/// @param selectedCell,targetCell,cells the selected cell, target cell and the game cell info array
void handleQorkiMove(Cell selectedCell, Cell targetCell, Cell cells[8][8], Game& game, Sound& move);
void handleKingQorkiMove(Cell selectedCell,Cell targetCell,Cell cells[8][8], Game& game, Sound& move);
/// @brief checks if a move is a legal checkers move
/// @param selectedCell,targetCell,cells the selected cell, target cell and the game cell info array
bool isMoveValid(Cell selectedCell, Cell targetCell, Cell cells[8][8], Game& game, Sound& move);
bool isKingMoveValid(Cell selectedCell, Cell targetCell, Cell cells[8][8], Game& game, Sound& move);
/// @brief removes the cell from the game
/// @param cell,cells the cell to remove and the game cell info array
void removeQorki(Cell cell, Cell cells[8][8]);
/// @brief Determines the winner of the game based on the current move.
/// @param selectedCell, targetCell, cells, game the selected cell, target cell, game boardand the game cell info array
void winner(Cell selectedCell, Cell targetCell, Cell cells[8][8], Game& game);
/// @brief Checks if the mouse is hovering over the button.
/// @param button The button to check.
/// @return Returns true if the mouse is over the button, false otherwise.
bool is_mouse_over_button(Button button);
/// @brief Resets the game state and restarts the game.
/// @param game, move The game object to reset.
void resetGame(Game& game, Sound& move, Sound& click);
/// @brief Handles text input for saving or loading game states.
/// @param file, type The filename to save to or load from and Specifies whether the operation is a save or load action.
void text_input(std::string& file, std::string type, Sound& click);
/// @brief Loads a previously saved game from a file.
/// @param game The game object to load the state into.
void loadgame(Game& game, Sound& click);
/// @brief Saves the current game state to a file.
/// @param game The game object whose state is to be saved.
void savegame(Game& game, Sound& click);
/// @brief Displays the help page with instructions or information.
/// @param game the game board info
void help_page(Game &game, Sound& click);
/// @brief Gets name for the players.
/// @param game, the game board info.
void player_name(Game& game, Sound& click);
/// @brief Draws the text and box drawing on the game board.
/// @param game the game board info
void drawings(Game& game);
/// @brief checks if there is on more valid moves
/// @param selectedCell,targetCell,cells, game the selected cell, target cell, game board info and the game cell info array
bool stalmate(Cell selectedCell, Cell targetCell, Cell cells[8][8], Game& game);
int main(){
newgame:
Game game;
Sound move, click;
restart:
initGame(game);
initBoard(game.board);
open:
InitWindow((game.board.boardWidth)+300, game.board.boardHeight, "DAMA");
InitAudioDevice();
move = LoadSound("Game sound\\gamesound.wav");
click = LoadSound("Game sound\\click.mp3");
SetSoundVolume(move, 1.0f);
SetSoundVolume(click, 1.0f);
while (!WindowShouldClose()){
BeginDrawing();
ClearBackground(RAYWHITE);
drawBoard(game.board);
drawCellsOnBoard(game.cellInfo);
drawQorki(game.cellInfo);
updateGame(game.cellInfo, game, move);
drawings(game);
Color turn;
if(game.turn) {
turn = (Color){251, 251, 238, 255}; // made the turn indicator color the same as the qorki color
} else {
turn = (Color){226, 135, 67, 255}; // made the turn indicator color the same as the qorki color
}
DrawRectangle(BOARD_WIDTH + 120, 250, 30, 30, turn);
Rectangle Hshadow = {BOARD_WIDTH + 14, 724, 280, 50};
Button help;
help.rect = {BOARD_WIDTH + 10, 720, 280, 50};
help.color = GRAY;
float roundness = 0.6f; // Adjust roundness (0.0 for sharp corners, 1.0 for fully rounded)
int segments = 10; // Adjust smoothness (higher value means smoother corners)
DrawRectangleRounded(Hshadow, roundness,segments, BLACK);
DrawRectangleRounded(help.rect, roundness,segments, help.color);
DrawText("HELP", BOARD_WIDTH + 40, 735, 28, BLACK);
if((is_mouse_over_button(help)) && (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))){
PlaySound(click);
help_page(game, click);
UnloadSound(move);
CloseAudioDevice();
goto open;
}
//DrawRectangle(BOARD_WIDTH + 120, 250, 30, 30, turn);
Button restart;
Rectangle Rshadow = {BOARD_WIDTH + 14, 659, 280, 50};
restart.rect = {BOARD_WIDTH + 10, 655, 280, 50};
restart.color = GRAY;
DrawRectangleRounded(Rshadow, roundness,segments, BLACK);
DrawRectangleRounded(restart.rect, roundness,segments, restart.color);
DrawText("RESTART GAME", BOARD_WIDTH + 40, 670, 28, BLACK);
if((is_mouse_over_button(restart)) && (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))){
PlaySound(click);
WaitTime(0.5);
UnloadSound(move);
CloseAudioDevice();
CloseWindow();
goto restart;
}
//DrawRectangle(BOARD_WIDTH + 120, 250, 30, 30, turn);
Button load;
Rectangle Lshadow = {BOARD_WIDTH + 14, 594, 280, 50};
load.rect = {BOARD_WIDTH + 10, 590, 280, 50};
load.color = GRAY;
roundness = 0.6f;
segments = 10;
DrawRectangleRounded(Lshadow, roundness, segments, BLACK);
DrawRectangleRounded(load.rect, roundness, segments, load.color);
DrawText("LOAD A GAME", BOARD_WIDTH + 40, 605, 28, BLACK);
if((is_mouse_over_button(load)) && (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))){
PlaySound(click);
loadgame(game, click);
cout << "Game loaded! Player one name: " << game.playerOneName << endl;
cout << "Player one score: " << game.p1 << endl;
UnloadSound(move);
CloseAudioDevice();
goto open;
}
//DrawRectangle(BOARD_WIDTH + 120, 250, 30, 30, turn);
Button save;
save.rect = {BOARD_WIDTH + 10, 525, 280, 50};
save.color = GRAY;
Rectangle Sshadow = {BOARD_WIDTH + 14, 529, 280, 50};
DrawRectangleRounded(Sshadow, roundness, segments, BLACK);
DrawRectangleRounded(save.rect, roundness, segments, save.color);
DrawText("SAVE GAME", BOARD_WIDTH + 40, 540, 28, BLACK);
if((is_mouse_over_button(save)) && (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))){
PlaySound(click);
savegame(game, click);
cout << "Game saved!\n";
goto quit;
}
//DrawRectangle(BOARD_WIDTH + 120, 250, 30, 30, turn);
Button name;
name.rect = {BOARD_WIDTH + 10, 460, 280, 50};
name.color = GRAY;
Rectangle Nshadow = {BOARD_WIDTH + 14, 464, 280, 50};
DrawRectangleRounded(Nshadow, roundness, segments, BLACK);
DrawRectangleRounded(name.rect, roundness ,segments , name.color);
DrawText("ADD NAMES", BOARD_WIDTH + 40, 475, 28, BLACK);
if((is_mouse_over_button(name)) && (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))){
PlaySound(click);
player_name(game, click);
UnloadSound(move);
CloseAudioDevice();
goto open;
}
if (game.winner== 1) {
CloseWindow();
InitWindow(game.board.boardWidth/2, game.board.boardHeight/4, "Game Over");
Button button1, button2;
button1.rect = {50, 100, 115, 50};
button2.rect = {250, 100, 100, 50};
button1.color = SKYBLUE;
button2.color = SKYBLUE;
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("PLAYER ONE WON", game.board.boardWidth/8 + 10, 30, 20, SKYBLUE);
DrawRectangle(165, 105, 4, 45, BLACK);
DrawRectangle(55, 150, 114, 4, BLACK);
DrawRectangle(350, 105, 4, 45, BLACK);
DrawRectangle(255, 150, 99, 4, BLACK);
// Draw the buttons
DrawRectangleRec(button1.rect, button1.color);
DrawText("New Game", 60, 115, 20, BLACK);
DrawRectangleRec(button2.rect, button2.color);
DrawText("Quit", 280, 115, 20, BLACK);
if((is_mouse_over_button(button1)) && (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))){
PlaySound(click);
resetGame(game, move, click);
UnloadSound(move);
CloseAudioDevice();
CloseWindow();
goto newgame;
}else if((is_mouse_over_button(button2)) && (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))){
PlaySound(click);
CloseWindow();
goto quit;
}
EndDrawing();
}
} else if (game.winner == 2) {
CloseWindow();
InitWindow(game.board.boardWidth/2, game.board.boardHeight/4, "Game Over");
Button button1, button2;
button1.rect = {50, 100, 115, 50};
button2.rect = {250, 100, 100, 50};
button1.color = PINK;
button2.color = PINK;
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("PLAYER TWO WON", game.board.boardWidth/8 + 10, 30, 20, PINK);
DrawRectangle(165, 105, 4, 45, BLACK);
DrawRectangle(55, 150, 114, 4, BLACK);
DrawRectangle(350, 105, 4, 45, BLACK);
DrawRectangle(255, 150, 99, 4, BLACK);
// Draw the buttons
DrawRectangleRec(button1.rect, button1.color);
DrawText("New Game", 60, 115, 20, BLACK);
DrawRectangleRec(button2.rect, button2.color);
DrawText("Quit", 280, 115, 20, BLACK);
if((is_mouse_over_button(button1)) && (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))){
PlaySound(click);
resetGame(game, move, click);
UnloadSound(move);
CloseAudioDevice();
CloseWindow();
goto newgame;
}else if((is_mouse_over_button(button2)) && (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))){
PlaySound(click);
CloseWindow();
goto quit;
}
EndDrawing();
}
}
EndDrawing();
}
quit:
UnloadSound(click);
UnloadSound(move);
CloseAudioDevice();
return 0;
}
void initGame(Game& game){
game.playerOneName = "Player 1";
game.playerTwoName = "Player 2";
game.p1 = 0;
game.p2 = 0;
game.turn = true;
for(int row = 0; row < 8; row++){
for(int col =0; col <8; col++){
game.cellInfo[row][col].row = row;
game.cellInfo[row][col].col = col;
game.cellInfo[row][col].cellType = initCellType(row, col);
}
}
}
void initBoard(Board& board){
board.boardHeight = BOARD_HEIGHT;
board.boardWidth = BOARD_WIDTH;
board.boardColor = RAYWHITE;
}
void drawBoard(Board board){
DrawRectangle(0, 0, board.boardWidth, board.boardHeight, board.boardColor);
DrawRectangle(board.boardWidth, 0, 300, board.boardHeight, WHITE);
}
int initCellType(int row, int col){
bool isEvenRow = row % 2 == 0;
bool isEvenCol = col % 2 == 0;
if((isEvenRow && !isEvenCol) || (!isEvenRow && isEvenCol)){
if(row < 3){
return player2Qorki;
}
if(row > 4){
return player1Qorki;
}
}
return emptyCell;
}
void drawCellsOnBoard(Cell cell[8][8]){
for(int row = 0; row < 8; row++)
for(int col = 0; col < 8; col++){
Cell currentCell = cell[row][col];
Color cellColor = getCellColor(cell[row][col]);
DrawRectangle(col * currentCell.cellSize, row * currentCell.cellSize, currentCell.cellSize, currentCell.cellSize, cellColor);
}
}
Color getCellColor(Cell cell){
bool isEvenRow = cell.row % 2 == 0;
bool isEvenCol = cell.col % 2 == 0;
Color darkWood = (Color){86,107,48,255}; // Dark brown for dark squares
Color lightWood = (Color){254,255,218,255}; // Light brown for light squares
if((isEvenRow && !isEvenCol) || (!isEvenRow && isEvenCol)){
return darkWood;
}
return lightWood;
}
void drawQorki(Cell cells[8][8]){
Qorki qorki;
float cellCenterPos = cells[0][0].cellSize / 2;
for(int row = 0; row < 8; row++){
int rowPos = row * cells[0][0].cellSize;
for(int col = 0; col < 8; col++){
int colPos = col * cells[0][0].cellSize;
Cell currentCell = cells[row][col];
if(currentCell.cellType != emptyCell){
Color qorkiColor = getQorkiColor(currentCell);
DrawCircle(cellCenterPos + colPos, cellCenterPos + rowPos, qorki.qorkiSize,qorkiColor);
}
}
}
}
Color getQorkiColor(Cell cell) {
// Define complementary wood-like colors for the Qorki pieces
Color player1QorkiColor = (Color){251, 251, 238, 255}; // Light Beige for Player 1's Qorki
Color player1KingColor = (Color){255, 215, 0, 255}; // Gold for Player 1's King Qorki
Color player2QorkiColor = (Color){226, 135, 67, 255}; // Light Brown for Player 2's Qorki
Color player2KingColor = (Color){106, 81, 75, 255}; // Dark Brown for Player 2's King Qorki
switch(cell.cellType) {
case player1Qorki:
return player1QorkiColor;
case player1KingQorki:
return player1KingColor;
case player2Qorki:
return player2QorkiColor;
case player2KingQorki:
return player2KingColor;
// Other cell types, if any
// default:
}
}
void updateGame(Cell cells[8][8], Game& game, Sound& move){
Cell static selectedCell;
Cell static targetCell;
int selectedXPos;
int selectedYPos;
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
selectedXPos = GetMouseX();
selectedYPos = GetMouseY();
selectedCell = getCell(selectedXPos, selectedYPos);
}
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) {
selectedXPos = GetMouseX();
selectedYPos = GetMouseY();
targetCell = getCell(selectedXPos, selectedYPos);
winner(selectedCell,targetCell,cells, game);
if(cells[selectedCell.row][selectedCell.col].cellType == player1Qorki || cells[selectedCell.row][selectedCell.col].cellType == player2Qorki)
handleQorkiMove(selectedCell, targetCell, cells, game, move);
if(cells[selectedCell.row][selectedCell.col].cellType == player1KingQorki || cells[selectedCell.row][selectedCell.col].cellType == player2KingQorki)
handleKingQorkiMove(selectedCell, targetCell, cells, game, move);
}
}
Cell getCell(int x, int y){
Cell c;
c.row = y/c.cellSize;
c.col = x/c.cellSize;
return c;
}
void handleQorkiMove(Cell selectedCell, Cell targetCell, Cell cells[8][8], Game& game, Sound& move) {
if(game.turn){
if(cells[selectedCell.row][selectedCell.col].cellType == player1Qorki){
if(isMoveValid(selectedCell,targetCell,cells, game, move)){
moveQorki(selectedCell,targetCell,cells,move);
game.turn = false;
}
}
}else{
if(cells[selectedCell.row][selectedCell.col].cellType == player2Qorki){
if(isMoveValid(selectedCell,targetCell,cells, game, move)){
moveQorki(selectedCell,targetCell,cells,move);
game.turn = true;
}
}
}
}
void handleKingQorkiMove(Cell selectedCell,Cell targetCell,Cell cells[8][8], Game& game, Sound& move){
if(game.turn){
if(cells[selectedCell.row][selectedCell.col].cellType == player1KingQorki){
if(isKingMoveValid(selectedCell,targetCell,cells, game, move)){
moveQorki(selectedCell,targetCell,cells,move);
game.turn = false;
}
}
}else{
if(cells[selectedCell.row][selectedCell.col].cellType == player2KingQorki){
if(isKingMoveValid(selectedCell,targetCell,cells, game,move)){
moveQorki(selectedCell,targetCell,cells,move);
game.turn = true;
}
}
}
}
void moveQorki(Cell selectedCell, Cell targetCell, Cell cells[8][8], Sound& move){
cells[targetCell.row][targetCell.col].cellType = cells[selectedCell.row][selectedCell.col].cellType;
removeQorki(selectedCell, cells);
if (targetCell.row == 0 && cells[targetCell.row][targetCell.col].cellType == player1Qorki) {
cells[targetCell.row][targetCell.col].cellType = player1KingQorki;
}else if (targetCell.row == 7 && cells[targetCell.row][targetCell.col].cellType == player2Qorki) {
cells[targetCell.row][targetCell.col].cellType = player2KingQorki;
}
PlaySound(move);
}
void removeQorki(Cell cell, Cell cells[8][8]){
cells[cell.row][cell.col].cellType = emptyCell;
}
bool isMoveValid(Cell selectedCell, Cell targetCell, Cell cells[8][8], Game& game, Sound& move){
Cell turntype;
//This is so that only not the selected type and the one that is being capured shouldnt be the same but also there king couter parts
if(game.turn){
turntype.cellType = player1KingQorki;
}else{
turntype.cellType = player2KingQorki;
}
// Check if the selected cell has a Qorki
if (cells[selectedCell.row][selectedCell.col].cellType == emptyCell)
return false;
// Check if the target cell already has a Qorki
if (cells[targetCell.row][targetCell.col].cellType != emptyCell){
return false;
}
// Check if the selected cell and the target cell are the same
if (selectedCell.row == targetCell.row && selectedCell.col == targetCell.col)
return false;
int rowdiff = targetCell.row - selectedCell.row;
int coldiff = targetCell.col - selectedCell.col;
// For player1Qorki, the row difference should be negative (moving upwards)
if (cells[selectedCell.row][selectedCell.col].cellType == player2Qorki && rowdiff <= 0) {
return false;
}
// For player2Qorki, the row difference should be positive (moving downwards)
if (cells[selectedCell.row][selectedCell.col].cellType == player1Qorki && rowdiff >= 0) {
return false;
}
if (abs(selectedCell.row - targetCell.row) == 1 && abs(selectedCell.col - targetCell.col) == 1) {
return true;
} else if(abs(selectedCell.row - targetCell.row) > 2){
if(abs(selectedCell.col - targetCell.col) == 0){
Cell nextcell;
Cell guess;
guess.row = selectedCell.row + 1;
guess.col = selectedCell.col + 1;
guess.cellType = cells[guess.row][guess.col].cellType;
int midrow;
int midcol;
int count = 1;
// Determine the direction of movement
int rowDirection = (targetCell.row > selectedCell.row) ? 1 : -1;
int colDirection = (guess.cellType != emptyCell) ? 1 : -1;
do {
int nextrow = selectedCell.row + 2 * rowDirection;
int nextcol = selectedCell.col + 2 * colDirection;
// Set the next potential cell based on direction and check for pieces
nextcell.row = nextrow;
nextcell.col = nextcol;
// Checks if the next cell is out of the board
if (nextcell.col < 0) {
return false;
}else if (nextcell.col > 7) {
return false;
}
if (nextcell.row < 0) {
return false;
}else if (nextcell.row > 7) {
return false;
}
// Calculate the intermediate cell position (the cell being captured)
midrow = (selectedCell.row + nextcell.row) / 2;
midcol = (selectedCell.col + nextcell.col) / 2;
Cell removeCell;
removeCell.row = midrow;
removeCell.col = midcol;
// Check if the intermediate cell has an opponent's Qorki
if ((cells[midrow][midcol].cellType != cells[selectedCell.row][selectedCell.col].cellType) && (cells[midrow][midcol].cellType !=turntype.cellType) && (cells[midrow][midcol].cellType != emptyCell)) {
// Capture the Qorki
removeQorki(removeCell, cells);
// Update scores based on which player performed the capture
if((cells[selectedCell.row][selectedCell.col].cellType == player1Qorki) ||
cells[selectedCell.row][selectedCell.col].cellType == player1KingQorki) {
game.p1++;
} else if((cells[selectedCell.row][selectedCell.col].cellType == player2Qorki) ||
cells[selectedCell.row][selectedCell.col].cellType == player2KingQorki) {
game.p2++;
}
winner(selectedCell,targetCell,cells, game);
// Move the KingQorki to the next cell
moveQorki(selectedCell, nextcell, cells, move);
} else {
// No valid capture found, move is invalid
return false;
}
// Update selectedCell to the new position for potential second capture
if(count == 1) {
selectedCell = nextcell;
nextcell = targetCell;
}
colDirection = (nextcell.col > selectedCell.col) ? 1 : -1;
count++;
} while(count == 2);
game.turn = !game.turn;
}
else{
if(abs(rowdiff) != abs(coldiff)){
return false;
}
Cell nextcell;
int midrow;
int midcol;
int count = 1;
int repet;
if (abs(rowdiff) == 4){
repet = 2;
}else if(abs(rowdiff) == 6){
repet= 3;
}
// Determine the direction of movement
int rowDirection = (targetCell.row > selectedCell.row) ? 1 : -1;
int colDirection = (targetCell.col > selectedCell.col) ? 1 : -1;
do {
int nextrow = selectedCell.row + 2 * rowDirection;
int nextcol = selectedCell.col + 2 * colDirection;
// Set the next potential cell based on direction and check for pieces
nextcell.row = nextrow;
nextcell.col = nextcol;
// Calculate the intermediate cell position (the cell being captured)
midrow = (selectedCell.row + nextcell.row) / 2;
midcol = (selectedCell.col + nextcell.col) / 2;
Cell removeCell;
removeCell.row = midrow;
removeCell.col = midcol;
// Check if the intermediate cell has an opponent's Qorki
if ((cells[midrow][midcol].cellType != cells[selectedCell.row][selectedCell.col].cellType) && (cells[midrow][midcol].cellType !=turntype.cellType) && (cells[midrow][midcol].cellType != emptyCell)) {
// Capture the Qorki
removeQorki(removeCell, cells);
// Update scores based on which player performed the capture
if((cells[selectedCell.row][selectedCell.col].cellType == player1Qorki) ||
cells[selectedCell.row][selectedCell.col].cellType == player1KingQorki) {
game.p1++;
} else if((cells[selectedCell.row][selectedCell.col].cellType == player2Qorki) ||
cells[selectedCell.row][selectedCell.col].cellType == player2KingQorki) {
game.p2++;
}
winner(selectedCell,targetCell,cells, game);
// Move the KingQorki to the next cell
moveQorki(selectedCell, nextcell, cells, move);
} else {
// No valid capture found, move is invalid
return false;
}
// Update selectedCell to the new position for potential second capture
if(count == 1) {
selectedCell = nextcell;
nextcell = targetCell;
}
count++;
} while(count == repet);
game.turn = !game.turn;
}
return false;
}else if (abs(selectedCell.row - targetCell.row) == 2 && abs(selectedCell.col - targetCell.col) == 2) {
// Check the intermediate cell to see if there is an opponent's Qorki
int midRow = (selectedCell.row + targetCell.row) / 2;
int midCol = (selectedCell.col + targetCell.col) / 2;
Cell removeCell;
removeCell.row = midRow;
removeCell.col = midCol;
// Check if the intermediate cell has an opponent's Qorki
if ((cells[midRow][midCol].cellType != cells[selectedCell.row][selectedCell.col].cellType) && (cells[midRow][midCol].cellType !=turntype.cellType) &&(cells[midRow][midCol].cellType != emptyCell)) {
removeQorki(removeCell, cells);
if((cells[selectedCell.row][selectedCell.col].cellType == player1Qorki) || cells[selectedCell.row][selectedCell.col].cellType == player1KingQorki){
game.p1++;
}
else if((cells[selectedCell.row][selectedCell.col].cellType == player2Qorki) || cells[selectedCell.row][selectedCell.col].cellType == player2KingQorki){
game.p2++;
}
winner(selectedCell,targetCell,cells, game);
return true;
}
else{
return false;
}
}else{
return false;
}
// implement all other conditions
return true;
}
bool isKingMoveValid(Cell selectedCell, Cell targetCell, Cell cells[8][8], Game& game, Sound& move){
Cell turntype;
//This is so that only not the selected type and the one that is being capured shouldnt be the same but also there normal couter parts
if(game.turn){
turntype.cellType = player1Qorki;
}else{
turntype.cellType = player2Qorki;
}
// Check if the selected cell has a Qorki
if (cells[selectedCell.row][selectedCell.col].cellType == emptyCell)
return false;
// Check if the target cell already has a Qorki
if (cells[targetCell.row][targetCell.col].cellType != emptyCell){
return false;
}
// Check if the selected cell and the target cell are the same
if (selectedCell.row == targetCell.row && selectedCell.col == targetCell.col)
return false;
if (abs(selectedCell.row - targetCell.row) == 1 && abs(selectedCell.col - targetCell.col) == 1) {
return true;
} else if (abs(selectedCell.row - targetCell.row) == 2 && abs(selectedCell.col - targetCell.col) == 2) {
// Check the intermediate cell to see if there is an opponent's Qorki
int midRow = (selectedCell.row + targetCell.row) / 2;
int midCol = (selectedCell.col + targetCell.col) / 2;
Cell removeCell;
removeCell.row = midRow;
removeCell.col = midCol;
// Check if the intermediate cell has an opponent's Qorki
if ((cells[midRow][midCol].cellType != cells[selectedCell.row][selectedCell.col].cellType) && (cells[midRow][midCol].cellType !=turntype.cellType) &&(cells[midRow][midCol].cellType != emptyCell)) {
removeQorki(removeCell, cells);
if((cells[selectedCell.row][selectedCell.col].cellType == player1Qorki) || cells[selectedCell.row][selectedCell.col].cellType == player1KingQorki){
game.p1++;
}
else if((cells[selectedCell.row][selectedCell.col].cellType == player2Qorki) || cells[selectedCell.row][selectedCell.col].cellType == player2KingQorki){
game.p2++;
}
winner(selectedCell,targetCell,cells, game);
return true;
}else if ((cells[midRow][midCol].cellType == emptyCell)){
return true;
}else{
return false;
}
}else if (abs(selectedCell.row - targetCell.row) > 2 ) {
int row, col; // The direction of the row and column
int rowdiff = targetCell.row - selectedCell.row;
int coldiff = targetCell.col - selectedCell.col;
if(abs(selectedCell.col - targetCell.col) == 0){
Cell nextcell;
Cell guess;
int midrow;
int midcol;
int count = 1;
// Determine the direction of movement
int rowDirection = (targetCell.row > selectedCell.row) ? 1 : -1;
if (rowDirection == 1){
guess.row = selectedCell.row - 1;
guess.col = selectedCell.col + 1;
guess.cellType = cells[guess.row][guess.col].cellType;
}
else{
guess.row = selectedCell.row + 1;
guess.col = selectedCell.col + 1;
guess.cellType = cells[guess.row][guess.col].cellType;
}
int colDirection = (guess.cellType != emptyCell) ? 1 : -1;
do {
int nextrow = selectedCell.row + 2 * rowDirection;
int nextcol = selectedCell.col + 2 * colDirection;
// Set the next potential cell based on direction and check for pieces
nextcell.row = nextrow;
nextcell.col = nextcol;
// Checks if the next cell is out of the board
if (nextcell.col < 0) {
return false;
}else if (nextcell.col > 7) {
return false;
}
if (nextcell.row < 0) {
return false;
}else if (nextcell.row > 7) {
return false;
}
if(cells[nextrow][nextcol].cellType != emptyCell){
return false;
}
// Calculate the intermediate cell position (the cell being captured)
midrow = (selectedCell.row + nextcell.row) / 2;
midcol = (selectedCell.col + nextcell.col) / 2;
Cell removeCell;
removeCell.row = midrow;
removeCell.col = midcol;
// Check if the intermediate cell has an opponent's Qorki
if ((cells[midrow][midcol].cellType != cells[selectedCell.row][selectedCell.col].cellType) && (cells[midrow][midcol].cellType !=turntype.cellType) && (cells[midrow][midcol].cellType != emptyCell)) {
// Capture the Qorki
removeQorki(removeCell, cells);
// Update scores based on which player performs the capture
if((cells[selectedCell.row][selectedCell.col].cellType == player1Qorki) ||
cells[selectedCell.row][selectedCell.col].cellType == player1KingQorki) {
game.p1++;
} else if((cells[selectedCell.row][selectedCell.col].cellType == player2Qorki) ||
cells[selectedCell.row][selectedCell.col].cellType == player2KingQorki) {
game.p2++;
}
winner(selectedCell,targetCell,cells, game);
// Move the KingQorki to the next cell
moveQorki(selectedCell, nextcell, cells, move);
} else {
// No valid capture found, move is invalid
return false;
}
// Update selectedCell to the new position for potential second capture
if(count == 1) {
selectedCell = nextcell;
nextcell = targetCell;
}
colDirection = (nextcell.col > selectedCell.col) ? 1 : -1;
count++;
} while(count == 2);
game.turn = !game.turn;
}else if(abs(rowdiff) != abs(coldiff)){
return false;
}else if(abs(selectedCell.col - targetCell.col) > 2){
Cell nextcell;
Cell removeCell;
int rowDirection = (targetCell.row > selectedCell.row) ? 1 : -1;
int colDirection = (targetCell.col > selectedCell.col) ? 1 : -1;
row = selectedCell.row + rowDirection;
col = selectedCell.col + colDirection;
while (row != targetCell.row && col != targetCell.col) {
if (cells[row][col].cellType != emptyCell) {
removeCell.row = row;
removeCell.col = col;
if ((cells[row][col].cellType != cells[selectedCell.row][selectedCell.col].cellType) && (cells[row][col].cellType !=turntype.cellType)) {
if(cells[row+rowDirection][col+colDirection].cellType != emptyCell){
return false;
}
removeQorki(removeCell, cells);
// Update scores based on which player performed the capture
if(cells[selectedCell.row][selectedCell.col].cellType == player1KingQorki) {
game.p1 = game.p1 + 1;
} else if(cells[selectedCell.row][selectedCell.col].cellType == player2KingQorki) {
game.p2 = game.p2 + 1;
}
winner(selectedCell,targetCell,cells, game);
}
else if ((cells[row][col].cellType == cells[selectedCell.row][selectedCell.col].cellType) || (cells[row][col].cellType ==turntype.cellType)){
return false;
}
}
row += rowDirection;
col += colDirection;
}
return true;
}
return false;
}
return false;
}
void winner(Cell selectedCell, Cell targetCell, Cell cells[8][8], Game& game){
bool playeronemove = false;
bool playertwomove = false;
if (game.p1 == 11 && game.p2 < 11) {
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
if (cells[row][col].cellType == player2Qorki) {
selectedCell.row = row;
selectedCell.col = col;
selectedCell.cellType = player2Qorki;
}
}
}
targetCell.row = selectedCell.row + 1;
targetCell.col = selectedCell.col + 1;
targetCell.cellType = cells[targetCell.row][targetCell.col].cellType;
if(stalmate(selectedCell,targetCell,cells, game)){
playeronemove = true;
}
targetCell.row = selectedCell.row + 2;
targetCell.col = selectedCell.col + 2;
targetCell.cellType = cells[targetCell.row][targetCell.col].cellType;
if(stalmate(selectedCell,targetCell,cells, game)){
playeronemove = true;
}
targetCell.row = selectedCell.row + 1;
targetCell.col = selectedCell.col - 1;
targetCell.cellType = cells[targetCell.row][targetCell.col].cellType;
if(stalmate(selectedCell,targetCell,cells, game)){
playeronemove = true;
}
targetCell.row = selectedCell.row + 2;
targetCell.col = selectedCell.col - 2;
targetCell.cellType = cells[targetCell.row][targetCell.col].cellType;
if(stalmate(selectedCell,targetCell,cells, game)){
playeronemove = true;
}
if (!playeronemove) {
game.winner = 1; // Player 1 wins
}
} else if (game.p2 == 11 && game.p1 < 11) {
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
if (game.cellInfo[row][col].cellType == player1Qorki) {
selectedCell.row = row;
selectedCell.col = col;
selectedCell.cellType = player1Qorki;
}
}
}
targetCell.row = selectedCell.row - 1;
targetCell.col = selectedCell.col + 1;
targetCell.cellType = cells[targetCell.row][targetCell.col].cellType;
if(stalmate(selectedCell,targetCell,cells, game)){