-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
3076 lines (3018 loc) · 91.6 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
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 <cstdlib>
#include "Button.h"
const char WHITE_ROOK = 'R';
const char WHITE_PAWN = 'P';
const char WHITE_HORSE = 'H';
const char WHITE_BISHOP = 'B';
const char WHITE_QUEEN = 'Q';
const char WHITE_KING = 'K';
const char BLACK_ROOK = 'r';
const char BLACK_PAWN = 'p';
const char BLACK_HORSE = 'h';
const char BLACK_BISHOP = 'b';
const char BLACK_QUEEN = 'q';
const char BLACK_KING = 'k';
bool WHITE_CASTLE_LEFT = true;
bool WHITE_CASTLE_RIGHT = true;
bool WHITE_CASTLE_KING = true;
bool BLACK_CASTLE_LEFT = true;
bool BLACK_CASTLE_RIGHT = true;
bool BLACK_CASTLE_KING = true;
char BLACK_EN_PASSANT;
char WHITE_EN_PASSANT;
const char EMPTY_SQUARE = -2;
const char VERTICAL_LINE = -70;
const char HORIZONTAL_LINE = -51;
const char WHITE_PLAYER = 1;
const char BLACK_PLAYER = 2;
const int SCREEN_H = 640;
const int SCREEN_W = 640;
int StartUP_menu()
{
int input;
start:
system("CLS");
std::cout << " SIMPLE CHESS " << std::endl;
std::cout << std::endl;
std::cout << " 1) Start P vs P Game (2 players)" << std::endl;
std::cout << " 2) Start P vs IA Game (1 players/Work in progress currently unavailable)" << std::endl;
std::cout << " 3) Basic Rules" << std::endl;
std::cout << " 4) Exit Game" << std::endl;
std::cout << std::endl;
std::cout << " Select Option(1-4): ";
std::cin >> input;
while (!std::cin.good() || input < 1 || input>4)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
system("CLS");
std::cout << " SIMPLE CHESS " << std::endl;
std::cout << std::endl;
std::cout << " 1) Start P vs P Game (2 players)" << std::endl;
std::cout << " 2) Start P vs IA Game (1 players/Work in progress currently unavailable)" << std::endl;
std::cout << " 3) Basic Rules" << std::endl;
std::cout << " 4) Exit Game" << std::endl;
std::cout << std::endl;
std::cout << " Select Option(1-4): ";
std::cin >> input;
}
switch (input)
{
case 1:
break;
case 2:
std::cout << "NOT READY YET .... , select any other option" << std::endl;
system("pause");
goto start;
case 3:
system("CLS");
std::cout << "White Player (UPERCASE)--> P,Q,K,R,B,H" << std::endl;
std::cout << "BLACK Player (LOWERCASE)--> p,q,k,r,b,h" << std::endl;
std::cout << std::endl;
std::cout << "Pieces List/ Movement:" << std::endl;
std::cout << std::endl;
std::cout << " - PAWN (P/p) --> Moves one square at a time, it can only move forward, it can only capture enemy pieces diagonally (one square to the left/Right and one up),if it has not moved yet they can start by moving two squares forward, it can do an en passant capture this happens when the opponent pawn does a double move start then if you have a pawn that could have killed the enemy pawn if it had moved only one square then you can still caputre it, if it reaches the final square on the board you can promote it to whatever piece you want." << std::endl;
std::cout << std::endl;
std::cout << " - Rook (R/r) --> Moves vertically and horizontally the amount of squares you want without passing through any pieces." << std::endl;
std::cout << std::endl;
std::cout << " - Horse (H/h) --> Moves in an L shape, two squares horizontally and one vertically or two squares vertically and one square horizontally." << std::endl;
std::cout << std::endl;
std::cout << " - Bishop (B/b) --> Moves Diagonally the amount of squares you want without passing through any pieces." << std::endl;
std::cout << std::endl;
std::cout << " - Queen (Q/q) --> Moves like a rook and a bishop, it can move diagonally/vertically/horizontally the amount of squares you want without passing through any pieces" << std::endl;
std::cout << std::endl;
std::cout << " - King (K/k) --> Can only move one square in any direction, you can castle the king queen side or king side, this means that if there are no pieces between your king and either of your rooks you can move yout king two squares in the direction you are castling and put the rook on that side next to the king(if you castled king side then your king will go from e1 to g1 and the rook will gor from h1 to f1), this can only be done if neither the king and rook that you are using to castle have moved that game and if your king isn't in check." << std::endl;
std::cout << std::endl;
std::cout << "Basic Knowledge:" << std::endl;
std::cout << std::endl;
std::cout << "Check --> Your King is being attacked by an enemy piece you must move your king to a safe spot or kill the atacker or interpose the attack with another piece." << std::endl;
std::cout << "Checkmate --> Your King is being attacked and you can't do anything to protect him, you lost the game." << std::endl;
std::cout << "Stalemate --> The player that has to make a move can't move any of his pieces, it is a tie." << std::endl;
system("pause");
goto start;
case 4:
return 4;
}
return 0;
}
void Create_Grid(char Grid[][8])
{
//Create Empty Board
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Grid[i][j] = EMPTY_SQUARE;
}
}
//Setup Pieces
for (int i = 0; i < 8; i++)
{
Grid[1][i] = BLACK_PAWN;
Grid[6][i] = WHITE_PAWN;
}
Grid[0][0] = BLACK_ROOK;
Grid[0][1] = BLACK_HORSE;
Grid[0][2] = BLACK_BISHOP;
Grid[0][3] = BLACK_QUEEN;
Grid[0][4] = BLACK_KING;
Grid[0][5] = BLACK_BISHOP;
Grid[0][6] = BLACK_HORSE;
Grid[0][7] = BLACK_ROOK;
Grid[7][0] = WHITE_ROOK;
Grid[7][1] = WHITE_HORSE;
Grid[7][2] = WHITE_BISHOP;
Grid[7][3] = WHITE_QUEEN;
Grid[7][4] = WHITE_KING;
Grid[7][5] = WHITE_BISHOP;
Grid[7][6] = WHITE_HORSE;
Grid[7][7] = WHITE_ROOK;
}
void Display_Grid(char Grid[][8])
{
system("CLS");
std::cout << " ";
for (int j = 0; j < 8; j++)
std::cout << (char)(j + 97) << " ";
std::cout << std::endl;
std::cout << " ";
for (int i = 0; i < 16; i++)
std::cout << HORIZONTAL_LINE;
std::cout << std::endl;
for (int i = 0; i < 8; i++)
{
std::cout << (8 - i) << " " << VERTICAL_LINE << " ";
for (int j = 0; j < 8; j++)
{
std::cout << Grid[i][j] << " ";
}
std::cout << std::endl;
}
}
bool Check_Start(char Grid[][8], char x, char y, char player)
{
char pos = Grid[x][y];
//Check piece existence
if (pos == EMPTY_SQUARE)
{
return false;
}
//Check if it is your piece
if (player == WHITE_PLAYER)
{
if (pos != WHITE_BISHOP && pos != WHITE_HORSE && pos != WHITE_ROOK && pos != WHITE_KING && pos != WHITE_QUEEN && pos != WHITE_PAWN)
return false;
}
else
{
if (pos != BLACK_BISHOP && pos != BLACK_HORSE && pos != BLACK_ROOK && pos != BLACK_KING && pos != BLACK_QUEEN && pos != BLACK_PAWN)
return false;
}
//Returns false if the starting position is wrong
return true;
}
bool Player_In_Check(char Grid[][8], char player)
{
int s, t, x, y;
if (player == BLACK_PLAYER)
{
for (int p = 0; p < 8; p++)
{
for (int m = 0; m < 8; m++)
{
if (Grid[p][m] == BLACK_BISHOP || Grid[p][m] == BLACK_QUEEN)
{
//Look on all directions
//Search TOP Left x-- y--
s = m - 1;
t = p - 1;
while (t >= 0 && s >= 0)
{
if (Grid[t][s] != EMPTY_SQUARE && Grid[t][s] != WHITE_KING)
break;
else if (Grid[t][s] == WHITE_KING)
return true;
s--;
t--;
}
//Search BOTTOM Right x++ y++
s = m + 1;
t = p + 1;
while (t < 8 && s < 8)
{
if (Grid[t][s] != EMPTY_SQUARE && Grid[t][s] != WHITE_KING)
break;
else if (Grid[t][s] == WHITE_KING)
return true;
s++;
t++;
}
//Search BOTTOM Left x++ y--
s = m - 1;
t = p + 1;
while (s >= 0 && t < 8)
{
if (Grid[t][s] != EMPTY_SQUARE && Grid[t][s] != WHITE_KING)
break;
else if (Grid[t][s] == WHITE_KING)
return true;
s--;
t++;
}
//Search top Right x-- y++
s = m + 1;
t = p - 1;
while (s < 8 && t >= 0)
{
if (Grid[t][s] != EMPTY_SQUARE && Grid[t][s] != WHITE_KING)
break;
else if (Grid[t][s] == WHITE_KING)
return true;
s++;
t--;
}
}
if (Grid[p][m] == BLACK_ROOK || Grid[p][m] == BLACK_QUEEN)
{
//Look Right
for (int t = m + 1; t < 8; t++)
{
if (Grid[p][t] != EMPTY_SQUARE && Grid[p][t] != WHITE_KING)
break;
else if (Grid[p][t] == WHITE_KING)
return true;
}
//Look Left
for (int t = m - 1; t >= 0; t--)
{
if (Grid[p][t] != EMPTY_SQUARE && Grid[p][t] != WHITE_KING)
break;
else if (Grid[p][t] == WHITE_KING)
return true;
}
//Look Up
for (int t = p + 1; t < 8; t++)
{
if (Grid[t][m] != EMPTY_SQUARE && Grid[t][m] != WHITE_KING)
break;
else if (Grid[t][m] == WHITE_KING)
return true;
}
//Look Down
for (int t = p - 1; t >= 0; t--)
{
if (Grid[t][m] != EMPTY_SQUARE && Grid[t][m] != WHITE_KING)
break;
else if (Grid[t][m] == WHITE_KING)
return true;
}
}
if (Grid[p][m] == BLACK_HORSE)
{
if (p + 2 < 8 && m + 1 < 8 && Grid[p + 2][m + 1] == WHITE_KING)
return true;
if (p + 2 < 8 && m - 1 >= 0 && Grid[p + 2][m - 1] == WHITE_KING)
return true;
if (p - 2 >= 0 && m + 1 < 8 && Grid[p - 2][m + 1] == WHITE_KING)
return true;
if (p - 2 >= 0 && m - 1 >= 0 && Grid[p - 2][m - 1] == WHITE_KING)
return true;
if (p + 1 < 8 && m + 2 < 8 && Grid[p + 1][m + 2] == WHITE_KING)
return true;
if (p + 1 < 8 && m - 2 >= 0 && Grid[p + 1][m - 2] == WHITE_KING)
return true;
if (p - 1 >= 0 && m + 2 < 8 && Grid[p - 1][m + 2] == WHITE_KING)
return true;
if (p - 1 >= 0 && m - 2 >= 0 && Grid[p - 1][m - 2] == WHITE_KING)
return true;
}
else if (Grid[p][m] == BLACK_PAWN)
{
if (m + 1 < 8 && p + 1 < 8 && Grid[p + 1][m + 1] == WHITE_KING)
return true;
if (m - 1 > 0 && p + 1 < 8 && Grid[p + 1][m - 1] == WHITE_KING)
return true;
}
}
}
}
else if (player == WHITE_PLAYER)
{
for (int p = 0; p < 8; p++)
{
for (int m = 0; m < 8; m++)
{
if (Grid[p][m] == WHITE_BISHOP || Grid[p][m] == WHITE_QUEEN)
{
//Look on all directions
//Search TOP Left x-- y--
s = m - 1;
t = p - 1;
while (t >= 0 && s >= 0)
{
if (Grid[t][s] != EMPTY_SQUARE && Grid[t][s] != BLACK_KING)
break;
else if (Grid[t][s] == BLACK_KING)
return true;
s--;
t--;
}
//Search BOTTOM Right x++ y++
s = m + 1;
t = p + 1;
while (t < 8 && s < 8)
{
if (Grid[t][s] != EMPTY_SQUARE && Grid[t][s] != BLACK_KING)
break;
else if (Grid[t][s] == BLACK_KING)
return true;
s++;
t++;
}
//Search BOTTOM Left x++ y--
s = m - 1;
t = p + 1;
while (s >= 0 && t < 8)
{
if (Grid[t][s] != EMPTY_SQUARE && Grid[t][s] != BLACK_KING)
break;
else if (Grid[t][s] == BLACK_KING)
return true;
s--;
t++;
}
//Search top Right x-- y++
s = m + 1;
t = p - 1;
while (s < 8 && t >= 0)
{
if (Grid[t][s] != EMPTY_SQUARE && Grid[t][s] != BLACK_KING)
break;
else if (Grid[t][s] == BLACK_KING)
return true;
s++;
t--;
}
}
if (Grid[p][m] == WHITE_ROOK || Grid[p][m] == WHITE_QUEEN)
{
//Look Right
for (int t = m + 1; t < 8; t++)
{
if (Grid[p][t] != EMPTY_SQUARE && Grid[p][t] != BLACK_KING)
break;
else if (Grid[p][t] == BLACK_KING)
return true;
}
//Look Left
for (int t = m - 1; t >= 0; t--)
{
if (Grid[p][t] != EMPTY_SQUARE && Grid[p][t] != BLACK_KING)
break;
else if (Grid[p][t] == BLACK_KING)
return true;
}
//Look Up
for (int t = p + 1; t < 8; t++)
{
if (Grid[t][m] != EMPTY_SQUARE && Grid[t][m] != BLACK_KING)
break;
else if (Grid[t][m] == BLACK_KING)
return true;
}
//Look Down
for (int t = p - 1; t >= 0; t--)
{
if (Grid[t][m] != EMPTY_SQUARE && Grid[t][m] != BLACK_KING)
break;
else if (Grid[t][m] == BLACK_KING)
return true;
}
}
if (Grid[p][m] == WHITE_HORSE)
{
if (p + 2 < 8 && m + 1 < 8 && Grid[p + 2][m + 1] == BLACK_KING)
return true;
if (p + 2 < 8 && m - 1 >= 0 && Grid[p + 2][m - 1] == BLACK_KING)
return true;
if (p - 2 >= 0 && m + 1 < 8 && Grid[p - 2][m + 1] == BLACK_KING)
return true;
if (p - 2 >= 0 && m - 1 >= 0 && Grid[p - 2][m - 1] == BLACK_KING)
return true;
if (p + 1 < 8 && m + 2 < 8 && Grid[p + 1][m + 2] == BLACK_KING)
return true;
if (p + 1 < 8 && m - 2 >= 0 && Grid[p + 1][m - 2] == BLACK_KING)
return true;
if (p - 1 >= 0 && m + 2 < 8 && Grid[p - 1][m + 2] == BLACK_KING)
return true;
if (p - 1 >= 0 && m - 2 >= 0 && Grid[p - 1][m - 2] == BLACK_KING)
return true;
}
else if (Grid[p][m] == WHITE_PAWN)
{
if (m + 1 < 8 && p - 1 > 0 && Grid[p - 1][m + 1] == BLACK_KING)
return true;
if (m - 1 > 0 && p - 1 > 0 && Grid[p - 1][m - 1] == BLACK_KING)
return true;
}
}
}
}
//Check that both kings are not in contact
//First locate the kings
for (s = 0; s < 8; s++)
for (t = 0; t < 8; t++)
if (Grid[s][t] == BLACK_KING)
goto fd;
fd:
for (x = 0; x < 8; x++)
for (y = 0; y < 8; y++)
if (Grid[x][y] == WHITE_KING)
goto fd2;
fd2:
//Then we check if they are close
if (abs(x - s) <= 1 && abs(y - t) <= 1)
return true;
return false;
}
bool Check_ADVANCED(char Grid[][8], char x, char y, char j, char z, char player, Button* pieces[])
{
char piece = Grid[x][y];
char dest = Grid[j][z];
bool Castle_Left = false;
bool Castle_Right = false;
bool King_Move = false;
bool check = false;
char en_passant = 'z';
char aux, aux_piece;
int m;
//Basic movement and Captures
switch (piece)
{
case WHITE_PAWN:
case BLACK_PAWN:
//Check backwards movement
if (piece == WHITE_PAWN && j > x)
return false;
else if (piece == BLACK_PAWN && x > j)
return false;
//Check posible double move if first time moving pawn
if (abs(x - j) == 2 && abs(y - z) == 0)
{
if (player == WHITE_PLAYER && x == 6)
{
en_passant = y;
goto CHECK_CHECK;
}
else if (player == BLACK_PLAYER && x == 1)
{
en_passant = y;
goto CHECK_CHECK;
}
}
//Check en passant
if (dest == EMPTY_SQUARE && abs(y - z) == 1 && abs(x - j) == 1 && ((player == WHITE_PLAYER && Grid[j + 1][z] == BLACK_PAWN && BLACK_EN_PASSANT == z) || (player == BLACK_PLAYER && Grid[j - 1][z] == WHITE_PAWN && WHITE_EN_PASSANT == z)))
{
en_passant = 'k';
goto CHECK_CHECK;
}
//Check that the move is not further than one square and if it goes diagonally there is not an empty square and can t kill enemies just walking forward
if (abs(y - z) > 1 || abs(x - j) > 1 || ((y != z) && (dest == EMPTY_SQUARE)) || ((y == z) && (dest != EMPTY_SQUARE)))
return false;
//Can t capture going horizontally
if (y - z != 0 && x - j == 0)
return false;
//check promote
if (j == 0 || j == 7)
{
std::cout << "What do you want to promote to?" << std::endl;
std::cout << "Queen (Q/q) Bishop (B/b) Horse (H/h) Rook (R/r)" << std::endl;
std::cin >> piece;
bool white_promote = (player == WHITE_PLAYER && (piece == WHITE_HORSE || piece == WHITE_BISHOP || piece == WHITE_QUEEN || piece == WHITE_ROOK));
bool black_promote = (player == BLACK_PLAYER && (piece == BLACK_HORSE || piece == BLACK_BISHOP || piece == BLACK_QUEEN || piece == BLACK_ROOK));
bool result = !(white_promote ^ black_promote);
while (!std::cin.good() || result)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
system("CLS");
Display_Grid(Grid);
std::cout << "What do you want to promote to?" << std::endl;
std::cout << "Queen (Q/q) Bishop (B/b) Horse (H/h) Rook (R/r)" << std::endl;
std::cout << "Insert Correct data" << std::endl;
std::cin >> piece;
white_promote = (player == WHITE_PLAYER && (piece == WHITE_HORSE || piece == WHITE_BISHOP || piece == WHITE_QUEEN || piece == WHITE_ROOK));
black_promote = (player == BLACK_PLAYER && (piece == BLACK_HORSE || piece == BLACK_BISHOP || piece == BLACK_QUEEN || piece == BLACK_ROOK));
result = !(white_promote ^ black_promote);
}
Grid[x][y] = piece;
}
break;
case WHITE_HORSE:
case BLACK_HORSE:
//Basic movement
if (abs(x - j) == 2 && abs(y - z) == 1)
goto CHECK_CHECK;
if (abs(y - z) == 2 && abs(x - j) == 1)
goto CHECK_CHECK;
return false;
case WHITE_QUEEN:
case BLACK_QUEEN:
case WHITE_ROOK:
case BLACK_ROOK:
//Check Vertical movement
if (x == j && y != z)
{
y < z ? aux = y : aux = z;
for (int t = 1; t < abs(y - z); t++)
if (Grid[x][t + aux] != EMPTY_SQUARE)
return false;
if (piece == BLACK_ROOK || piece == WHITE_ROOK)
{
if (y == 7) Castle_Right = true;
if (y == 0) Castle_Left = true;
}
goto CHECK_CHECK;
}
//Check Horizontal movement
if (z == y && x != j)
{
x < j ? aux = x : aux = j;
for (int t = 1; t < abs(x - j); t++)
if (Grid[t + aux][y] != EMPTY_SQUARE)
return false;
if (piece == BLACK_ROOK || piece == WHITE_ROOK)
{
if (y == 7) Castle_Right = true;
if (y == 0) Castle_Left = true;
}
goto CHECK_CHECK;
}
if (piece == BLACK_ROOK || piece == WHITE_ROOK)
return false;
case WHITE_BISHOP:
case BLACK_BISHOP:
//Check diagonal movement
if (abs(x - j) != abs(y - z) || abs(x - j) == 0)
return false;
//Search TOP Left x-- y--
if (x > j && y > z)
{
m = y - 1;
for (int t = x - 1; t > j; t--)
{
if (Grid[t][m] != EMPTY_SQUARE)
return false;
m--;
}
}
//Search BOTTOM Right x++ y++
else if (x < j && y < z)
{
m = y + 1;
for (int t = x + 1; t < j; t++)
{
if (Grid[t][m] != EMPTY_SQUARE)
return false;
m++;
}
}
//Search BOTTOM Left x++ y--
else if (x < j && y>z)
{
m = y - 1;
for (int t = x + 1; t < j; t++)
{
if (Grid[t][m] != EMPTY_SQUARE)
return false;
m--;
}
}
//Search top Right x-- y++
else
{
m = y + 1;
for (int t = x - 1; t > j; t--)
{
if (Grid[t][m] != EMPTY_SQUARE)
return false;
m++;
}
}
goto CHECK_CHECK;
break;
case WHITE_KING:
case BLACK_KING:
//Basic movement
if (abs(j - x) > 1 || abs(y - z) > 2 || (abs(y - z) > 1 && y != 4))
return false;
//Castle
if (abs(y - z) == 2 && abs(j - x) == 0 && (x == 0 || x == 7) && y == 4)
{
if (player == WHITE_PLAYER && WHITE_CASTLE_KING == true && (WHITE_CASTLE_LEFT == true || WHITE_CASTLE_RIGHT == true))
{
if (z == 6)//Castle right
{
if (Grid[x][5] != EMPTY_SQUARE || Grid[x][6] != EMPTY_SQUARE || WHITE_CASTLE_RIGHT == false)
return false;
Grid[x][5] = WHITE_KING;
Grid[x][7] = EMPTY_SQUARE;
check = Player_In_Check(Grid, player % 2 + 1);
if (check == true)
return false;
Grid[x][6] = WHITE_KING;
check = Player_In_Check(Grid, player % 2 + 1);
if (check == true)
return false;
for (int i = 0; i < 32; i++)
{
if (pieces[i]->Getpiece() == WHITE_ROOK)
{
int xx, yy;
pieces[i]->Getposition(&xx, &yy);
if (xx == (7) * 80 && yy == x * 80)
{
pieces[i]->Setposition(5 * 80, x * 80);
break;
}
}
}
Grid[x][5] = WHITE_ROOK;
King_Move = true;
goto CHECK_CHECK;
}
if (z == 2)
{
if (Grid[x][3] != EMPTY_SQUARE || Grid[x][2] != EMPTY_SQUARE || Grid[x][1] != EMPTY_SQUARE || WHITE_CASTLE_LEFT == false)
return false;
Grid[x][3] = WHITE_KING;
Grid[x][0] = EMPTY_SQUARE;
check = Player_In_Check(Grid, player % 2 + 1);
if (check == true)
return false;
Grid[x][2] = WHITE_KING;
check = Player_In_Check(Grid, player % 2 + 1);
if (check == true)
return false;
for (int i = 0; i < 32; i++)
{
if (pieces[i]->Getpiece() == WHITE_ROOK)
{
int xx, yy;
pieces[i]->Getposition(&xx, &yy);
if (xx == 0 && yy == x * 80)
{
pieces[i]->Setposition(3 * 80, x * 80);
break;
}
}
}
Grid[x][3] = WHITE_ROOK;
King_Move = true;
goto CHECK_CHECK;
}
}
if (player == BLACK_PLAYER && BLACK_CASTLE_KING == true && (BLACK_CASTLE_LEFT == true || BLACK_CASTLE_RIGHT == true))
{
if (z == 6)//Castle right
{
if (Grid[x][5] != EMPTY_SQUARE || Grid[x][6] != EMPTY_SQUARE || BLACK_CASTLE_RIGHT == false)
return false;
Grid[x][5] = BLACK_KING;
Grid[x][7] = EMPTY_SQUARE;
check = Player_In_Check(Grid, player % 2 + 1);
if (check == true)
return false;
Grid[x][6] = BLACK_KING;
check = Player_In_Check(Grid, player % 2 + 1);
if (check == true)
return false;
for (int i = 0; i < 32; i++)
{
if (pieces[i]->Getpiece() == BLACK_ROOK)
{
int xx, yy;
pieces[i]->Getposition(&xx, &yy);
if (xx == (7) * 80 && yy == x * 80)
{
pieces[i]->Setposition(5 * 80,x*80);
break;
}
}
}
Grid[x][5] = BLACK_ROOK;
King_Move = true;
goto CHECK_CHECK;
}
if (z == 2)
{
if (Grid[x][3] != EMPTY_SQUARE || Grid[x][2] != EMPTY_SQUARE || Grid[x][1] != EMPTY_SQUARE || BLACK_CASTLE_LEFT == false)
return false;
Grid[x][3] = BLACK_KING;
Grid[x][0] = EMPTY_SQUARE;
check = Player_In_Check(Grid, player % 2 + 1);
if (check == true)
return false;
Grid[x][2] = BLACK_KING;
check = Player_In_Check(Grid, player % 2 + 1);
if (check == true)
return false;
for (int i = 0; i < 32; i++)
{
if (pieces[i]->Getpiece() == BLACK_ROOK)
{
int xx, yy;
pieces[i]->Getposition(&xx, &yy);
if (xx == 0 && yy == x * 80)
{
pieces[i]->Setposition(3*80, x * 80);
break;
}
}
}
Grid[x][3] = BLACK_ROOK;
King_Move = true;
goto CHECK_CHECK;
}
}
return false;
}
King_Move = true;
break;
}
CHECK_CHECK:
//We simulate the move on the actual Grid but if the move is not posible we goto restore and we change the grid again.
Grid[x][y] = EMPTY_SQUARE;
aux_piece = Grid[j][z];
Grid[j][z] = piece;
if (player == WHITE_PLAYER)
{
//An en passant capture is being tested
if (en_passant == 'k') Grid[j + 1][z] = EMPTY_SQUARE;
check = Player_In_Check(Grid, player % 2 + 1);
if (check == true)
{
if (en_passant == 'k') Grid[j + 1][z] = BLACK_PAWN;
goto restore;
}
//If en passant is possible kill pawn visually
if (en_passant == 'k')
{
for (int i = 0; i < 32; i++)
{
if (pieces[i]->Getpiece() == BLACK_PAWN)
{
int xx, yy;
pieces[i]->Getposition(&xx, &yy);
if (yy == (j + 1)*80 && xx == z*80)
{
pieces[i]->Setdead();
break;
}
}
}
}
//update castle variables
if (WHITE_CASTLE_KING == true) WHITE_CASTLE_KING = !King_Move;
if (WHITE_CASTLE_LEFT == true) WHITE_CASTLE_LEFT = !Castle_Left;
if (WHITE_CASTLE_RIGHT == true) WHITE_CASTLE_RIGHT = !Castle_Right;
//update en passant column
if (en_passant != 'z' && en_passant != 'k') WHITE_EN_PASSANT = en_passant;
BLACK_EN_PASSANT = 'z';
}
if (player == BLACK_PLAYER)
{
//An en passant capture is being tested
if (en_passant == 'k') Grid[j - 1][z] = EMPTY_SQUARE;
check = Player_In_Check(Grid, player % 2 + 1);
if (check == true)
{
if (en_passant == 'k') Grid[j - 1][z] = WHITE_PAWN;
goto restore;
}
//If en passant is possible kill pawn visually
if (en_passant == 'k')
{
for (int i = 0; i < 32; i++)
{
if (pieces[i]->Getpiece() == WHITE_PAWN)
{
int xx, yy;
pieces[i]->Getposition(&xx, &yy);
if (yy == (j + 1) * 80 && xx == z * 80)
{
pieces[i]->Setdead();
break;
}
}
}
}
//update castle variables
if (BLACK_CASTLE_KING == true) BLACK_CASTLE_KING = !King_Move;
if (BLACK_CASTLE_LEFT == true) BLACK_CASTLE_LEFT = !Castle_Left;
if (BLACK_CASTLE_RIGHT == true) BLACK_CASTLE_RIGHT = !Castle_Right;
//update en passant column
if (en_passant != 'z' && en_passant != 'k') BLACK_EN_PASSANT = en_passant;
WHITE_EN_PASSANT = 'z';
}
//returns false if the move is not possible
return true;
restore:
Grid[x][y] = Grid[j][z];
Grid[j][z] = aux_piece;
return false;
}
bool Check_END(char Grid[][8], char x, char y, char j, char z, char player,Button* pieces[])
{
char pos = Grid[j][z];
//Check if destination is empty
if (pos == EMPTY_SQUARE)
{
return Check_ADVANCED(Grid, x, y, j, z, player,pieces);
}
//Check if destination is one of your pieces
if (player == WHITE_PLAYER)
{
if (pos == WHITE_BISHOP || pos == WHITE_HORSE || pos == WHITE_ROOK || pos == WHITE_KING || pos == WHITE_QUEEN || pos == WHITE_PAWN)
return false;
}
else
{
if (pos == BLACK_BISHOP || pos == BLACK_HORSE || pos == BLACK_ROOK || pos == BLACK_KING || pos == BLACK_QUEEN || pos == BLACK_PAWN)
return false;
}
return Check_ADVANCED(Grid, x, y, j, z, player,pieces);
}
bool Check_Tie(char Grid[][8], char player)
{
char piece, aux, obj1, obj2;
//Check all the pieces of the player until one can move
if (Player_In_Check(Grid, player) == true)
{
return false;
}
if (player == BLACK_PLAYER)
{
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
piece = Grid[x][y];
if (piece == WHITE_PAWN)
{
//check normal movement then finally check if en passant is posible
if (Grid[x - 1][y] == EMPTY_SQUARE)
{
Grid[x][y] = EMPTY_SQUARE;
Grid[x - 1][y] = piece;
if (Player_In_Check(Grid, player) == false)
{
Grid[x - 1][y] = EMPTY_SQUARE;
Grid[x][y] = piece;
return false;
}
Grid[x - 1][y] = EMPTY_SQUARE;
Grid[x][y] = piece;
}
//check capture right + en passant right
if (y + 1 < 8)
{
obj1 = Grid[x - 1][y + 1];
if (obj1 == BLACK_PAWN || obj1 == BLACK_ROOK || obj1 == BLACK_HORSE || obj1 == BLACK_BISHOP || obj1 == BLACK_QUEEN)
{
Grid[x - 1][y + 1] = piece;
Grid[x][y] = EMPTY_SQUARE;
if (Player_In_Check(Grid, player) == false)
{
Grid[x][y] = piece;
Grid[x - 1][y + 1] = obj1;
return false;
}
Grid[x][y] = piece;
Grid[x - 1][y + 1] = obj1;
}
//check en passant
if (obj1 == EMPTY_SQUARE && x == 3 && (Grid[x][y + 1] == BLACK_PAWN && BLACK_EN_PASSANT == y + 1))
{
aux = Grid[x][y + 1];
Grid[x - 1][y + 1] = piece;
Grid[x][y] = EMPTY_SQUARE;
Grid[x][y + 1] = EMPTY_SQUARE;
if (Player_In_Check(Grid, player) == false)
{
Grid[x - 1][y + 1] = EMPTY_SQUARE;
Grid[x][y] = piece;
Grid[x][y + 1] = aux;
return false;
}
Grid[x - 1][y + 1] = EMPTY_SQUARE;
Grid[x][y] = piece;
Grid[x][y + 1] = aux;
}
}
//check capture left + en passant left
if (y - 1 >= 0)
{
obj2 = Grid[x - 1][y - 1];
if (obj2 == BLACK_PAWN || obj2 == BLACK_ROOK || obj2 == BLACK_HORSE || obj2 == BLACK_BISHOP || obj2 == BLACK_QUEEN)
{
Grid[x - 1][y - 1] = piece;
Grid[x][y] = EMPTY_SQUARE;
if (Player_In_Check(Grid, player) == false)
{
Grid[x][y] = piece;
Grid[x - 1][y - 1] = obj2;
return false;
}
Grid[x][y] = piece;
Grid[x - 1][y - 1] = obj2;
}
//check en passant
if (obj2 == EMPTY_SQUARE && x == 3 && (Grid[x][y - 1] == BLACK_PAWN && BLACK_EN_PASSANT == y - 1))
{
aux = Grid[x][y - 1];
Grid[x - 1][y - 1] = piece;
Grid[x][y] = EMPTY_SQUARE;
Grid[x][y - 1] = EMPTY_SQUARE;
if (Player_In_Check(Grid, player) == false)
{
Grid[x - 1][y - 1] = EMPTY_SQUARE;
Grid[x][y] = piece;
Grid[x][y - 1] = aux;
return false;
}
Grid[x - 1][y - 1] = EMPTY_SQUARE;
Grid[x][y] = piece;
Grid[x][y - 1] = aux;
}
}
}
if (piece == WHITE_HORSE)
{
//Check all its moves until one can be done
if (x + 2 < 8 && y + 1 < 8)
{
obj1 = Grid[x + 2][y + 1];
if (obj1 == EMPTY_SQUARE || obj1 == BLACK_PAWN || obj1 == BLACK_ROOK || obj1 == BLACK_HORSE || obj1 == BLACK_BISHOP || obj1 == BLACK_QUEEN)
{
Grid[x + 2][y + 1] = piece;
Grid[x][y] = EMPTY_SQUARE;
if (Player_In_Check(Grid, player) == false)
{
Grid[x + 2][y + 1] = obj1;
Grid[x][y] = piece;
return false;
}
Grid[x + 2][y + 1] = obj1;
Grid[x][y] = piece;
}
}
if (x + 2 < 8 && y - 1 >= 0)
{
obj1 = Grid[x + 2][y - 1];
if (obj1 == EMPTY_SQUARE || obj1 == BLACK_PAWN || obj1 == BLACK_ROOK || obj1 == BLACK_HORSE || obj1 == BLACK_BISHOP || obj1 == BLACK_QUEEN)
{
Grid[x + 2][y - 1] = piece;
Grid[x][y] = EMPTY_SQUARE;
if (Player_In_Check(Grid, player) == false)
{