-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchessfunctions.cpp
924 lines (787 loc) · 22.8 KB
/
chessfunctions.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
#include <Adafruit_GFX.h>
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_ILI9341.h>
#include "lcd_image.h"
#include "chessfunctions.h" //contains global variables
#include "validmoves.h" //handles determining if a move is valid
#include "specialcases.h" //handles special moves like castling
/*
================================================================================
LCD STUFF
*/
#define TFT_DC 9
#define TFT_CS 10
#define SD_CS 6
#define DISPLAY_WIDTH 320
#define DISPLAY_HEIGHT 240
#define BOARD_SIZE 240
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// common colors
#define WHITE 0xFFFF
#define YELLOW 0xFFE0
#define RED 0xF800
#define BLACK 0x0000
#define CYAN 0x07FF
uint16_t BROWN = tft.color565(139,69,19);
uint16_t BEIGE = tft.color565(205,133,63);
#define CHOCOBROWN 0x8AE7
#define GREEN 0x07E0
// images of the pieces
// dark and light refers to the background color
lcd_image_t wPawnImgDARK = {"icons/wpawnD.lcd", 25 , 25};
lcd_image_t wRookImgDARK = {"icons/wrookD.lcd", 25 , 25};
lcd_image_t wBishopImgDARK = {"icons/wbishopD.lcd", 25 , 25};
lcd_image_t wKnightImgDARK = {"icons/wknightD.lcd", 25 , 25};
lcd_image_t wKingImgDARK = {"icons/wkingD.lcd", 25 , 25};
lcd_image_t wQueenImgDARK = {"icons/wqueenD.lcd", 25 , 25};
lcd_image_t bPawnImgDARK = {"icons/bpawnD.lcd", 25 , 25};
lcd_image_t bRookImgDARK = {"icons/brookD.lcd", 25 , 25};
lcd_image_t bBishopImgDARK = {"icons/bbishopD.lcd", 25 , 25};
lcd_image_t bKnightImgDARK = {"icons/bknightD.lcd", 25 , 25};
lcd_image_t bKingImgDARK = {"icons/bkingD.lcd", 25 , 25};
lcd_image_t bQueenImgDARK = {"icons/bqueenD.lcd", 25 , 25};
lcd_image_t wPawnImgLIGHT = {"icons/wpawnL.lcd", 25 , 25};
lcd_image_t wRookImgLIGHT = {"icons/wrookL.lcd", 25 , 25};
lcd_image_t wBishopImgLIGHT = {"icons/wbishopL.lcd", 25 , 25};
lcd_image_t wKnightImgLIGHT = {"icons/wknightL.lcd", 25 , 25};
lcd_image_t wKingImgLIGHT = {"icons/wkingL.lcd", 25 , 25};
lcd_image_t wQueenImgLIGHT = {"icons/wqueenL.lcd", 25 , 25};
lcd_image_t bPawnImgLIGHT = {"icons/bpawnL.lcd", 25 , 25};
lcd_image_t bRookImgLIGHT = {"icons/brookL.lcd", 25 , 25};
lcd_image_t bBishopImgLIGHT = {"icons/bbishopL.lcd", 25 , 25};
lcd_image_t bKnightImgLIGHT = {"icons/bknightL.lcd", 25 , 25};
lcd_image_t bKingImgLIGHT = {"icons/bkingL.lcd", 25 , 25};
lcd_image_t bQueenImgLIGHT = {"icons/bqueenL.lcd", 25 , 25};
/*
================================================================================
*/
/*
================================================================================
JOYSTICK STUFF
*/
#define JOY1_VERT A1 // should connect A1 to pin VRx
#define JOY1_HORIZ A0 // should connect A0 to pin VRy
#define JOY1_SEL 2
#define JOY2_VERT A5 //player 2 VRx
#define JOY2_HORIZ A4 // VRy
#define JOY2_SEL 8
#define JOY_CENTER 512
#define JOY_DEADZONE 64
/*
================================================================================
*/
/*
================================================================================
MATRIX STUFF FOR CHESSBOARD
*/
// integers to represent types of pieces
#define EMPTY 0
#define W_PAWN 1
#define W_ROOK 2
#define W_KNIGHT 3
#define W_BISHOP 4
#define W_KING 5
#define W_QUEEN 6
#define B_PAWN -1
#define B_ROOK -2
#define B_KNIGHT -3
#define B_BISHOP -4
#define B_KING -5
#define B_QUEEN -6
//2-D array that represents the board
int board [8][8];
// hold the position of square selected
int selectedY = 0;
int oldSelectedY;
int selectedX = 0;
int oldSelectedX;
// holds position of piece you want to move
int chosenX=10;
int chosenY=10;
//castling
bool p1_kingMoved=0;
bool p1_leftRookmoved=0;
bool p1_rightRookmoved=0;
bool p2_kingMoved=0;
bool p2_leftRookmoved=0;
bool p2_rightRookmoved=0;
//en passant
bool p1_pawn2spaces[9]={0};
bool p2_pawn2spaces[9]={0};
/*
================================================================================
/*
/*
DRAWING FUNCTIONS
================================================================================
*/
/*
Function that draws an empty board to the screen
*/
void drawBoard() {
uint16_t sqColor;
//clear the screen first
tft.fillScreen(0);
//rows where beige is first, brown is second
for (int j=0; j < 4; j++) {
for (int i=0; i < 8; i++) {
if (i%2 == 0) {
sqColor = BEIGE; //yellow
}
else {
sqColor = BROWN; // black
}
tft.fillRect( (BOARD_SIZE/8)*i , (BOARD_SIZE/4)*j , BOARD_SIZE/8, BOARD_SIZE/8, sqColor);
}
}
//rows where brown is first
for (int j=0; j < 4; j++) {
for (int i=0; i < 8; i++) {
if (i%2 == 0) {
sqColor = BROWN;
}
else {
sqColor = BEIGE;
}
//offset the row by 1
tft.fillRect( (BOARD_SIZE/8)*i , BOARD_SIZE/8 + (BOARD_SIZE/4)*j , BOARD_SIZE/8, BOARD_SIZE/8, sqColor);
}
}
// fills in right menu
tft.fillRect(BOARD_SIZE,0,DISPLAY_WIDTH-BOARD_SIZE,DISPLAY_HEIGHT,BLACK);
}
/*
Function that draws the contents of the board array to the screen
*/
void drawArray() {
int piece;
for (int i=0; i<8; i++) {
for (int j=0;j<8;j++) {
piece = board[i][j];
drawPiece(j,i,piece);
}
}
}
/*
Function that clears the square of any pieces
*/
void emptySquare(int squarex, int squarey) {
int sqColor;
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
sqColor = BROWN;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
sqColor = BEIGE;
}
tft.fillRect( (BOARD_SIZE/8)*squarex , (BOARD_SIZE/8)*squarey , BOARD_SIZE/8, BOARD_SIZE/8, sqColor);
}
/*
Function that draws a specified piece onto a specified squarex and squarey,
also uploading a different background color based on the square
*/
void drawPiece(int squarex, int squarey, int piecetype) {
lcd_image_t image;
bool isEmpty = false;
// based on the piece specified, and the square you want to draw it to
// a different image will be loaded
switch (piecetype) {
case EMPTY:
isEmpty = true;
break;
case W_ROOK:
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
image = wRookImgDARK;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
image = wRookImgLIGHT;
}
break;
case W_KNIGHT:
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
image = wKnightImgDARK;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
image = wKnightImgLIGHT;
}
break;
case W_BISHOP:
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
image = wBishopImgDARK;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
image = wBishopImgLIGHT;
}
break;
case W_KING:
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
image = wKingImgDARK;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
image = wKingImgLIGHT;
}
break;
case W_QUEEN:
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
image = wQueenImgDARK;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
image = wQueenImgLIGHT;
}
break;
case W_PAWN:
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
image = wPawnImgDARK;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
image = wPawnImgLIGHT;
}
break;
case B_ROOK:
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
image = bRookImgDARK;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
image = bRookImgLIGHT;
}
break;
case B_KNIGHT:
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
image = bKnightImgDARK;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
image = bKnightImgLIGHT;
}
break;
case B_BISHOP:
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
image = bBishopImgDARK;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
image = bBishopImgLIGHT;
}
break;
case B_KING:
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
image = bKingImgDARK;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
image = bKingImgLIGHT;
}
break;
case B_QUEEN:
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
image = bQueenImgDARK;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
image = bQueenImgLIGHT;
}
break;
case B_PAWN:
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
image = bPawnImgDARK;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
image = bPawnImgLIGHT;
}
break;
}
if (isEmpty == false) {
lcd_image_draw(&image, &tft,1,1,(BOARD_SIZE/8)*squarex +3,(BOARD_SIZE/8)*squarey+3,24, 24);
}
else {
emptySquare(squarex,squarey);
}
}
/*
Function that clears a square (sets it back to its original state)
@param: The x and y index of the square you want to revert
So unhighlightSquare(0,0) will empty the top left square
*/
void unhighlightSquare(int squarex, int squarey) {
int sqColor;
if ( (squarex % 2 !=0 && squarey % 2 == 0) || (squarex % 2 ==0 && squarey % 2 != 0) ) {
sqColor = BROWN;
}
else if ( (squarex % 2 ==0 && squarey % 2 == 0) || (squarex % 2 !=0 && squarey % 2 != 0) ) {
sqColor = BEIGE;
}
tft.drawRect( (BOARD_SIZE/8)*squarex , (BOARD_SIZE/8)*squarey , BOARD_SIZE/8, BOARD_SIZE/8, sqColor);
}
/* Highlights a square
@param: The x and y index of the square you want to highlight
So unhighlightSquare(0,0) will highlight the top left square
Last parameter is optional.
*/
void highlightSquare(int squarex, int squarey, uint16_t bordercolor) {
if (bordercolor == RED) {
tft.drawRect( (BOARD_SIZE/8)*squarex , (BOARD_SIZE/8)*squarey , BOARD_SIZE/8, BOARD_SIZE/8, RED);
}
else if (bordercolor == GREEN) {
tft.drawRect( (BOARD_SIZE/8)*squarex , (BOARD_SIZE/8)*squarey , BOARD_SIZE/8, BOARD_SIZE/8, GREEN);
}
else if (bordercolor == 0x600F){
tft.drawRect( (BOARD_SIZE/8)*squarex , (BOARD_SIZE/8)*squarey , BOARD_SIZE/8, BOARD_SIZE/8, bordercolor );
}
else if (currentplayer == 1 ) {
tft.drawRect( (BOARD_SIZE/8)*squarex , (BOARD_SIZE/8)*squarey , BOARD_SIZE/8, BOARD_SIZE/8, YELLOW);
}
else if (currentplayer == 2) {
tft.drawRect( (BOARD_SIZE/8)*squarex , (BOARD_SIZE/8)*squarey , BOARD_SIZE/8, BOARD_SIZE/8, CYAN );
}
}
/*
This function updates the sidemenu to let the players know whose turn it is
*/
void dispCurrentPlayer(){
tft.setCursor(BOARD_SIZE+8,8);
tft.setTextSize(6);
switch (currentplayer){
case 1:
tft.setTextColor(YELLOW,BLACK);
tft.print("P1");
break;
case 2:
tft.setTextColor(CYAN,BLACK);
tft.print("P2");
break;
}
}
/*
This function accepts input arguments, which allows us to display different
contextual tips based on what is happening in the game, displayed on the
side menu underneath the player indicator
*/
void dispTips(String tip){
tft.fillRect(BOARD_SIZE,60,DISPLAY_WIDTH-BOARD_SIZE,120,BLACK);
tft.setCursor(BOARD_SIZE+5,60);
tft.setTextSize(1);
if (tip =="wrongpiece" || tip == "emptysquare" || tip == "invalidmove" || tip == "leavecheck"){
highlightSquare(selectedX,selectedY,RED);
if(tip == "wrongpiece"){
tft.println("Oops! You");
tft.setCursor(BOARD_SIZE+5,70);
tft.println("picked your");
tft.setCursor(BOARD_SIZE+5,80);
tft.println("opponent's");
tft.setCursor(BOARD_SIZE+5,90);
tft.println("piece, try");
tft.setCursor(BOARD_SIZE+5,100);
tft.println("picking one");
tft.setCursor(BOARD_SIZE+5,110);
tft.println("of yours!");
}
else if(tip == "emptysquare"){
tft.println("You didn't");
tft.setCursor(BOARD_SIZE+5,70);
tft.println("select a");
tft.setCursor(BOARD_SIZE+5,80);
tft.println("piece to");
tft.setCursor(BOARD_SIZE+5,90);
tft.println("move!");
}
else if(tip == "invalidmove"){
tft.println("Oops, you");
tft.setCursor(BOARD_SIZE+5,70);
tft.println("chose an");
tft.setCursor(BOARD_SIZE+5,80);
tft.println("invalid spot");
tft.setCursor(BOARD_SIZE+5,90);
tft.println("to move to");
}
else if(tip == "leavecheck"){
tft.println("Oops, you");
tft.setCursor(BOARD_SIZE+5,70);
tft.println("must get");
tft.setCursor(BOARD_SIZE+5,80);
tft.println("out of the");
tft.setCursor(BOARD_SIZE+5,90);
tft.println("check!");
}
delay(2000);
highlightSquare(selectedX,selectedY, YELLOW);
}
else if(tip=="select"){
tft.println("Pick a piece");
tft.setCursor(BOARD_SIZE+5,70);
tft.println("to move");
}
else if(tip == "move"){
tft.println("Choose a");
tft.setCursor(BOARD_SIZE+5,70);
tft.println("spot to move");
tft.setCursor(BOARD_SIZE+5,80);
tft.println("it to. Or");
tft.setCursor(BOARD_SIZE+5,90);
tft.println("cancel by");
tft.setCursor(BOARD_SIZE+5,100);
tft.println("clicking on");
tft.setCursor(BOARD_SIZE+5,110);
tft.println("the chosen");
tft.setCursor(BOARD_SIZE+5,120);
tft.println("piece again");
tft.setCursor(BOARD_SIZE+5,130);
tft.println("then pick");
tft.setCursor(BOARD_SIZE+5,140);
tft.println("a different");
tft.setCursor(BOARD_SIZE+5,150);
tft.println("piece");
}
else if(tip == "promotion"){
tft.println("Your pawn");
tft.setCursor(BOARD_SIZE+5,70);
tft.println("has been");
tft.setCursor(BOARD_SIZE+5,80);
tft.println("promoted to");
tft.setCursor(BOARD_SIZE+5,90);
tft.println("a queen!");
}
else if(tip == "castled"){
tft.println("You have");
tft.setCursor(BOARD_SIZE+5,70);
tft.println("castled!");
}
else if (tip == "check") {
tft.setTextSize(2);
tft.setTextColor(RED,BLACK);
tft.setCursor(BOARD_SIZE+7,200);
tft.println("Check!");
}
else if(tip == "enpassant"){
tft.println("Hon Hon Hon");
tft.setCursor(BOARD_SIZE+5,70);
tft.println("someone");
tft.setCursor(BOARD_SIZE+5,80);
tft.println("knows the");
tft.setCursor(BOARD_SIZE+5,90);
tft.println("en passant");
tft.setCursor(BOARD_SIZE+5,100);
tft.println("maneuver!");
}
}
/*
GAME LOGIC
================================================================================
*/
/*
Function that fills the board array with the appropriate pieces at the start
of a chess game
*/
void fillBoardArray() {
//pawns
for (int i=0; i<8; i++) {
board[6][i] = W_PAWN;
board[1][i] = B_PAWN;
}
// bottom/ white pieces
board[7][0] = W_ROOK;
board[7][1] = W_KNIGHT;
board[7][2] = W_BISHOP;
board[7][3] = W_QUEEN;
board[7][4] = W_KING;
board[7][5] = W_BISHOP;
board[7][6] = W_KNIGHT;
board[7][7] = W_ROOK;
// top/ black pieces
board[0][0] = B_ROOK;
board[0][1] = B_KNIGHT;
board[0][2] = B_BISHOP;
board[0][3] = B_QUEEN;
board[0][4] = B_KING;
board[0][5] = B_BISHOP;
board[0][6] = B_KNIGHT;
board[0][7] = B_ROOK;
//empty spaces
for (int i=2; i < 6; i++) {
for (int j=0; j<8; j++) {
board[i][j] = EMPTY;
}
}
}
/*
Function that allows the player to scroll through the squares to highlight them
*/
void scroll() {
oldSelectedY = selectedY;
oldSelectedX = selectedX;
int xVal, yVal;
// different joysticks control different players
switch (currentplayer){
case 1:
yVal = analogRead(JOY1_VERT);
xVal = analogRead(JOY1_HORIZ);
break;
case 2:
yVal = analogRead(JOY2_VERT);
xVal = analogRead(JOY2_HORIZ);
break;
}
if (yVal < JOY_CENTER - JOY_DEADZONE) {
selectedY -= 1;
}
else if (yVal > JOY_CENTER + JOY_DEADZONE) {
selectedY += 1;
}
if (xVal < JOY_CENTER - JOY_DEADZONE) {
selectedX += 1;
}
else if (xVal > JOY_CENTER + JOY_DEADZONE) {
selectedX -= 1;
}
selectedY = constrain(selectedY,0,7);
selectedX = constrain(selectedX,0,7);
if (oldSelectedY != selectedY || oldSelectedX != selectedX) {
if (selectedX == chosenX && selectedY == chosenY) {
// don't rehighlight the piece you want to move
}
else {
highlightSquare(selectedX,selectedY);
}
if (oldSelectedX == chosenX && oldSelectedY == chosenY) {
// don't clear the highlighting of the piece you want to move
} else {
unhighlightSquare(oldSelectedX,oldSelectedY);
}
}
delay(100);
}
/*
Function that moves a piece to the selected square, and updates the board array
Relies on the global variables, selectedX and selectedY, to move the piece to
that location
*/
void movePiece(int oldx, int oldy, int pieceToMove) {
board[oldy][oldx] = EMPTY;
board[selectedY][selectedX] = pieceToMove;
emptySquare(oldx,oldy);
drawPiece(selectedX,selectedY,pieceToMove);
//keeps track of moved pieces that determine if castling is allowed
if(pieceToMove==W_ROOK){
if(oldx==0 && oldy==7){
p1_leftRookmoved=1;
}
else if(oldx==7 && oldy==7){
p1_rightRookmoved=1;
}
}
else if(pieceToMove==B_ROOK){
if(oldx==0 && oldy==0){
p2_leftRookmoved=1;
}
else if(oldx==7 && oldy==0){
p2_rightRookmoved=1;
}
}
else if(pieceToMove==W_KING){
p1_kingMoved=1;
}
else if(pieceToMove==B_KING){
p2_kingMoved=1;
}
}
/*
Once a piece is selected, player enters this mode to actually move the piece
where they want
*/
void moveMode() {
// keep track of who is in check (if anyone)
bool checkBlack;
bool checkWhite;
int pieceToMove = board[selectedY][selectedX];
//These contextual tips were the first ones added
if (pieceToMove == EMPTY) {
dispTips("emptysquare");
return;
}
else if (pieceToMove > 0 && currentplayer == 2) {
//cant move opponents piece
//flash square red to let player know that square can't be moved
//highlightSquare(selectedX,selectedY,RED);
dispTips("wrongpiece");
return;
}
else if (pieceToMove < 0 && currentplayer == 1) {
// cant move opponents piece
//flash square red to let player know that square can't be moved
//highlightSquare(selectedX,selectedY,RED);
dispTips("wrongpiece");
return;
}
else{
dispTips("move");
}
chosenX = selectedX;
chosenY = selectedY;
// the piece you're moving is now highlighted green
highlightSquare(chosenX,chosenY,GREEN);
bool highlighted;
while(true) {
scroll();
// show valid moves only when you're on the piece that you selected
if (selectedX == chosenX && selectedY == chosenY) {
highlightValid(pieceToMove);
highlighted = true;
}
else if (highlighted == true ){
unhighlightValid(pieceToMove);
highlightSquare(selectedX,selectedY);
highlighted = false;
}
//player 1 selects a place to move
if (currentplayer == 1 && digitalRead(JOY1_SEL)==0){
while (digitalRead(JOY1_SEL) == LOW) { delay(10); }
if (selectedX == chosenX && selectedY == chosenY){
//same piece selected, cancel the move
break;
}
checkWhite = checkOnWhite(pieceToMove,selectedX,selectedY);
bool valid = validateMove(pieceToMove,selectedX,selectedY,board) && checkObstruction(pieceToMove,selectedX,selectedY,board);
bool specialcase = checkSpecialcases(selectedX,selectedY,pieceToMove);
if (specialcase && !checkWhite){
//if a special case already happened, that function took care of
//moving the pieces already
//clear the check message
tft.fillRect(BOARD_SIZE,180,DISPLAY_WIDTH-BOARD_SIZE,240-180,BLACK);
currentplayer=2;
break;
}
else if (valid && !checkWhite) {
//move is valid so move the piece
movePiece(chosenX,chosenY,pieceToMove);
currentplayer = 2;
//clear the check message
tft.fillRect(BOARD_SIZE,180,DISPLAY_WIDTH-BOARD_SIZE,240-180,BLACK);
//update enpassant array
if(p2_pawn2spaces[8]){
memset(p2_pawn2spaces,0,sizeof(p2_pawn2spaces));
}
//end the move
break;
}
else if(!valid && !checkWhite){
dispTips("invalidmove");
dispTips("move");
}
else if (checkWhite) {
dispTips("leavecheck");
dispTips("move");
}
}
// player 2 selects where they want to move
else if (currentplayer ==2 && digitalRead(JOY2_SEL)==0){
while (digitalRead(JOY2_SEL) == LOW) { delay(10); }
if (selectedX == chosenX && selectedY == chosenY){
//same piece selected, cancel the move
break;
}
checkBlack = checkOnBlack(pieceToMove,selectedX,selectedY);
bool valid = validateMove(pieceToMove,selectedX,selectedY,board) && checkObstruction(pieceToMove,selectedX,selectedY,board);
bool specialcase = checkSpecialcases(selectedX,selectedY,pieceToMove);
if (specialcase && !checkBlack){
//if a special case already happened, that function took care of
//moving the pieces already
//clear the check message
tft.fillRect(BOARD_SIZE,180,DISPLAY_WIDTH-BOARD_SIZE,240-180,BLACK);
currentplayer=1;
break;
}
else if (valid && !checkBlack) {
movePiece(chosenX,chosenY,pieceToMove);
currentplayer = 1;
//clear the check message
tft.fillRect(BOARD_SIZE,180,DISPLAY_WIDTH-BOARD_SIZE,240-180,BLACK);
//update enpassant array
if(p1_pawn2spaces[8]){
memset(p1_pawn2spaces,0,sizeof(p1_pawn2spaces));
}
//end the move
break;
}
else if(!valid && !checkBlack){
dispTips("invalidmove");
dispTips("move");
}
else if (checkBlack) {
dispTips("leavecheck");
dispTips("move");
}
}
}
unhighlightValid(pieceToMove);
highlightSquare(selectedX,selectedY);
// set chosen piece to nothing
chosenX = 10;
chosenY = 10;
//look for a checkmate for a potential end game
if (checkOnWhite()) {
if (checkmate("white")) {
//P2 wins
gameover = 2;
}
else {
dispTips("check");
}
}
else if (checkOnBlack()) {
if (checkmate("black")) {
//P1 wins
gameover = 1;
}
else {
dispTips("check");
}
}
}
/*
Stops the game and displays the winner
*/
void endGame( int player) {
unhighlightSquare(selectedX,selectedY);
tft.fillRect(BOARD_SIZE,0,DISPLAY_WIDTH-BOARD_SIZE,DISPLAY_HEIGHT,BLACK);
tft.setTextSize(2);
tft.setCursor(BOARD_SIZE+10,10);
tft.setTextColor(RED,BLACK);
tft.println("CHECK");
tft.setCursor(BOARD_SIZE+10,30);
tft.println("MATE!");
tft.setTextSize(3);
tft.setCursor(BOARD_SIZE+22,70);
switch (player) {
case 1:
tft.setTextColor(YELLOW,BLACK);
tft.println("P1");
break;
case 2:
tft.setTextColor(CYAN,BLACK);
tft.println("P2");
break;
}
tft.setTextSize(2);
tft.setCursor(BOARD_SIZE+13,100);
tft.println("WINS!");
}
/*
================================================================================
*/
void setup() {
init();
Serial.begin(9600);
tft.begin();
pinMode(JOY1_SEL, INPUT_PULLUP);
pinMode(JOY2_SEL,INPUT_PULLUP);
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("failed! Is it inserted properly?");
while (true) {}
}
Serial.println("OK!");
tft.setRotation(3);
// setup the board for the start of the game
drawBoard();
fillBoardArray();
drawArray();
tft.setCursor(DISPLAY_WIDTH-(DISPLAY_WIDTH- BOARD_SIZE),0);
dispCurrentPlayer();
dispTips("select");
}