forked from Concept-Bytes/Open-Chess
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchess_bot.cpp
More file actions
1149 lines (979 loc) · 40.6 KB
/
chess_bot.cpp
File metadata and controls
1149 lines (979 loc) · 40.6 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
#include "chess_bot.h"
#include <Arduino.h>
ChessBot::ChessBot(BoardDriver* boardDriver, ChessEngine* chessEngine, BotDifficulty diff, bool playerWhite) {
_boardDriver = boardDriver;
_chessEngine = chessEngine;
difficulty = diff;
playerIsWhite = playerWhite;
// Set difficulty settings
switch(difficulty) {
case BOT_EASY: settings = StockfishSettings::easy(); break;
case BOT_MEDIUM: settings = StockfishSettings::medium(); break;
case BOT_HARD: settings = StockfishSettings::hard(); break;
case BOT_EXPERT: settings = StockfishSettings::expert(); break;
}
// Set initial turn: In chess, White always moves first
// If player is White, player goes first (isWhiteTurn = true)
// If player is Black, bot (White) goes first (isWhiteTurn = true)
isWhiteTurn = true; // White always moves first in chess
gameStarted = false;
botThinking = false;
wifiConnected = false;
currentEvaluation = 0.0;
}
void ChessBot::begin() {
Serial.println("=== Starting Chess Bot Mode ===");
Serial.print("Player plays: ");
Serial.println(playerIsWhite ? "White" : "Black");
Serial.print("Bot plays: ");
Serial.println(playerIsWhite ? "Black" : "White");
Serial.print("Bot Difficulty: ");
switch(difficulty) {
case BOT_EASY: Serial.println("Easy (Depth 6)"); break;
case BOT_MEDIUM: Serial.println("Medium (Depth 10)"); break;
case BOT_HARD: Serial.println("Hard (Depth 14)"); break;
case BOT_EXPERT: Serial.println("Expert (Depth 16)"); break;
}
_boardDriver->clearAllLEDs();
_boardDriver->showLEDs();
// Connect to WiFi
Serial.println("Connecting to WiFi...");
showConnectionStatus();
if (connectToWiFi()) {
Serial.println("WiFi connected! Bot mode ready.");
wifiConnected = true;
// Show success animation
for (int i = 0; i < 3; i++) {
_boardDriver->clearAllLEDs();
_boardDriver->showLEDs();
delay(200);
// Light up entire board green briefly
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
_boardDriver->setSquareLED(row, col, 0, 255, 0); // Green
}
}
_boardDriver->showLEDs();
delay(200);
}
initializeBoard();
waitForBoardSetup();
} else {
Serial.println("Failed to connect to WiFi. Bot mode unavailable.");
wifiConnected = false;
// Show error animation (red flashing)
for (int i = 0; i < 5; i++) {
_boardDriver->clearAllLEDs();
_boardDriver->showLEDs();
delay(300);
// Light up entire board red briefly
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
_boardDriver->setSquareLED(row, col, 255, 0, 0); // Red
}
}
_boardDriver->showLEDs();
delay(300);
}
_boardDriver->clearAllLEDs();
_boardDriver->showLEDs();
}
}
void ChessBot::update() {
if (!wifiConnected) {
return; // No WiFi, can't play against bot
}
if (!gameStarted) {
return; // Waiting for initial setup
}
if (botThinking) {
showBotThinking();
return;
}
_boardDriver->readSensors();
// Detect piece movements (player's turn)
bool isPlayerTurn = (playerIsWhite && isWhiteTurn) || (!playerIsWhite && !isWhiteTurn);
if (isPlayerTurn) {
static unsigned long lastTurnDebug = 0;
if (millis() - lastTurnDebug > 5000) {
Serial.print("Your turn! Move a ");
Serial.println(playerIsWhite ? "WHITE piece (uppercase letters)" : "BLACK piece (lowercase letters)");
lastTurnDebug = millis();
}
// Look for piece pickups and placements
static int selectedRow = -1, selectedCol = -1;
static bool piecePickedUp = false;
// Check for piece pickup
if (!piecePickedUp) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (!_boardDriver->getSensorState(row, col) && _boardDriver->getSensorPrev(row, col)) {
// Check what piece was picked up
char piece = board[row][col];
if (piece != ' ') {
// Player should only be able to move their own pieces
bool isPlayerPiece = (playerIsWhite && piece >= 'A' && piece <= 'Z') ||
(!playerIsWhite && piece >= 'a' && piece <= 'z');
if (isPlayerPiece) {
selectedRow = row;
selectedCol = col;
piecePickedUp = true;
Serial.print("Player picked up ");
Serial.print(playerIsWhite ? "WHITE" : "BLACK");
Serial.print(" piece '");
Serial.print(board[row][col]);
Serial.print("' at ");
Serial.print((char)('a' + col));
Serial.print(8 - row);
Serial.print(" (array position ");
Serial.print(row);
Serial.print(",");
Serial.print(col);
Serial.println(")");
// Show selected square
_boardDriver->setSquareLED(row, col, 255, 0, 0); // Red
// Show possible moves
int moveCount = 0;
int moves[27][2];
_chessEngine->getPossibleMoves(board, row, col, moveCount, moves);
for (int i = 0; i < moveCount; i++) {
_boardDriver->setSquareLED(moves[i][0], moves[i][1], 0, 0, 0, 255); // Bright white using W channel
}
_boardDriver->showLEDs();
break;
} else {
// Player tried to pick up the wrong color piece
Serial.print("ERROR: You tried to pick up ");
Serial.print((piece >= 'A' && piece <= 'Z') ? "WHITE" : "BLACK");
Serial.print(" piece '");
Serial.print(piece);
Serial.print("' at ");
Serial.print((char)('a' + col));
Serial.print(8 - row);
Serial.print(". You can only move ");
Serial.print(playerIsWhite ? "WHITE" : "BLACK");
Serial.println(" pieces!");
// Flash red to indicate error
_boardDriver->blinkSquare(row, col, 3);
}
}
}
}
if (piecePickedUp) break;
}
}
// Check for piece placement
if (piecePickedUp) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (_boardDriver->getSensorState(row, col) && !_boardDriver->getSensorPrev(row, col)) {
// Check if piece was returned to its original position
if (row == selectedRow && col == selectedCol) {
// Piece returned to original position - cancel selection
Serial.println("Piece returned to original position. Selection cancelled.");
piecePickedUp = false;
selectedRow = selectedCol = -1;
// Clear all indicators
_boardDriver->clearAllLEDs();
_boardDriver->showLEDs();
break;
}
// Piece placed somewhere else - validate move
int moveCount = 0;
int moves[27][2];
_chessEngine->getPossibleMoves(board, selectedRow, selectedCol, moveCount, moves);
bool validMove = false;
for (int i = 0; i < moveCount; i++) {
if (moves[i][0] == row && moves[i][1] == col) {
validMove = true;
break;
}
}
if (validMove) {
char piece = board[selectedRow][selectedCol];
// Complete LED animations BEFORE API request
processPlayerMove(selectedRow, selectedCol, row, col, piece);
// Flash confirmation on destination square for player move
confirmSquareCompletion(row, col);
piecePickedUp = false;
selectedRow = selectedCol = -1;
// Switch to bot's turn
// If player is White, bot is Black (isWhiteTurn = false)
// If player is Black, bot is White (isWhiteTurn = true)
isWhiteTurn = !playerIsWhite;
botThinking = true;
Serial.println("Player move completed. Bot thinking...");
// Start bot move calculation
makeBotMove();
} else {
Serial.println("Invalid move! Please try again.");
_boardDriver->blinkSquare(row, col, 3); // Blink red for invalid move
// Restore move indicators - piece is still selected
_boardDriver->clearAllLEDs();
// Show selected square again
_boardDriver->setSquareLED(selectedRow, selectedCol, 255, 0, 0); // Red
// Show possible moves again
int moveCount = 0;
int moves[27][2];
_chessEngine->getPossibleMoves(board, selectedRow, selectedCol, moveCount, moves);
for (int i = 0; i < moveCount; i++) {
_boardDriver->setSquareLED(moves[i][0], moves[i][1], 0, 0, 0, 255); // Bright white using W channel
}
_boardDriver->showLEDs();
Serial.println("Piece is still selected. Place it on a valid move or return it to its original position.");
}
break;
}
}
}
}
} else {
// Bot's turn - if player is Black, bot (White) goes first
if (!botThinking && !playerIsWhite && isWhiteTurn) {
// Bot plays White and it's White's turn
botThinking = true;
makeBotMove();
} else if (!botThinking && playerIsWhite && !isWhiteTurn) {
// Bot plays Black and it's Black's turn
botThinking = true;
makeBotMove();
}
}
_boardDriver->updateSensorPrev();
}
bool ChessBot::connectToWiFi() {
#if defined(ESP32) || defined(ESP8266)
// ESP32/ESP8266 WiFi connection
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
WiFi.mode(WIFI_STA); // Set WiFi to station mode
WiFi.begin(SECRET_SSID, SECRET_PASS);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
attempts++;
Serial.print("Connection attempt ");
Serial.print(attempts);
Serial.print("/20 - Status: ");
Serial.println(WiFi.status());
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to WiFi!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
return true;
} else {
Serial.println("Failed to connect to WiFi");
return false;
}
#elif defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_NANO_RP2040_CONNECT)
// WiFiNINA boards
// Check for WiFi module
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("WiFi module not found!");
return false;
}
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 10) {
WiFi.begin(SECRET_SSID, SECRET_PASS);
delay(5000);
attempts++;
Serial.print("Connection attempt ");
Serial.print(attempts);
Serial.print("/10 - Status: ");
Serial.println(WiFi.status());
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to WiFi!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
return true;
} else {
Serial.println("Failed to connect to WiFi");
return false;
}
#else
Serial.println("WiFi not supported on this board");
return false;
#endif
}
String ChessBot::makeStockfishRequest(String fen) {
WiFiSSLClient client;
#if defined(ESP32) || defined(ESP8266)
// ESP32/ESP8266: Set insecure mode for SSL (or add proper certificate validation)
// For production, you should validate certificates properly
client.setInsecure();
#endif
Serial.println("Making API request to Stockfish...");
Serial.print("FEN: ");
Serial.println(fen);
// Retry logic
for (int attempt = 1; attempt <= settings.maxRetries; attempt++) {
Serial.print("Attempt ");
Serial.print(attempt);
Serial.print("/");
Serial.println(settings.maxRetries);
if (client.connect(STOCKFISH_API_URL, STOCKFISH_API_PORT)) {
// URL encode the FEN string
String encodedFen = urlEncode(fen);
// Make HTTP GET request
String url = String(STOCKFISH_API_PATH) + "?fen=" + encodedFen + "&depth=" + String(settings.depth);
Serial.print("Request URL: ");
Serial.println(url);
client.println("GET " + url + " HTTP/1.1");
client.println("Host: " + String(STOCKFISH_API_URL));
client.println("Connection: close");
client.println();
// Wait for response
unsigned long startTime = millis();
String response = "";
bool gotResponse = false;
while (client.connected() && (millis() - startTime < settings.timeoutMs)) {
if (client.available()) {
response = client.readString();
gotResponse = true;
break;
}
delay(10);
}
client.stop();
if (gotResponse && response.length() > 0) {
// Debug: Print raw response (truncated if too long)
Serial.println("=== RAW API RESPONSE ===");
if (response.length() > 500) {
Serial.println(response.substring(0, 500) + "... (truncated)");
} else {
Serial.println(response);
}
Serial.println("=== END RAW RESPONSE ===");
return response;
} else {
Serial.println("API request timeout or empty response");
if (attempt < settings.maxRetries) {
Serial.println("Retrying...");
delay(1000); // Wait before retry
}
}
} else {
Serial.println("Failed to connect to Stockfish API");
if (attempt < settings.maxRetries) {
Serial.println("Retrying...");
delay(1000); // Wait before retry
}
}
}
Serial.println("All API request attempts failed");
return "";
}
bool ChessBot::parseStockfishResponse(String response, String &bestMove, float &evaluation) {
// Initialize evaluation to 0 (neutral)
evaluation = 0.0;
// Find JSON content
int jsonStart = response.indexOf("{");
if (jsonStart == -1) {
Serial.println("No JSON found in response");
Serial.println("Response was: " + response);
return false;
}
String json = response.substring(jsonStart);
Serial.print("Extracted JSON: ");
Serial.println(json);
// Check if request was successful (some APIs use "success":true, others just return the move)
bool hasSuccess = json.indexOf("\"success\"") >= 0;
if (hasSuccess && json.indexOf("\"success\":true") == -1) {
Serial.println("API request was not successful");
return false;
}
// Parse evaluation - try different possible field names
// Format 1: "evaluation": 0.5 (in pawns)
// Format 2: "evaluation": 50 (in centipawns)
// Format 3: "score": 0.5
// Format 4: "cp": 50 (centipawns)
int evalStart = json.indexOf("\"evaluation\":");
if (evalStart == -1) {
evalStart = json.indexOf("\"score\":");
if (evalStart == -1) {
evalStart = json.indexOf("\"cp\":");
if (evalStart >= 0) {
evalStart += 5; // Skip "cp":
}
} else {
evalStart += 8; // Skip "score":
}
} else {
evalStart += 14; // Skip "evaluation":
}
if (evalStart >= 0) {
// Find the number value
String evalStr = json.substring(evalStart);
evalStr.trim();
// Remove any leading whitespace or quotes
while (evalStr.length() > 0 && (evalStr[0] == ' ' || evalStr[0] == '"' || evalStr[0] == '\'')) {
evalStr = evalStr.substring(1);
}
// Find the end of the number (comma, }, or whitespace)
int evalEnd = evalStr.length();
for (int i = 0; i < evalStr.length(); i++) {
char c = evalStr[i];
if (c == ',' || c == '}' || c == ' ' || c == '\n' || c == '\r') {
evalEnd = i;
break;
}
}
evalStr = evalStr.substring(0, evalEnd);
evalStr.trim();
if (evalStr.length() > 0) {
evaluation = evalStr.toFloat();
// If evaluation is small (< 10), assume it's in pawns, convert to centipawns
// If evaluation is large (> 10), assume it's already in centipawns
if (evaluation > -10 && evaluation < 10) {
evaluation = evaluation * 100.0; // Convert pawns to centipawns
}
Serial.print("Parsed evaluation: ");
Serial.print(evaluation);
Serial.println(" centipawns");
}
} else {
Serial.println("No evaluation field found in response");
}
// Parse bestmove field - try different possible formats
// Format 1: "bestmove":"e2e4"
// Format 2: "bestmove":"bestmove e2e4 ponder e7e5"
// Format 3: {"move":"e2e4"}
int bestmoveStart = json.indexOf("\"bestmove\":\"");
if (bestmoveStart == -1) {
// Try alternative format with just "move"
bestmoveStart = json.indexOf("\"move\":\"");
if (bestmoveStart == -1) {
Serial.println("No bestmove or move field found in response");
return false;
}
bestmoveStart += 8; // Skip "move":"
} else {
bestmoveStart += 12; // Skip "bestmove":"
}
int bestmoveEnd = json.indexOf("\"", bestmoveStart);
if (bestmoveEnd == -1) {
Serial.println("Invalid bestmove format - no closing quote");
return false;
}
String fullMove = json.substring(bestmoveStart, bestmoveEnd);
Serial.print("Full move string: ");
Serial.println(fullMove);
// Check if the move string contains "bestmove " prefix (some APIs include it)
int moveStart = fullMove.indexOf("bestmove ");
if (moveStart >= 0) {
moveStart += 9; // Skip "bestmove "
int moveEnd = fullMove.indexOf(" ", moveStart);
if (moveEnd == -1) {
// No ponder part, take rest of string
bestMove = fullMove.substring(moveStart);
} else {
bestMove = fullMove.substring(moveStart, moveEnd);
}
} else {
// Move is directly in the field value
bestMove = fullMove;
}
// Clean up the move - remove any whitespace
bestMove.trim();
Serial.print("Parsed move: ");
Serial.println(bestMove);
// Validate move format (should be 4-5 characters like "e2e4" or "e7e8q")
if (bestMove.length() < 4 || bestMove.length() > 5) {
Serial.print("Invalid move length: ");
Serial.println(bestMove.length());
return false;
}
return true;
}
void ChessBot::makeBotMove() {
Serial.println("=== BOT MOVE CALCULATION ===");
Serial.print("Bot is playing as: ");
Serial.println(isWhiteTurn ? "White" : "Black");
// Show thinking animation
showBotThinking();
String fen = boardToFEN();
Serial.print("Sending FEN to Stockfish: ");
Serial.println(fen);
String response = makeStockfishRequest(fen);
if (response.length() > 0) {
String bestMove;
float evaluation = 0.0;
if (parseStockfishResponse(response, bestMove, evaluation)) {
// Store and print evaluation
currentEvaluation = evaluation;
Serial.print("=== STOCKFISH EVALUATION ===");
Serial.println();
if (evaluation > 0) {
Serial.print("White advantage: +");
Serial.print(evaluation / 100.0, 2);
Serial.println(" pawns");
} else if (evaluation < 0) {
Serial.print("Black advantage: ");
Serial.print(evaluation / 100.0, 2);
Serial.println(" pawns");
} else {
Serial.println("Position is equal (0.00 pawns)");
}
Serial.print("Evaluation in centipawns: ");
Serial.println(evaluation);
Serial.println("============================");
int fromRow, fromCol, toRow, toCol;
if (parseMove(bestMove, fromRow, fromCol, toRow, toCol)) {
Serial.print("Bot calculated move: ");
Serial.println(bestMove);
// Verify the move is from the correct color piece
// Bot plays White if player is Black, Bot plays Black if player is White
char piece = board[fromRow][fromCol];
bool botPlaysWhite = !playerIsWhite;
bool isBotPiece = (botPlaysWhite && piece >= 'A' && piece <= 'Z') ||
(!botPlaysWhite && piece >= 'a' && piece <= 'z');
if (!isBotPiece) {
Serial.print("ERROR: Bot tried to move a ");
Serial.print((piece >= 'A' && piece <= 'Z') ? "WHITE" : "BLACK");
Serial.print(" piece, but bot plays ");
Serial.println(botPlaysWhite ? "WHITE" : "BLACK");
Serial.print("Piece at source: ");
Serial.println(piece);
botThinking = false;
return;
}
if (piece == ' ') {
Serial.println("ERROR: Bot tried to move from an empty square!");
botThinking = false;
return;
}
executeBotMove(fromRow, fromCol, toRow, toCol);
// Switch back to player's turn
// If player is White, isWhiteTurn = true; if player is Black, isWhiteTurn = false
isWhiteTurn = playerIsWhite;
botThinking = false;
Serial.println("Bot move completed. Your turn!");
} else {
Serial.print("Failed to parse bot move: ");
Serial.println(bestMove);
botThinking = false;
}
} else {
Serial.println("Failed to parse Stockfish response");
Serial.print("Response was: ");
if (response.length() > 200) {
Serial.println(response.substring(0, 200) + "... (truncated)");
} else {
Serial.println(response);
}
botThinking = false;
}
} else {
Serial.println("No response from Stockfish API after all retries");
botThinking = false;
}
}
String ChessBot::boardToFEN() {
String fen = "";
// Board position - FEN expects rank 8 (black pieces) first, rank 1 (white pieces) last
// Our array: row 0 = rank 1 (white pieces at bottom), row 7 = rank 8 (black pieces at top)
// So we need to reverse the order: start from row 7 and go to row 0
for (int row = 7; row >= 0; row--) {
int emptyCount = 0;
for (int col = 0; col < 8; col++) {
if (board[row][col] == ' ') {
emptyCount++;
} else {
if (emptyCount > 0) {
fen += String(emptyCount);
emptyCount = 0;
}
fen += board[row][col];
}
}
if (emptyCount > 0) {
fen += String(emptyCount);
}
if (row > 0) fen += "/";
}
// Active color - when we call this for bot's move, isWhiteTurn is false (bot is Black)
// So we correctly indicate it's Black's turn
fen += isWhiteTurn ? " w" : " b";
// Castling availability (simplified - assume all available initially)
fen += " KQkq";
// En passant target square (simplified - assume none)
fen += " -";
// Halfmove clock (simplified)
fen += " 0";
// Fullmove number (simplified)
fen += " 1";
Serial.print("Generated FEN: ");
Serial.println(fen);
Serial.print("Active color: ");
Serial.println(isWhiteTurn ? "White" : "Black");
return fen;
}
void ChessBot::fenToBoard(String fen) {
// Parse FEN string and update board state
// FEN format: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
// We only parse the board part (first part before space)
int spacePos = fen.indexOf(' ');
if (spacePos > 0) {
fen = fen.substring(0, spacePos);
}
// Clear board
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
board[row][col] = ' ';
}
}
// Parse FEN ranks (rank 8 first, rank 1 last)
// FEN: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
// Our array: row 0 = rank 1, row 7 = rank 8
int rank = 7; // Start with rank 8 (row 7 in our array)
int col = 0;
for (int i = 0; i < fen.length() && rank >= 0; i++) {
char c = fen.charAt(i);
if (c == '/') {
// Next rank
rank--;
col = 0;
} else if (c >= '1' && c <= '8') {
// Empty squares
int emptyCount = c - '0';
col += emptyCount;
} else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
// Piece
if (rank >= 0 && rank < 8 && col >= 0 && col < 8) {
board[rank][col] = c;
col++;
}
}
}
Serial.println("Board updated from FEN");
printCurrentBoard();
}
bool ChessBot::parseMove(String move, int &fromRow, int &fromCol, int &toRow, int &toCol) {
if (move.length() < 4) {
Serial.print("Move too short: ");
Serial.println(move);
return false;
}
// Parse chess notation (e.g., "e2e4")
// File (column): a-h -> 0-7
// Rank (row): 1-8 -> In our array: rank 1 = row 0, rank 8 = row 7
// So rank 1 -> row 0, rank 2 -> row 1, ..., rank 8 -> row 7
char fromFile = move.charAt(0);
char fromRank = move.charAt(1);
char toFile = move.charAt(2);
char toRank = move.charAt(3);
// Validate file characters
if (fromFile < 'a' || fromFile > 'h' || toFile < 'a' || toFile > 'h') {
Serial.println("Invalid file in move");
return false;
}
// Validate rank characters
if (fromRank < '1' || fromRank > '8' || toRank < '1' || toRank > '8') {
Serial.println("Invalid rank in move");
return false;
}
fromCol = fromFile - 'a';
fromRow = (fromRank - '0') - 1; // Convert 1-8 to 0-7 (rank 1 = row 0)
toCol = toFile - 'a';
toRow = (toRank - '0') - 1; // Convert 1-8 to 0-7
// Debug coordinate conversion
Serial.print("Move string: ");
Serial.println(move);
Serial.print("Parsed: ");
Serial.print(fromFile);
Serial.print(fromRank);
Serial.print(" -> ");
Serial.print(toFile);
Serial.print(toRank);
Serial.print(" | Array coords: (");
Serial.print(fromRow);
Serial.print(",");
Serial.print(fromCol);
Serial.print(") to (");
Serial.print(toRow);
Serial.print(",");
Serial.print(toCol);
Serial.println(")");
// Check for promotion
if (move.length() >= 5) {
char promotionPiece = move.charAt(4);
Serial.print("Promotion to: ");
Serial.println(promotionPiece);
}
// Validate coordinates
bool valid = (fromRow >= 0 && fromRow < 8 && fromCol >= 0 && fromCol < 8 &&
toRow >= 0 && toRow < 8 && toCol >= 0 && toCol < 8);
if (!valid) {
Serial.println("Invalid coordinates after parsing");
}
return valid;
}
void ChessBot::executeBotMove(int fromRow, int fromCol, int toRow, int toCol) {
char piece = board[fromRow][fromCol];
char capturedPiece = board[toRow][toCol];
// Update board state
board[toRow][toCol] = piece;
board[fromRow][fromCol] = ' ';
Serial.print("Bot wants to move piece from ");
Serial.print((char)('a' + fromCol));
Serial.print(8 - fromRow);
Serial.print(" to ");
Serial.print((char)('a' + toCol));
Serial.println(8 - toRow);
Serial.println("Please make this move on the physical board...");
// Show the move that needs to be made
showBotMoveIndicator(fromRow, fromCol, toRow, toCol);
// Wait for user to physically complete the bot's move
waitForBotMoveCompletion(fromRow, fromCol, toRow, toCol);
if (capturedPiece != ' ') {
Serial.print("Piece captured: ");
Serial.println(capturedPiece);
_boardDriver->captureAnimation();
}
// Flash confirmation on the destination square
confirmSquareCompletion(toRow, toCol);
Serial.println("Bot move completed. Your turn!");
}
void ChessBot::showBotThinking() {
static unsigned long lastUpdate = 0;
static int thinkingStep = 0;
if (millis() - lastUpdate > 500) {
// Animated thinking indicator - pulse the corners
_boardDriver->clearAllLEDs();
uint8_t brightness = (sin(thinkingStep * 0.3) + 1) * 127;
_boardDriver->setSquareLED(0, 0, 0, 0, brightness); // Corner LEDs pulse blue
_boardDriver->setSquareLED(0, 7, 0, 0, brightness);
_boardDriver->setSquareLED(7, 0, 0, 0, brightness);
_boardDriver->setSquareLED(7, 7, 0, 0, brightness);
_boardDriver->showLEDs();
thinkingStep++;
lastUpdate = millis();
}
}
void ChessBot::showConnectionStatus() {
// Show WiFi connection attempt with animated LEDs
for (int i = 0; i < 8; i++) {
_boardDriver->setSquareLED(3, i, 0, 0, 255); // Blue row
_boardDriver->showLEDs();
delay(200);
}
}
void ChessBot::initializeBoard() {
// Copy initial board state
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
board[row][col] = INITIAL_BOARD[row][col];
}
}
}
void ChessBot::waitForBoardSetup() {
Serial.println("Please set up the chess board in starting position...");
while (!_boardDriver->checkInitialBoard(INITIAL_BOARD)) {
_boardDriver->readSensors();
_boardDriver->updateSetupDisplay(INITIAL_BOARD);
_boardDriver->showLEDs();
delay(100);
}
Serial.println("Board setup complete! Game starting...");
_boardDriver->fireworkAnimation();
gameStarted = true;
// Show initial board state
printCurrentBoard();
}
void ChessBot::processPlayerMove(int fromRow, int fromCol, int toRow, int toCol, char piece) {
char capturedPiece = board[toRow][toCol];
// Update board state
board[toRow][toCol] = piece;
board[fromRow][fromCol] = ' ';
Serial.print("Player moved ");
Serial.print(piece);
Serial.print(" from ");
Serial.print((char)('a' + fromCol));
Serial.print(8 - fromRow);
Serial.print(" to ");
Serial.print((char)('a' + toCol));
Serial.println(8 - toRow);
if (capturedPiece != ' ') {
Serial.print("Captured ");
Serial.println(capturedPiece);
_boardDriver->captureAnimation();
}
// Check for pawn promotion
if (_chessEngine->isPawnPromotion(piece, toRow)) {
char promotedPiece = _chessEngine->getPromotedPiece(piece);
board[toRow][toCol] = promotedPiece;
Serial.print("Pawn promoted to ");
Serial.println(promotedPiece);
_boardDriver->promotionAnimation(toCol);
}
}
String ChessBot::urlEncode(String str) {
String encoded = "";
char c;
char code0;
char code1;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c == ' ') {
encoded += "%20";
} else if (c == '/') {
encoded += "%2F";
} else if (isalnum(c)) {
encoded += c;
} else {
code1 = (c & 0xf) + '0';
if ((c & 0xf) > 9) {
code1 = (c & 0xf) - 10 + 'A';
}
c = (c >> 4) & 0xf;
code0 = c + '0';
if (c > 9) {
code0 = c - 10 + 'A';
}
encoded += '%';
encoded += code0;
encoded += code1;
}
}
return encoded;
}
void ChessBot::showBotMoveIndicator(int fromRow, int fromCol, int toRow, int toCol) {
// Clear all LEDs first
_boardDriver->clearAllLEDs();
// Show source square flashing (where to pick up from)
// Use white channel (W) for much brighter display
_boardDriver->setSquareLED(fromRow, fromCol, 0, 0, 0, 255); // Bright white using W channel