-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessAI.java
More file actions
309 lines (253 loc) · 12.1 KB
/
ChessAI.java
File metadata and controls
309 lines (253 loc) · 12.1 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
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Objects;
public class ChessAI {
private static final int SIZE = 8;
private static final int SQUARE_SIZE = 60;
private boolean inCheck = false;
public void setInCheck(boolean c){
this.inCheck = c;
}
public List<Move> findAllLegalMoves(ChessBoard chessBoard, String color) {
ArrayList<Move> allMoves = new ArrayList<>();
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
ChessPiece piece = chessBoard.getPiece(i, j);
if (piece != null && piece.getColor().equals(color)) {
addLegalMovesForPiece(piece, i, j, allMoves, chessBoard);
}
}
}
return allMoves;
}
public List<Move> findAllLegalMovesForWhite(ChessBoard chessBoard) {
ArrayList<Move> allWhiteMoves = new ArrayList<>();
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
ChessPiece piece = chessBoard.getPiece(i, j);
if (piece != null && piece.getColor().equals("White")) {
ArrayList<Move> potentialMoves = new ArrayList<>();
addLegalMovesForPiece(piece, i, j, potentialMoves, chessBoard);
}
}
}
return allWhiteMoves;
}
public void addLegalMovesForPiece(ChessPiece piece, int row, int col, List<Move> allMoves, ChessBoard chessBoard) {
// Example for a Pawn
if (piece instanceof Pawn) {
// Consider moving forward one and two squares, and capturing moves
for (int newRow = row - 1; newRow <= row + 2; newRow++) {
for (int newCol = col - 1; newCol <= col + 1; newCol++) {
if (newRow >= 0 && newRow < SIZE && newCol >= 0 && newCol < SIZE) {
boolean temp = false;
temp = piece.getHasMoved(piece);
if (piece.isValidMove(row, col, newRow, newCol, chessBoard.getBoard())) {
//System.out.println(piece.getHasMoved(piece));
piece.setHasMoved(temp);
//System.out.println(piece.getHasMoved(piece));
allMoves.add(new Move(piece, row, col, newRow, newCol));
}
}
}
}
}
if (piece instanceof Rook) {
// Loop through all squares in the same row and column
for (int i = 0; i < SIZE; i++) {
// Check column moves
if (i != col && piece.isValidMove(row, col, row, i, chessBoard.getBoard())) {
allMoves.add(new Move(piece, row, col, row, i));
}
// Check row moves
if (i != row && piece.isValidMove(row, col, i, col, chessBoard.getBoard())) {
allMoves.add(new Move(piece, row, col, i, col));
}
}
}
if (piece instanceof Knight){
int[][] knightMoves = {
{-2, -1}, {-2, 1}, // Upwards L-moves
{-1, -2}, {-1, 2}, // Leftward L-moves
{1, -2}, {1, 2}, // Rightward L-moves
{2, -1}, {2, 1} // Downward L-moves
};
for (int[] move : knightMoves) {
int newRow = row + move[0];
int newCol = col + move[1];
if (newRow >= 0 && newRow < SIZE && newCol >= 0 && newCol < SIZE) {
if (piece.isValidMove(row, col, newRow, newCol, chessBoard.getBoard())) {
allMoves.add(new Move(piece, row, col, newRow, newCol));
}
}
}
}
if (piece instanceof Bishop){
int[][] directions = {
{-1, -1}, {-1, 1},
{1, -1}, {1, 1}
};
for (int[] direction : directions) {
int currentRow = row;
int currentCol = col;
while (true) {
currentRow += direction[0];
currentCol += direction[1];
// Check if the new position is within bounds
if (currentRow < 0 || currentRow >= SIZE || currentCol < 0 || currentCol >= SIZE) {
break; // Break if out of bounds
}
// Use the isValidMove method to check if the move is legal
if (piece.isValidMove(row, col, currentRow, currentCol, chessBoard.getBoard())) {
allMoves.add(new Move(piece, row, col, currentRow, currentCol));
// If there is a piece at the destination, we must break, whether it's a capture or our own piece
if (chessBoard.getPiece(currentRow, currentCol) != null) {
break;
}
} else {
break; // If the move is not valid, break out of the loop
}
}
}
}
if (piece instanceof Queen){
int[][] queenMoves = {
{-1, -1}, {-1, 0}, {-1, 1}, // Up-Left, Up, Up-Right
{0, -1}, {0, 1}, // Left, Right
{1, -1}, {1, 0}, {1, 1} // Down-Left, Down, Down-Right
};
for (int[] move : queenMoves) {
int currentRow = row;
int currentCol = col;
while (true) {
currentRow += move[0];
currentCol += move[1];
// Check if the new position is within bounds
if (currentRow < 0 || currentRow >= SIZE || currentCol < 0 || currentCol >= SIZE) {
break; // Break if out of bounds
}
// Use the isValidMove method to check if the move is legal
if (piece.isValidMove(row, col, currentRow, currentCol, chessBoard.getBoard())) {
allMoves.add(new Move(piece, row, col, currentRow, currentCol));
// If there is a piece at the destination, we must break, whether it's a capture or our own piece
if (chessBoard.getPiece(currentRow, currentCol) != null) {
break;
}
} else {
break; // If the move is not valid, break out of the loop
}
}
}
}
if (piece instanceof King){
int[][] kingMoves = {
{-1, -1}, {-1, 0}, {-1, 1}, // Up-Left, Up, Up-Right
{0, -1}, {0, 1}, // Left, Right
{1, -1}, {1, 0}, {1, 1} // Down-Left, Down, Down-Right
};
for (int[] move : kingMoves) {
int newRow = row + move[0];
int newCol = col + move[1];
// Ensure the new position is on the board
if (newRow >= 0 && newRow < SIZE && newCol >= 0 && newCol < SIZE) {
if (piece.isValidMove(row, col, newRow, newCol, chessBoard.getBoard())) {
// If the destination is empty or contains an opponent's piece, add the move
if (chessBoard.getPiece(newRow, newCol) == null ||
!chessBoard.getPiece(newRow, newCol).getColor().equals(piece.getColor())) {
allMoves.add(new Move(piece, row, col, newRow, newCol));
}
}
}
}
}
// Add similar logic for other types of pieces (Rooks, Knights, etc.)
}
public void isValidForSinglePiece(ChessPiece piece, int r, int c,ChessBoard chessBoard){
}
public Move selectBestMove(List<Move> moves, ChessBoard chessBoard) {
Move bestMove = null;
int highestScore = 0;
for (Move move : moves) {
if (move.capturesOpponentPiece(move.getNewX(), move.getNewY(), chessBoard)) {
ChessPiece targetPiece = chessBoard.getPiece(move.getNewY(), move.getNewX());
ChessPiece movingPiece = chessBoard.getPiece(move.getInitY(), move.getInitX());
if (targetPiece != null && !targetPiece.getColor().equals(movingPiece.getColor())) {
int score = targetPiece.getScore();
System.out.println("Evaluating move " + move + " with score " + score);
if (score > highestScore) {
highestScore = score;
bestMove = move;
}
}
}
}
if (bestMove != null) {
return bestMove;
} else {
// If no capturing move is found or all are same color, select a random move
return moves.get(new Random().nextInt(moves.size()));
}
}
public void executeMove(Move move, ChessBoard chessBoard) {
int initX = move.getInitX();
int initY = move.getInitY();
int newX = move.getNewX();
int newY = move.getNewY();
ChessPiece targetSquare = chessBoard.getPiece(newY, newX);
ChessPiece currentSquare = chessBoard.getPiece(initY, initX);
// Check if the target square has an opponent's piece
if (targetSquare != null && !targetSquare.getColor().equals(currentSquare.getColor())) {
System.out.println(currentSquare.getType(currentSquare) + " " + currentSquare.getColor() + " captures " + targetSquare.getType(targetSquare) + " at " + move.toBoardCoordinate(newX,newY));
}
// Update the board with the move
chessBoard.movePiece(initY, initX, newY, newX);
//String pos = squareToLetter(initX, initY);
String posF = squareToLetter(newY, newX);
}
public Move makeMove(ChessBoard chessBoard) {
List<Move> possibleMoves = findAllLegalMoves(chessBoard, "Black");
if (!inCheck){
if (!possibleMoves.isEmpty()) {
return selectBestMove(possibleMoves, chessBoard);
} else {
System.out.println("No legal moves found");
// No legal moves found - could be stalemate or checkmate
// Add logic here to handle endgame conditions
return null;
}
}
else {
System.out.println("Checking");
List<Move> escapeMoves = new ArrayList<>();
for (Move move : possibleMoves) {
// Simulate each move
ChessPiece targetPiece = chessBoard.getPiece(move.getNewY(), move.getNewX());
ChessPiece movingPiece = chessBoard.getPiece(move.getInitY(), move.getInitX()); // Save the moving piece
chessBoard.simulateMove(move.getInitY(),move.getInitX(), move.getNewY(),move.getNewX());
if (!GameRules.isKingInCheck(chessBoard, "Black")) {
escapeMoves.add(move);
}
chessBoard.undoSimulatedMove(move.getInitY(),move.getInitX(), move.getNewY(),move.getNewX(),movingPiece, targetPiece);
// Undo the move or reset the board to its original state
}
if (!escapeMoves.isEmpty()) {
return selectBestMove(escapeMoves, chessBoard);
} else {
System.out.println("No moves to escape check - checkmate");
// Handle checkmate
System.out.println("checkmate");
return null;
}
}
}
public List<Move> returnAllLegalMoves(ChessBoard chessBoard) {
return findAllLegalMoves(chessBoard, "Black");
}
public String squareToLetter(int col, int row) {
String columnLetter = String.valueOf((char)('A' + col));
int chessRow = 8 - row; // Convert array row index to chess row number
return columnLetter + chessRow;
}
}