-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathchess_moves.cpp
More file actions
320 lines (264 loc) · 12.9 KB
/
chess_moves.cpp
File metadata and controls
320 lines (264 loc) · 12.9 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
#include "chess_moves.h"
#include <Arduino.h>
// Expected initial configuration (as printed in the grid)
const char ChessMoves::INITIAL_BOARD[8][8] = {
{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}, // row 0 (rank 1)
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, // row 1 (rank 2)
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, // row 2 (rank 3)
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, // row 3 (rank 4)
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, // row 4 (rank 5)
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, // row 5 (rank 6)
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'}, // row 6 (rank 7)
{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'} // row 7 (rank 8)
};
ChessMoves::ChessMoves(BoardDriver* bd, ChessEngine* ce) : boardDriver(bd), chessEngine(ce) {
// Initialize board state
initializeBoard();
}
void ChessMoves::begin() {
Serial.println("Starting Chess Game Mode...");
// Copy expected configuration into our board state
initializeBoard();
// Wait for board setup
waitForBoardSetup();
Serial.println("Chess game ready to start!");
boardDriver->fireworkAnimation();
// Initialize sensor previous state for move detection
boardDriver->readSensors();
boardDriver->updateSensorPrev();
}
void ChessMoves::update() {
boardDriver->readSensors();
// Look for a piece pickup
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (boardDriver->getSensorPrev(row, col) && !boardDriver->getSensorState(row, col)) {
char piece = board[row][col];
// Skip empty squares
if (piece == ' ') continue;
Serial.print("Piece lifted from ");
Serial.print((char)('a' + col));
Serial.println(row + 1);
// Generate possible moves
int moveCount = 0;
int moves[28][2]; // up to 28 moves (maximum for a queen)
chessEngine->getPossibleMoves(board, row, col, moveCount, moves);
// Light up current square and possible move squares
boardDriver->setSquareLED(row, col, 0, 0, 0, 100); // Dimmer, but solid
// Highlight possible move squares (including captures)
for (int i = 0; i < moveCount; i++) {
int r = moves[i][0];
int c = moves[i][1];
// Different highlighting for empty squares vs capture squares
if (board[r][c] == ' ') {
boardDriver->setSquareLED(r, c, 0, 0, 0, 50); // Soft white for moves
} else {
boardDriver->setSquareLED(r, c, 255, 0, 0, 50); // Red tint for captures
}
}
boardDriver->showLEDs();
// Wait for piece placement - handle both normal moves and captures
int targetRow = -1, targetCol = -1;
bool piecePlaced = false;
bool captureInProgress = false;
// Wait for a piece placement on any square
while (!piecePlaced) {
boardDriver->readSensors();
// First check if the original piece was placed back
if (boardDriver->getSensorState(row, col)) {
targetRow = row;
targetCol = col;
piecePlaced = true;
break;
}
// Then check all squares for a regular move or capture initiation
for (int r2 = 0; r2 < 8; r2++) {
for (int c2 = 0; c2 < 8; c2++) {
// Skip the original square which was already checked
if (r2 == row && c2 == col) continue;
// Check if this would be a legal move
bool isLegalMove = false;
for (int i = 0; i < moveCount; i++) {
if (moves[i][0] == r2 && moves[i][1] == c2) {
isLegalMove = true;
break;
}
}
// If not a legal move, no need to check further
if (!isLegalMove) continue;
// For capture moves: detect when the target piece is removed
if (board[r2][c2] != ' ' && !boardDriver->getSensorState(r2, c2) && boardDriver->getSensorPrev(r2, c2)) {
Serial.print("Capture initiated at ");
Serial.print((char)('a' + c2));
Serial.println(r2 + 1);
// Store the target square and wait for the capturing piece to be placed there
targetRow = r2;
targetCol = c2;
captureInProgress = true;
// Flash the capture square to indicate waiting for piece placement
boardDriver->setSquareLED(r2, c2, 255, 0, 0, 100);
boardDriver->showLEDs();
// Wait for the capturing piece to be placed
bool capturePiecePlaced = false;
while (!capturePiecePlaced) {
boardDriver->readSensors();
if (boardDriver->getSensorState(r2, c2)) {
capturePiecePlaced = true;
piecePlaced = true;
break;
}
delay(50);
}
break;
}
// For normal non-capture moves: detect when a piece is placed on an empty square
else if (board[r2][c2] == ' ' && boardDriver->getSensorState(r2, c2) && !boardDriver->getSensorPrev(r2, c2)) {
targetRow = r2;
targetCol = c2;
piecePlaced = true;
break;
}
}
if (piecePlaced || captureInProgress) break;
}
delay(50);
}
// Check if piece is replaced in the original spot
if (targetRow == row && targetCol == col) {
Serial.println("Piece replaced in original spot");
// Blink once to confirm
boardDriver->setSquareLED(row, col, 0, 0, 0, 255);
boardDriver->showLEDs();
delay(200);
boardDriver->setSquareLED(row, col, 0, 0, 0, 100);
boardDriver->showLEDs();
// Clear all LED effects
boardDriver->clearAllLEDs();
continue; // Skip to next iteration
}
// Check if move is legal
bool legalMove = false;
bool isCapture = false;
for (int i = 0; i < moveCount; i++) {
if (moves[i][0] == targetRow && moves[i][1] == targetCol) {
legalMove = true;
// Check if this is a capture move
if (board[targetRow][targetCol] != ' ') {
isCapture = true;
}
break;
}
}
if (legalMove) {
Serial.print("Legal move to ");
Serial.print((char)('a' + targetCol));
Serial.println(targetRow + 1);
// Play capture animation if needed
if (board[targetRow][targetCol] != ' ') {
Serial.println("Performing capture animation");
boardDriver->captureAnimation();
}
// Process the move
processMove(row, col, targetRow, targetCol, piece);
// Check for pawn promotion
checkForPromotion(targetRow, targetCol, piece);
// Confirmation: Double blink destination square
for (int blink = 0; blink < 2; blink++) {
boardDriver->setSquareLED(targetRow, targetCol, 0, 0, 0, 255);
boardDriver->showLEDs();
delay(200);
boardDriver->setSquareLED(targetRow, targetCol, 0, 0, 0, 50);
boardDriver->showLEDs();
delay(200);
}
} else {
Serial.println("Illegal move, reverting");
}
// Clear any remaining LED effects
boardDriver->clearAllLEDs();
}
}
}
// Update previous sensor state
boardDriver->updateSensorPrev();
}
void ChessMoves::initializeBoard() {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
board[row][col] = INITIAL_BOARD[row][col];
}
}
}
void ChessMoves::waitForBoardSetup() {
Serial.println("Waiting for pieces to be placed...");
while (!boardDriver->checkInitialBoard(INITIAL_BOARD)) {
boardDriver->updateSetupDisplay(INITIAL_BOARD);
boardDriver->printBoardState(INITIAL_BOARD);
delay(500);
}
}
void ChessMoves::processMove(int fromRow, int fromCol, int toRow, int toCol, char piece) {
// Update board state
board[toRow][toCol] = piece;
board[fromRow][fromCol] = ' ';
}
void ChessMoves::checkForPromotion(int targetRow, int targetCol, char piece) {
if (chessEngine->isPawnPromotion(piece, targetRow)) {
char promotedPiece = chessEngine->getPromotedPiece(piece);
Serial.print((piece == 'P' ? "White" : "Black"));
Serial.print(" pawn promoted to Queen at ");
Serial.print((char)('a' + targetCol));
Serial.println((piece == 'P' ? "8" : "1"));
// Play promotion animation
boardDriver->promotionAnimation(targetCol);
// Promote to queen in board state
board[targetRow][targetCol] = promotedPiece;
// Handle the promotion process
handlePromotion(targetRow, targetCol, piece);
}
}
void ChessMoves::handlePromotion(int targetRow, int targetCol, char piece) {
Serial.println("Please replace the pawn with a queen piece");
// First wait for the pawn to be removed
while (boardDriver->getSensorState(targetRow, targetCol)) {
// Blink the square to indicate action needed
boardDriver->setSquareLED(targetRow, targetCol, 255, 215, 0, 50);
boardDriver->showLEDs();
delay(250);
boardDriver->setSquareLED(targetRow, targetCol, 0, 0, 0, 0);
boardDriver->showLEDs();
delay(250);
// Read sensors
boardDriver->readSensors();
}
Serial.println("Pawn removed, please place a queen");
// Then wait for the queen to be placed
while (!boardDriver->getSensorState(targetRow, targetCol)) {
// Blink the square to indicate action needed
boardDriver->setSquareLED(targetRow, targetCol, 255, 215, 0, 50);
boardDriver->showLEDs();
delay(250);
boardDriver->setSquareLED(targetRow, targetCol, 0, 0, 0, 0);
boardDriver->showLEDs();
delay(250);
// Read sensors
boardDriver->readSensors();
}
Serial.println("Queen placed, promotion complete");
// Final confirmation blink
for (int i = 0; i < 3; i++) {
boardDriver->setSquareLED(targetRow, targetCol, 255, 215, 0, 50);
boardDriver->showLEDs();
delay(100);
boardDriver->setSquareLED(targetRow, targetCol, 0, 0, 0, 0);
boardDriver->showLEDs();
delay(100);
}
}
bool ChessMoves::isActive() {
return true; // Simple implementation for now
}
void ChessMoves::reset() {
boardDriver->clearAllLEDs();
initializeBoard();
}