forked from Concept-Bytes/Open-Chess
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchess_engine.cpp
More file actions
251 lines (211 loc) · 8.65 KB
/
chess_engine.cpp
File metadata and controls
251 lines (211 loc) · 8.65 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
#include "chess_engine.h"
#include <Arduino.h>
// ---------------------------
// ChessEngine Implementation
// ---------------------------
ChessEngine::ChessEngine() {
// Constructor - nothing to initialize for now
}
// Main move generation function
void ChessEngine::getPossibleMoves(const char board[8][8], int row, int col, int &moveCount, int moves[][2]) {
moveCount = 0;
char piece = board[row][col];
if (piece == ' ') return; // Empty square
char pieceColor = getPieceColor(piece);
// Convert to uppercase for easier comparison
piece = (piece >= 'a' && piece <= 'z') ? piece - 32 : piece;
switch(piece) {
case 'P': // Pawn
addPawnMoves(board, row, col, pieceColor, moveCount, moves);
break;
case 'R': // Rook
addRookMoves(board, row, col, pieceColor, moveCount, moves);
break;
case 'N': // Knight
addKnightMoves(board, row, col, pieceColor, moveCount, moves);
break;
case 'B': // Bishop
addBishopMoves(board, row, col, pieceColor, moveCount, moves);
break;
case 'Q': // Queen
addQueenMoves(board, row, col, pieceColor, moveCount, moves);
break;
case 'K': // King
addKingMoves(board, row, col, pieceColor, moveCount, moves);
break;
}
}
// Pawn move generation
void ChessEngine::addPawnMoves(const char board[8][8], int row, int col, char pieceColor, int &moveCount, int moves[][2]) {
int direction = (pieceColor == 'w') ? 1 : -1;
// One square forward
if (isValidSquare(row + direction, col) && isSquareEmpty(board, row + direction, col)) {
moves[moveCount][0] = row + direction;
moves[moveCount][1] = col;
moveCount++;
// Initial two-square move
if ((pieceColor == 'w' && row == 1) || (pieceColor == 'b' && row == 6)) {
if (isSquareEmpty(board, row + 2*direction, col)) {
moves[moveCount][0] = row + 2*direction;
moves[moveCount][1] = col;
moveCount++;
}
}
}
// Diagonal captures
int captureColumns[] = {col-1, col+1};
for (int i = 0; i < 2; i++) {
int captureRow = row + direction;
int captureCol = captureColumns[i];
if (isValidSquare(captureRow, captureCol) &&
isSquareOccupiedByOpponent(board, captureRow, captureCol, pieceColor)) {
moves[moveCount][0] = captureRow;
moves[moveCount][1] = captureCol;
moveCount++;
}
}
}
// Rook move generation
void ChessEngine::addRookMoves(const char board[8][8], int row, int col, char pieceColor, int &moveCount, int moves[][2]) {
int directions[4][2] = {{1,0}, {-1,0}, {0,1}, {0,-1}};
for (int d = 0; d < 4; d++) {
for (int step = 1; step < 8; step++) {
int newRow = row + step * directions[d][0];
int newCol = col + step * directions[d][1];
if (!isValidSquare(newRow, newCol)) break;
if (isSquareEmpty(board, newRow, newCol)) {
moves[moveCount][0] = newRow;
moves[moveCount][1] = newCol;
moveCount++;
} else {
// Check if it's a capturable piece
if (isSquareOccupiedByOpponent(board, newRow, newCol, pieceColor)) {
moves[moveCount][0] = newRow;
moves[moveCount][1] = newCol;
moveCount++;
}
break; // Can't move past any piece
}
}
}
}
// Knight move generation
void ChessEngine::addKnightMoves(const char board[8][8], int row, int col, char pieceColor, int &moveCount, int moves[][2]) {
int knightMoves[8][2] = {{2,1}, {1,2}, {-1,2}, {-2,1},
{-2,-1}, {-1,-2}, {1,-2}, {2,-1}};
for (int i = 0; i < 8; i++) {
int newRow = row + knightMoves[i][0];
int newCol = col + knightMoves[i][1];
if (isValidSquare(newRow, newCol)) {
if (isSquareEmpty(board, newRow, newCol) ||
isSquareOccupiedByOpponent(board, newRow, newCol, pieceColor)) {
moves[moveCount][0] = newRow;
moves[moveCount][1] = newCol;
moveCount++;
}
}
}
}
// Bishop move generation
void ChessEngine::addBishopMoves(const char board[8][8], int row, int col, char pieceColor, int &moveCount, int moves[][2]) {
int directions[4][2] = {{1,1}, {1,-1}, {-1,1}, {-1,-1}};
for (int d = 0; d < 4; d++) {
for (int step = 1; step < 8; step++) {
int newRow = row + step * directions[d][0];
int newCol = col + step * directions[d][1];
if (!isValidSquare(newRow, newCol)) break;
if (isSquareEmpty(board, newRow, newCol)) {
moves[moveCount][0] = newRow;
moves[moveCount][1] = newCol;
moveCount++;
} else {
// Check if it's a capturable piece
if (isSquareOccupiedByOpponent(board, newRow, newCol, pieceColor)) {
moves[moveCount][0] = newRow;
moves[moveCount][1] = newCol;
moveCount++;
}
break; // Can't move past any piece
}
}
}
}
// Queen move generation (combination of rook and bishop)
void ChessEngine::addQueenMoves(const char board[8][8], int row, int col, char pieceColor, int &moveCount, int moves[][2]) {
addRookMoves(board, row, col, pieceColor, moveCount, moves);
addBishopMoves(board, row, col, pieceColor, moveCount, moves);
}
// King move generation
void ChessEngine::addKingMoves(const char board[8][8], int row, int col, char pieceColor, int &moveCount, int moves[][2]) {
int kingMoves[8][2] = {{1,0}, {-1,0}, {0,1}, {0,-1},
{1,1}, {1,-1}, {-1,1}, {-1,-1}};
for (int i = 0; i < 8; i++) {
int newRow = row + kingMoves[i][0];
int newCol = col + kingMoves[i][1];
if (isValidSquare(newRow, newCol)) {
if (isSquareEmpty(board, newRow, newCol) ||
isSquareOccupiedByOpponent(board, newRow, newCol, pieceColor)) {
moves[moveCount][0] = newRow;
moves[moveCount][1] = newCol;
moveCount++;
}
}
}
}
// Helper function to check if a square is occupied by an opponent piece
bool ChessEngine::isSquareOccupiedByOpponent(const char board[8][8], int row, int col, char pieceColor) {
char targetPiece = board[row][col];
if (targetPiece == ' ') return false;
char targetColor = getPieceColor(targetPiece);
return targetColor != pieceColor;
}
// Helper function to check if a square is empty
bool ChessEngine::isSquareEmpty(const char board[8][8], int row, int col) {
return board[row][col] == ' ';
}
// Helper function to check if coordinates are within board bounds
bool ChessEngine::isValidSquare(int row, int col) {
return row >= 0 && row < 8 && col >= 0 && col < 8;
}
// Helper function to get piece color
char ChessEngine::getPieceColor(char piece) {
return (piece >= 'a' && piece <= 'z') ? 'b' : 'w';
}
// Move validation
bool ChessEngine::isValidMove(const char board[8][8], int fromRow, int fromCol, int toRow, int toCol) {
int moveCount = 0;
int moves[28][2]; // Maximum possible moves for a queen
getPossibleMoves(board, fromRow, fromCol, moveCount, moves);
for (int i = 0; i < moveCount; i++) {
if (moves[i][0] == toRow && moves[i][1] == toCol) {
return true;
}
}
return false;
}
// Check if a pawn move results in promotion
bool ChessEngine::isPawnPromotion(char piece, int targetRow) {
if (piece == 'P' && targetRow == 7) return true; // White pawn reaches 8th rank
if (piece == 'p' && targetRow == 0) return true; // Black pawn reaches 1st rank
return false;
}
// Get the promoted piece (always queen for now)
char ChessEngine::getPromotedPiece(char piece) {
return (piece == 'P') ? 'Q' : 'q';
}
// Utility function to print a move in readable format
void ChessEngine::printMove(int fromRow, int fromCol, int toRow, int toCol) {
Serial.print((char)('a' + fromCol));
Serial.print(fromRow + 1);
Serial.print(" to ");
Serial.print((char)('a' + toCol));
Serial.println(toRow + 1);
}
// Convert algebraic notation file (a-h) to column index (0-7)
char ChessEngine::algebraicToCol(char file) {
return file - 'a';
}
// Convert algebraic notation rank (1-8) to row index (0-7)
int ChessEngine::algebraicToRow(int rank) {
return rank - 1;
}