forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessGameEngine.java
More file actions
191 lines (158 loc) · 6.96 KB
/
ChessGameEngine.java
File metadata and controls
191 lines (158 loc) · 6.96 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
import java.util.*;
public class ChessGameEngine {
private char[][] board;
private boolean isWhiteTurn;
private boolean[] castlingRights; // [whiteKingSide, whiteQueenSide, blackKingSide, blackQueenSide]
private int[] enPassantSquare; // [row, col] of square where en passant capture is possible
public ChessGameEngine() {
initializeBoard();
isWhiteTurn = true;
castlingRights = new boolean[]{true, true, true, true};
enPassantSquare = null;
}
private void initializeBoard() {
board = new char[8][8];
// Initialize pieces
// Black pieces
board[0] = new char[]{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'};
Arrays.fill(board[1], 'p');
// Empty squares
for (int i = 2; i < 6; i++) {
Arrays.fill(board[i], ' ');
}
// White pieces
Arrays.fill(board[6], 'P');
board[7] = new char[]{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'};
}
public boolean makeMove(int startRow, int startCol, int endRow, int endCol) {
if (!isValidMove(startRow, startCol, endRow, endCol)) {
return false;
}
// Store the move
char piece = board[startRow][startCol];
char capturedPiece = board[endRow][endCol];
// Make the move
board[endRow][endCol] = piece;
board[startRow][startCol] = ' ';
// Handle special moves
handleSpecialMoves(piece, startRow, startCol, endRow, endCol);
// Check if the move puts the current player in check
if (isInCheck(isWhiteTurn)) {
// Undo the move
board[startRow][startCol] = piece;
board[endRow][endCol] = capturedPiece;
return false;
}
// Update game state
isWhiteTurn = !isWhiteTurn;
return true;
}
private boolean isValidMove(int startRow, int startCol, int endRow, int endCol) {
// Basic boundary checks
if (!isValidPosition(startRow, startCol) || !isValidPosition(endRow, endCol)) {
return false;
}
char piece = board[startRow][startCol];
boolean isWhitePiece = Character.isUpperCase(piece);
// Check if it's the correct player's turn
if (isWhiteTurn != isWhitePiece) {
return false;
}
// Implement piece-specific move validation
return validatePieceMove(piece, startRow, startCol, endRow, endCol);
}
private boolean validatePieceMove(char piece, int startRow, int startCol, int endRow, int endCol) {
// Implement specific validation for each piece type
switch (Character.toLowerCase(piece)) {
case 'p': return validatePawnMove(piece, startRow, startCol, endRow, endCol);
case 'r': return validateRookMove(startRow, startCol, endRow, endCol);
case 'n': return validateKnightMove(startRow, startCol, endRow, endCol);
case 'b': return validateBishopMove(startRow, startCol, endRow, endCol);
case 'q': return validateQueenMove(startRow, startCol, endRow, endCol);
case 'k': return validateKingMove(piece, startRow, startCol, endRow, endCol);
default: return false;
}
}
private void handleSpecialMoves(char piece, int startRow, int startCol, int endRow, int endCol) {
// Handle castling
if (Character.toLowerCase(piece) == 'k' && Math.abs(endCol - startCol) == 2) {
handleCastling(startRow, startCol, endRow, endCol);
}
// Handle pawn promotion
if (Character.toLowerCase(piece) == 'p' && (endRow == 0 || endRow == 7)) {
handlePromotion(endRow, endCol);
}
// Handle en passant
if (Character.toLowerCase(piece) == 'p' && startCol != endCol && board[endRow][endCol] == ' ') {
handleEnPassant(startRow, endRow, endCol);
}
}
private boolean isInCheck(boolean isWhiteKing) {
// Find king's position
int[] kingPos = findKing(isWhiteKing);
if (kingPos == null) return false;
// Check if any opponent's piece can capture the king
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
char piece = board[i][j];
if (piece != ' ' && isWhitePiece(piece) != isWhiteKing) {
if (isValidMove(i, j, kingPos[0], kingPos[1])) {
return true;
}
}
}
}
return false;
}
private boolean isValidPosition(int row, int col) {
return row >= 0 && row < 8 && col >= 0 && col < 8;
}
private boolean isWhitePiece(char piece) {
return Character.isUpperCase(piece);
}
// Helper methods for move validation and special moves
private boolean validatePawnMove(char piece, int startRow, int startCol, int endRow, int endCol) {
return PieceValidation.validatePawnMove(board, piece, startRow, startCol, endRow, endCol);
}
private boolean validateRookMove(int startRow, int startCol, int endRow, int endCol) {
return PieceValidation.validateRookMove(board, startRow, startCol, endRow, endCol);
}
private boolean validateKnightMove(int startRow, int startCol, int endRow, int endCol) {
return PieceValidation.validateKnightMove(board, startRow, startCol, endRow, endCol);
}
private boolean validateBishopMove(int startRow, int startCol, int endRow, int endCol) {
return PieceValidation.validateBishopMove(board, startRow, startCol, endRow, endCol);
}
private boolean validateQueenMove(int startRow, int startCol, int endRow, int endCol) {
return PieceValidation.validateQueenMove(board, startRow, startCol, endRow, endCol);
}
private boolean validateKingMove(char piece, int startRow, int startCol, int endRow, int endCol) {
return PieceValidation.validateKingMove(board, startRow, startCol, endRow, endCol);
}
private void handleCastling(int startRow, int startCol, int endRow, int endCol) {
SpecialMoves.handleCastling(board, startRow, startCol, endRow, endCol);
}
private void handlePromotion(int endRow, int endCol) {
SpecialMoves.handlePromotion(board, endRow, endCol);
}
private void handleEnPassant(int startRow, int endRow, int endCol) {
SpecialMoves.handleEnPassant(board, startRow, endRow, endCol);
}
private int[] findKing(boolean isWhiteKing) {
char kingChar = isWhiteKing ? 'K' : 'k';
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == kingChar) {
return new int[]{i, j};
}
}
}
return null;
}
public char[][] getBoard() {
return board;
}
public boolean isWhiteTurn() {
return isWhiteTurn;
}
}