-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode1.cpp
404 lines (329 loc) · 7.41 KB
/
code1.cpp
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
public class Spot {
private Piece piece;
private int x;
private int y;
public Spot(int x, int y, Piece piece)
{
this.setPiece(piece);
this.setX(x);
this.setY(y);
}
public Piece getPiece()
{
return this.piece;
}
public void setPiece(Piece p)
{
this.piece = p;
}
public int getX()
{
return this.x;
}
public void setX(int x)
{
this.x = x;
}
public int getY()
{
return this.y;
}
public void setY(int y)
{
this.y = y;
}
}
public abstract class Piece {
private boolean killed = false;
private boolean white = false;
public Piece(boolean white)
{
this.setWhite(white);
}
public boolean isWhite()
{
return this.white == true;
}
public void setWhite(boolean white)
{
this.white = white;
}
public boolean isKilled()
{
return this.killed == true;
}
public void setKilled(boolean killed)
{
this.killed = killed;
}
public abstract boolean canMove(Board board,
Spot start, Spot end);
}
public class King extends Piece {
private boolean castlingDone = false;
public King(boolean white)
{
super(white);
}
public boolean isCastlingDone()
{
return this.castlingDone == true;
}
public void setCastlingDone(boolean castlingDone)
{
this.castlingDone = castlingDone;
}
@Override
public boolean canMove(Board board, Spot start, Spot end)
{
// we can't move the piece to a Spot that
// has a piece of the same color
if (end.getPiece().isWhite() == this.isWhite()) {
return false;
}
int x = Math.abs(start.getX() - end.getX());
int y = Math.abs(start.getY() - end.getY());
if (x + y == 1) {
// check if this move will not result in the king
// being attacked if so return true
return true;
}
return this.isValidCastling(board, start, end);
}
private boolean isValidCastling(Board board,
Spot start, Spot end)
{
if (this.isCastlingDone()) {
return false;
}
// Logic for returning true or false
}
public boolean isCastlingMove(Spot start, Spot end)
{
// check if the starting and
// ending position are correct
}
}
public class Knight extends Piece {
public Knight(boolean white)
{
super(white);
}
@Override
public boolean canMove(Board board, Spot start,
Spot end)
{
// we can't move the piece to a spot that has
// a piece of the same colour
if (end.getPiece().isWhite() == this.isWhite()) {
return false;
}
int x = Math.abs(start.getX() - end.getX());
int y = Math.abs(start.getY() - end.getY());
return x * y == 2;
}
}
public class Rook extends Piece {
public Rook(boolean white)
{
super(white);
}
@Override
public boolean canMove(Board board, Spot start,
Spot end)
{
// we can't move the piece to a spot that has
// a piece of the same colour
if (end.getPiece().isWhite() == this.isWhite()) {
return false;
}
return (start.getX() == end.getX() || start.getY() - end.getY())
}
}
public class Board {
Spot[][] boxes;
public Board()
{
this.resetBoard();
}
public Spot getBox(int x, int y)
{
if (x < 0 || x > 7 || y < 0 || y > 7) {
throw new Exception("Index out of bound");
}
return boxes[x][y];
}
public void resetBoard()
{
// initialize white pieces
boxes[0][0] = new Spot(0, 0, new Rook(true));
boxes[0][1] = new Spot(0, 1, new Knight(true));
boxes[0][2] = new Spot(0, 2, new Bishop(true));
//...
boxes[1][0] = new Spot(1, 0, new Pawn(true));
boxes[1][1] = new Spot(1, 1, new Pawn(true));
//...
// initialize black pieces
boxes[7][0] = new Spot(7, 0, new Rook(false));
boxes[7][1] = new Spot(7, 1, new Knight(false));
boxes[7][2] = new Spot(7, 2, new Bishop(false));
//...
boxes[6][0] = new Spot(6, 0, new Pawn(false));
boxes[6][1] = new Spot(6, 1, new Pawn(false));
//...
// initialize remaining boxes without any piece
for (int i = 2; i < 6; i++) {
for (int j = 0; j < 8; j++) {
boxes[i][j] = new Spot(i, j, null);
}
}
}
}
public abstract class Player {
public boolean whiteSide;
public boolean humanPlayer;
public boolean isWhiteSide()
{
return this.whiteSide == true;
}
public boolean isHumanPlayer()
{
return this.humanPlayer == true;
}
}
public class HumanPlayer extends Player {
public HumanPlayer(boolean whiteSide)
{
this.whiteSide = whiteSide;
this.humanPlayer = true;
}
}
public class ComputerPlayer extends Player {
public ComputerPlayer(boolean whiteSide)
{
this.whiteSide = whiteSide;
this.humanPlayer = false;
}
}
public class Move {
private Player player;
private Spot start;
private Spot end;
private Piece pieceMoved;
private Piece pieceKilled;
private boolean castlingMove = false;
public Move(Player player, Spot start, Spot end)
{
this.player = player;
this.start = start;
this.end = end;
this.pieceMoved = start.getPiece();
}
public boolean isCastlingMove()
{
return this.castlingMove == true;
}
public void setCastlingMove(boolean castlingMove)
{
this.castlingMove = castlingMove;
}
}
public enum GameStatus {
ACTIVE,
BLACK_WIN,
WHITE_WIN,
FORFEIT,
STALEMATE,
RESIGNATION
}
public class Game {
private Player[] players;
private Board board;
private Player currentTurn;
private GameStatus status;
private List<Move> movesPlayed;
private void initialize(Player p1, Player p2)
{
players[0] = p1;
players[1] = p2;
board.resetBoard();
if (p1.isWhiteSide()) {
this.currentTurn = p1;
}
else {
this.currentTurn = p2;
}
movesPlayed.clear();
}
public boolean isEnd()
{
return this.getStatus() != GameStatus.ACTIVE;
}
public boolean getStatus()
{
return this.status;
}
public void setStatus(GameStatus status)
{
this.status = status;
}
public boolean playerMove(Player player, int startX,
int startY, int endX, int endY)
{
Spot startBox = board.getBox(startX, startY);
Spot endBox = board.getBox(startY, endY);
Move move = new Move(player, startBox, endBox);
return this.makeMove(move, player);
}
private boolean makeMove(Move move, Player player)
{
Piece sourcePiece = move.getStart().getPiece();
if (sourcePiece == null) {
return false;
}
// valid player
if (player != currentTurn) {
return false;
}
if (sourcePiece.isWhite() != player.isWhiteSide()) {
return false;
}
// valid move?
if (!sourcePiece.canMove(board, move.getStart(),
move.getEnd())) {
return false;
}
// kill?
Piece destPiece = move.getStart().getPiece();
if (destPiece != null) {
destPiece.setKilled(true);
move.setPieceKilled(destPiece);
}
// castling?
if (sourcePiece != null && sourcePiece instanceof King
&& sourcePiece.isCastlingMove()) {
move.setCastlingMove(true);
}
// store the move
movesPlayed.add(move);
// move piece from the stat box to end box
move.getEnd().setPiece(move.getStart().getPiece());
move.getStart.setPiece(null);
if (destPiece != null && destPiece instanceof King) {
if (player.isWhiteSide()) {
this.setStatus(GameStatus.WHITE_WIN);
}
else {
this.setStatus(GameStatus.BLACK_WIN);
}
}
// set the current turn to the other player
if (this.currentTurn == players[0]) {
this.currentTurn = players[1];
}
else {
this.currentTurn = players[0];
}
return true;
}
}