- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessBoard.java
More file actions
Latest commit
180 lines (151 loc) · 5.3 KB
/
Copy pathChessBoard.java
File metadata and controls
180 lines (151 loc) · 5.3 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
packagechess;
importjava.util.ArrayList;
importjava.util.Iterator;
importjavafx.collections.*;
publicclassChessBoard {
privateSquareboard[][];
privateChessColouractiveColour;
privateintfullMove;
privateArrayList<Piece> takenPieces;
privateArrayList<Piece> whiteTakenPieces = newArrayList<Piece>();
privateArrayList<Piece> blackTakenPieces = newArrayList<Piece>();
privateObservableList<Piece> obsWhiteTakenPieces = FXCollections.observableList(whiteTakenPieces);
privateObservableList<Piece> obsBlackTakenPieces = FXCollections.observableList(blackTakenPieces);
//Create 2-dimensional gameboard
publicChessBoard() {
board = newSquare[8][8];
for (intc = 0; c < 8; c++)
for (intr = 0; r < 8; r++)
board[c][r] = newSquare(newCoordinate(c, r));
reset();
activeColour = ChessColour.WHITE;
fullMove = 1;
takenPieces = newArrayList<Piece>();
}
//Create 2-dimensional gameboard
publicChessBoard(Coordinatepositions[], Piecepieces[]) throwsIllegalArgumentException {
if (positions.length != pieces.length)
thrownewIllegalArgumentException("The list of positions must correspond to the list of pieces");
board = newSquare[8][8];
for (intr = 0; r < 8; r++)
for (intc = 0; c < 8; c++)
board[r][c] = newSquare(newCoordinate(r, c));
for (inti = 0; i < positions.length; i++) {
board[positions[i].getColumnNumber()][positions[i].getRowNumber()].addPiece(pieces[i]);
}
activeColour = ChessColour.WHITE;
fullMove = 1;
}
//Method initializes the chessboard with the locations of all pieces
privatevoidreset() {
// White rows
board[0][0].addPiece(newRook(ChessColour.WHITE));
board[7][0].addPiece(newRook(ChessColour.WHITE));
board[1][0].addPiece(newKnight(ChessColour.WHITE));
board[6][0].addPiece(newKnight(ChessColour.WHITE));
board[2][0].addPiece(newBishop(ChessColour.WHITE));
board[5][0].addPiece(newBishop(ChessColour.WHITE));
board[3][0].addPiece(newQueen(ChessColour.WHITE));
board[4][0].addPiece(newKing(ChessColour.WHITE));
for (intc = 0; c < 8; c++)
board[c][1].addPiece(newPawn(ChessColour.WHITE));
// Black rows
board[0][7].addPiece(newRook(ChessColour.BLACK));
board[7][7].addPiece(newRook(ChessColour.BLACK));
board[1][7].addPiece(newKnight(ChessColour.BLACK));
board[6][7].addPiece(newKnight(ChessColour.BLACK));
board[2][7].addPiece(newBishop(ChessColour.BLACK));
board[5][7].addPiece(newBishop(ChessColour.BLACK));
board[3][7].addPiece(newQueen(ChessColour.BLACK));
board[4][7].addPiece(newKing(ChessColour.BLACK));
for (intc = 0; c < 8; c++)
board[c][6].addPiece(newPawn(ChessColour.BLACK));
// Middle rows
Piecep;
for (intc = 0; c < 8; c++)
for (intr = 2; r < 6; r++)
p = board[c][r].deletePiece();
}
//Method returns square on chessboard at a specified coordinate
protectedSquaregetSquare(Coordinatec) {
returnboard[c.getColumnNumber()][c.getRowNumber()];
}
//Method is used by players to make moves and take away pieces
publicbooleanmove(Coordinatesrc, Coordinatedest) {
SquaresrcSquare = this.getSquare(src);
if (!srcSquare.isOccupied())
returnfalse;
Piecep = srcSquare.getPiece();
if (!p.getColour().equals(activeColour))
returnfalse;
if (p.isLegalMove(this, src, dest)) {
SquaredestSquare = this.getSquare(dest);
PiecetakenPiece = destSquare.addPiece(p);
if (takenPiece != null) {
if (takenPiece.getColour() == ChessColour.BLACK) {
obsBlackTakenPieces.add(takenPiece);
} else {
obsWhiteTakenPieces.add(takenPiece);
}
}
srcSquare.deletePiece();
activeColour = (activeColour == ChessColour.BLACK) ? ChessColour.WHITE : ChessColour.BLACK;
if (activeColour == ChessColour.WHITE)
fullMove++;
// fullMove is incremented only after BLACK has moved.
returntrue;
} else
returnfalse;
}
// Method registers listener on white and black observable lists
publicvoidaddTakenObserver(ListChangeListenerlistener) {
obsWhiteTakenPieces.addListener(listener);
obsBlackTakenPieces.addListener(listener);
}
//Print board to console using my own custom notation
publicStringtoString() {
Strings = "Board\n";
for (intr = 7; r >= 0; r--) {
for (intc = 0; c < 8; c++) {
Piecep = board[c][r].getPiece();
s += (p == null) ? " " : p.getShortName();
s += "_";
}
s += "\n";
}
returns;
}
//Print Board out to console using FEN notation
publicStringtoFEN() {
Strings = "";
for (intr = 7; r >= 0; r--) {
for (intc = 0; c < 8; c++) {
Piecep = board[c][r].getPiece();
s += (p == null) ? " " : p.getShortName();
}
s += "/";
}
s += " " + ((activeColour == ChessColour.WHITE) ? "w" : "b");
s += " " + fullMove;
s += "\n";
returns;
}
// Method prints out list of taken pieces starting with white
publicStringtoTakenString() {
Iterator<Piece> whiteIterator = whiteTakenPieces.iterator();
Iterator<Piece> blackIterator = blackTakenPieces.iterator();
Stringblack = "";
Stringwhite = "";
//Iterate through white taken pieces
while (whiteIterator.hasNext()) {
Piecepiece = (Piece) whiteIterator.next();
white += piece.getShortName();
}
//Iterate through black taken pieces
while (blackIterator.hasNext()) {
Piecepiece = (Piece) blackIterator.next();
black += piece.getShortName();
}
returnwhite + "," + black;
}
}