-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChess.java
More file actions
258 lines (233 loc) · 7.53 KB
/
Chess.java
File metadata and controls
258 lines (233 loc) · 7.53 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
import java.util.ArrayList;
public class Chess {
private Square[][] board;
private Player WHITE;
private Player BLACK;
private Piece targetPiece;
private boolean winner;
private ArrayList<Piece> takenPieces;
private int currentKingX = 0;
private int currentKingY = 0;
public Chess() {
WHITE = new Player(true);
BLACK = new Player(false);
WHITE.changeTurn();
takenPieces = new ArrayList<Piece>();
initialize();
}
public void addPieces(Square[][] chessboard) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (row == 0) {
if (col == 0 || col == 7) {
board[row][col] = new Square(new Rook(BLACK, "Rook"));
} else if (col == 1 || col == 6) {
board[row][col] = new Square(new Knight(BLACK, "Knight"));
} else if (col == 2 || col == 5) {
board[row][col] = new Square(new Bishop(BLACK, "Bishop"));
} else if (col == 3) {
board[row][col] = new Square(new Queen(BLACK, "Queen"));
} else if (col == 4) {
board[row][col] = new Square(new King(BLACK, "King"));
}
} else if (row == 1) {
board[row][col] = new Square(new Pawn(BLACK, "Pawn"));
} else if (row == 6) {
board[row][col] = new Square(new Pawn(WHITE, "Pawn"));
} else if (row == 7) {
if (col == 0 || col == 7) {
board[row][col] = new Square(new Rook(WHITE, "Rook"));
} else if (col == 1 || col == 6) {
board[row][col] = new Square(new Knight(WHITE, "Knight"));
} else if (col == 2 || col == 5) {
board[row][col] = new Square(new Bishop(WHITE, "Bishop"));
} else if (col == 3) {
board[row][col] = new Square(new Queen(WHITE, "Queen"));
} else if (col == 4) {
board[row][col] = new Square(new King(WHITE, "King"));
}
} else {
board[row][col] = new Square(null);
}
}
}
}
public Square[][] move(Player player, int prevX, int prevY, int finX, int finY) {
targetPiece = board[prevX][prevY].getPiece();
Square startSquare = board[prevX][prevY];
Square targetSquare = board[finX][finY];
Piece potentialOpponentPiece = targetSquare.getPiece();
if (potentialOpponentPiece != null) {
targetSquare.removePiece();
takenPieces.add(potentialOpponentPiece);
targetSquare.placePiece(targetPiece);
} else {
targetSquare.placePiece(targetPiece);
}
startSquare.removePiece();
return board;
}
public void initialize() {
board = new Square[8][8];
addPieces(board);
}
public Square[][] getBoard() {
return board;
}
public void switchTurns() {
WHITE.changeTurn();
BLACK.changeTurn();
}
public void playerWins() {
if (winner) {
System.out.println("WHITE WINS!");
} if (!winner) {
System.out.println("BLACK WINS!");
}
}
public boolean isKingChecked(Square[][] board, Player player) {
findKing(board, player);
Piece currentKing = board[currentKingX][currentKingY].getPiece();
System.out.println("(" + currentKingX + ", " + currentKingY + ")");
if (currentKing == null) {
System.out.println("KING IS NULL");
return true;
}
if (!"King".equals(currentKing.getType())) {
return false;
}
Player currentPlayer = currentKing.getPlayer();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j].getPiece() == null) {
continue;
} else if (currentPlayer.isWhite == board[i][j].getPiece()
.getPlayer().isBlack) {
Piece piece = board[i][j].getPiece();
if (piece.isValid(board, i, j, currentKingX, currentKingY)) {
System.out.println(piece.getType() + " KING STILL IN CHECK!");
return true;
}
}
}
}
System.out.println("KING NOT IN CHECK ANYMORE");
return false;
}
private void findKing(Square[][] board, Player player) {
if (player.isBlack) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j].getPiece() == null) {
continue;
}
if (board[i][j].getPiece().getPlayer().isBlack) {
if ("King".equals(board[i][j].getPiece().getType())) {
currentKingX = i;
currentKingY = j;
}
}
}
}
} else {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j].getPiece() == null) {
continue;
}
if (board[i][j].getPiece().getPlayer().isWhite) {
if ("King".equals(board[i][j].getPiece().getType())) {
currentKingX = i;
currentKingY = j;
}
}
}
}
}
}
public Square[][] getDummyBoard(int startRow,
int startCol, int finalRow, int finalCol, Square[][] gameBoard) {
Square[][] board = new Square[8][8];
Player WHITE = new Player(true);
Player BLACK = new Player(false);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (gameBoard[i][j].getPiece() == null) {
board[i][j] = new Square(null);
} else if (gameBoard[i][j].getPiece().getPlayer().isWhite) {
if ("Pawn".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Pawn(WHITE, "Pawn"));
} else if ("Rook".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Rook(WHITE, "Rook"));
} else if ("Knight".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Knight(WHITE, "Knight"));
} else if ("Bishop".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Bishop(WHITE, "Bishop"));
} else if ("Queen".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Queen(WHITE, "Queen"));
} else if ("King".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new King(WHITE, "King"));
}
} else {
if ("Pawn".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Pawn(BLACK, "Pawn"));
} else if ("Rook".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Rook(BLACK, "Rook"));
} else if ("Knight".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Knight(BLACK, "Knight"));
} else if ("Bishop".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Bishop(BLACK, "Bishop"));
} else if ("Queen".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Queen(BLACK, "Queen"));
} else if ("King".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new King(BLACK, "King"));
}
}
}
}
Piece targetPiece = board[startRow][startCol].getPiece();
Square startSquare = board[startRow][startCol];
Square targetSquare = board[finalRow][finalCol];
Piece potentialOpponentPiece = targetSquare.getPiece();
if (potentialOpponentPiece != null) {
targetSquare.removePiece();
targetSquare.placePiece(targetPiece);
} else {
targetSquare.placePiece(targetPiece);
}
startSquare.removePiece();
return board;
}
public boolean checkCheckMate(Player player, Square[][] board) {
// Get all pieces of current color pieces
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j].getPiece() == null) {
continue;
}
if (board[i][j].getPiece().getPlayer().isWhite == player.isWhite) {
for (int a = 0; a < 8; a++) {
for (int b = 0; b < 8; b++) {
if (i == a && j == b) {
continue;
}
if (board[i][j].getPiece().isValid(board, i, j, a, b)) {
if (!isKingChecked(getDummyBoard(i, j, a, b, board), player)) {
return false;
}
}
}
}
}
}
}
return true;
}
public void setWinner(String color) {
if ("white".equals(color)) {
winner = true;
} else {
winner = false;
}
}
}