-
Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathChessBoard.java
More file actions
258 lines (168 loc) · 7.37 KB
/
ChessBoard.java
File metadata and controls
258 lines (168 loc) · 7.37 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
package com.chessBoard;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.chessBoard.interfaces.IChessBoard;
import com.chessBoard.interfaces.IChessBoardInitialPositionInitiator;
import com.chessPiece.interfaces.IChessPiece;
import com.chessPiece.interfaces.IChessPieceFactory;
import com.coordinates.Coordinates;
import com.enums.ChessCellColor;
import com.enums.ChessPieceColor;
import com.enums.ChessPieceType;
public class ChessBoard implements IChessBoard {
private static int DEFAULT_MAX_X = 8;
private static int DEFAULT_MAX_Y = 8;
private static ChessPieceColor DEFAULT_START_CHANCE_BY = ChessPieceColor.WHITE;
private Map<Coordinates, Set<Coordinates>> attacksMap;
private Map<Coordinates, Set<Coordinates>> attackedByMap;
private Map<Coordinates, IChessPiece> coordinateMap;
private List<List<Coordinates>> coordinatesMatrix;
private ChessPieceColor currentChanceBy;
private Map<ChessPieceColor, Set<Coordinates>> blockedPositionsForKing;
private int maxX;
private int maxY;
private IChessBoardInitialPositionInitiator chessBoardInitialPositionInitiator;
private Map<ChessPieceColor, Set<Coordinates>> checkMap;
private IChessPieceFactory chessPieceFactory;
private Map<ChessPieceColor, Coordinates> currentPositionOfKingMap;
public ChessBoard(IChessBoardInitialPositionInitiator chessBoardInitialPositionInitiator, IChessPieceFactory chessPieceFactory) {
this(ChessBoard.DEFAULT_MAX_X, ChessBoard.DEFAULT_MAX_Y, chessBoardInitialPositionInitiator, chessPieceFactory);
}
public ChessBoard(int maxX, int maxY, IChessBoardInitialPositionInitiator chessBoardInitialPositionInitiator, IChessPieceFactory chessPieceFactory) {
this(ChessBoard.DEFAULT_START_CHANCE_BY, maxX, maxY, chessBoardInitialPositionInitiator, chessPieceFactory);
}
public ChessBoard(ChessPieceColor currentChanceBy, int maxX, int maxY,
IChessBoardInitialPositionInitiator chessBoardInitialPositionInitiator, IChessPieceFactory chessPieceFactory) {
this.currentChanceBy = currentChanceBy;
this.maxX = maxX;
this.maxY = maxY;
this.chessBoardInitialPositionInitiator = chessBoardInitialPositionInitiator;
this.chessPieceFactory = chessPieceFactory;
this.instantiateBoard();
}
@Override
public IChessPiece getChessPieceAtCoordinate(Coordinates coordinates) {
// TODO Auto-generated method stub
return this.coordinateMap.get(this.getCoordinatesAtChessBoardObject(coordinates));
}
@Override
public int getMaxX() {
return this.maxX;
}
@Override
public int getMaxY() {
// TODO Auto-generated method stub
return this.maxY;
}
@Override
public boolean instantiateBoard() {
int i = 0;
this.coordinateMap = new HashMap<>();
this.attackedByMap = new HashMap<>();
this.attacksMap = new HashMap<>();
this.blockedPositionsForKing = new HashMap<>();
this.blockedPositionsForKing.put(ChessPieceColor.BLACK, new HashSet<>());
this.blockedPositionsForKing.put(ChessPieceColor.WHITE, new HashSet<>());
this.checkMap = new HashMap<>();
this.checkMap.put(ChessPieceColor.BLACK, new HashSet<>());
this.checkMap.put(ChessPieceColor.WHITE, new HashSet<>());
this.coordinatesMatrix = new ArrayList<>();
while (i < this.maxX) {
int j = 0;
ChessCellColor chessCellColor = ChessCellColor.BLACK;
if (i % 2 == 1) {
chessCellColor = ChessCellColor.WHITE;
}
while (j < this.maxY) {
if (this.coordinatesMatrix.size() <= i) {
this.coordinatesMatrix.add(new ArrayList<Coordinates>());
}
if (j % 2 == 1) {
this.coordinatesMatrix.get(i).add(new Coordinates(i, j, chessCellColor == ChessCellColor.BLACK ? ChessCellColor.WHITE : ChessCellColor.BLACK));
} else {
this.coordinatesMatrix.get(i).add(new Coordinates(i, j, chessCellColor));
}
this.chessBoardInitialPositionInitiator.setChessPieceAtCoordinate(this.coordinatesMatrix.get(i).get(j), this);
j++;
}
i++;
}
this.currentPositionOfKingMap = new HashMap<>();
this.currentPositionOfKingMap.put(ChessPieceColor.BLACK, this.coordinatesMatrix.get(0).get(maxY / 2));
this.currentPositionOfKingMap.put(ChessPieceColor.WHITE, this.coordinatesMatrix.get(maxX - 1).get(maxY / 2));
return true;
}
@Override
public boolean setChessPieceAtCoordinate(Coordinates coordinates, IChessPiece chessPiece) {
if (this.coordinateMap == null) {
this.coordinateMap = new HashMap<>();
}
Coordinates currentCoordinates = this.getCoordinatesAtChessBoardObject(coordinates);
if (this.coordinateMap.get(currentCoordinates).getChessPieceType() != ChessPieceType.NONE) {
this.setChessPieceAtCoordinate(currentCoordinates, this.chessPieceFactory.createChessPiece(ChessPieceType.NONE, ChessPieceColor.NONE));
}
if (chessPiece.getChessPieceType() == ChessPieceType.NONE) {
Set<Coordinates> attacks = this.attacksMap.get(currentCoordinates);
attacks.remove(currentCoordinates);
Iterator<Coordinates> it = attacks.iterator();
while (it.hasNext()) {
this.attackedByMap.get(it.next()).remove(currentCoordinates);
}
this.coordinateMap.put(currentCoordinates, chessPiece);
return true;
}
this.coordinateMap.put(currentCoordinates, chessPiece);
Set<Coordinates> attacks = chessPiece.getAllPossibleNextCoordinates(this, currentCoordinates);
Iterator<Coordinates> it = attacks.iterator();
while (it.hasNext()) {
Coordinates next = it.next();
if (!this.attacksMap.containsKey(currentCoordinates)) {
this.attacksMap.put(currentCoordinates, new HashSet<>());
}
this.attacksMap.get(currentCoordinates).add(next);
if (!this.attackedByMap.containsKey(next)) {
this.attackedByMap.put(next, new HashSet<>());
}
this.attackedByMap.get(next).add(currentCoordinates);
}
return true;
}
@Override
public boolean isValidMove(Coordinates startCoordinates, Coordinates endCoordinates) {
if (startCoordinates.getX() < 0 || startCoordinates.getY() < 0 || startCoordinates.getX() >= this.getMaxX() || startCoordinates.getY() >= this.getMaxY()) {
return false;
}
if (endCoordinates.getX() < 0 || endCoordinates.getY() < 0 || endCoordinates.getX() >= this.getMaxX() || endCoordinates.getY() >= this.getMaxY()) {
return false;
}
Coordinates start = this.coordinatesMatrix.get(startCoordinates.getX()).get(startCoordinates.getY());
Coordinates end = this.coordinatesMatrix.get(endCoordinates.getX()).get(endCoordinates.getY());
if (this.coordinateMap.get(start).getChessPieceColor() != this.currentChanceBy) {
return false;
}
if (this.coordinateMap.get(start).getChessPieceColor() == this.coordinateMap.get(end).getChessPieceColor()) {
return false;
}
return this.coordinateMap.get(start).isValidMove(start, end, this);
}
@Override
public Set<Coordinates> getBlockedPositionsForKing(ChessPieceColor chessPieceColor) {
return this.blockedPositionsForKing.get(chessPieceColor);
}
@Override
public Coordinates getCoordinatesAtChessBoardObject(Coordinates coordinates) {
return this.coordinatesMatrix.get(coordinates.getX()).get(coordinates.getY());
}
@Override
public boolean isChecked(ChessPieceColor chessPieceColor) {
if (this.checkMap.get(chessPieceColor) != null && this.checkMap.get(chessPieceColor).size() > 0) {
return true;
}
return false;
}
}