-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChessGameBoard.java
321 lines (272 loc) · 8.37 KB
/
ChessGameBoard.java
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
import java.util.ArrayList;
public class ChessGameBoard {
ProposedBoard proposedBoard; // game board with next moves including collisions
ProposedBoard black; // game board with next moves including collisions
ProposedBoard white; // game board with next moves including collisions
private Piece[][] board; // a board of chess pieces
private int ID=100; // used to create a unique ID value for each piece
Game game;
enum Game {
RANDOM,
BATTLEROYALE
}
// generate unique IDs
private int getNewID() {
ID++;
return ID;
}
public ChessGameBoard() {
// create a board of chess pieces
board = new Piece[8][8];
//add black pawns
for(int i=0;i<8;++i) {
board[i][1] = new Pawn(Piece.Team.BLACK,getNewID(),1,i);
}
//add the other pieces
board[0][0] = new Piece(Piece.Team.BLACK, Piece.Type.ROOK,getNewID());
board[7][0] = new Piece(Piece.Team.BLACK, Piece.Type.ROOK,getNewID());
board[1][0] = new Piece(Piece.Team.BLACK, Piece.Type.KNIGHT,getNewID());
board[6][0] = new Piece(Piece.Team.BLACK, Piece.Type.KNIGHT,getNewID());
board[2][0] = new Piece(Piece.Team.BLACK, Piece.Type.BISHOP,getNewID());
board[5][0] = new Piece(Piece.Team.BLACK, Piece.Type.BISHOP,getNewID());
board[3][0] = new Piece(Piece.Team.BLACK, Piece.Type.QUEEN,getNewID());
board[4][0] = new Piece(Piece.Team.BLACK, Piece.Type.KING,getNewID());
//add white pawns
for(int i=0;i<8;++i) {
board[i][6] = new Pawn(Piece.Team.WHITE,getNewID(),6,i);
}
//add the other pieces
board[0][7] = new Piece(Piece.Team.WHITE, Piece.Type.ROOK,getNewID());
board[7][7] = new Piece(Piece.Team.WHITE, Piece.Type.ROOK,getNewID());
board[1][7] = new Piece(Piece.Team.WHITE, Piece.Type.KNIGHT,getNewID());
board[6][7] = new Piece(Piece.Team.WHITE, Piece.Type.KNIGHT,getNewID());
board[2][7] = new Piece(Piece.Team.WHITE, Piece.Type.BISHOP,getNewID());
board[5][7] = new Piece(Piece.Team.WHITE, Piece.Type.BISHOP,getNewID());
board[3][7] = new Piece(Piece.Team.WHITE, Piece.Type.QUEEN,getNewID());
board[4][7] = new Piece(Piece.Team.WHITE, Piece.Type.KING,getNewID());
proposedBoard = new ProposedBoard();
game = Game.BATTLEROYALE;
}
// print the current Board
public String printCurrentBoard() {
String line = "";
String row = "";
for(int k=0;k<8;k++) {
row+="---";
}
row+="-\n";
for(int i=0;i<8;++i) {
line = "|";
for(int j=0;j<8;++j) {
if (board[i][j] == null) {
line += " |";
} else {
if (board[i][j].getTeam() == Piece.Team.BLACK) {
line += "B";
} else {
line += "W";
}
if (board[i][j].getType() == Piece.Type.PAWN) {
line += "P";
} else if (board[i][j].getType() == Piece.Type.ROOK) {
line += "R";
} else if (board[i][j].getType() == Piece.Type.KNIGHT) {
line += "K";
} else if (board[i][j].getType() == Piece.Type.BISHOP) {
line += "B";
} else if (board[i][j].getType() == Piece.Type.QUEEN) {
line += "Q";
} else if (board[i][j].getType() == Piece.Type.KING) {
line += "+";
}
line+= "|";
}
}
row+=line+"\n";
for(int k=0;k<8;k++) {
row+="---";
}
row+="-\n";
}
return row;
}
//returns the chess piece at those coordinates
public Piece getPiece(int x, int y) {
return board[y][x];
}
public void setPiece(int x, int y, Piece p) {
board[y][x] = p;
}
public Position getPositionByID(int ID) {
for(int y=0;y<8;++y) {
for(int x=0;x<8;++x) {
if ((board[y][x]!= null) && (board[y][x].getID() == ID)) {
return new Position(x,y);
}
}
}
return null;
}
public ChessGameBoard(Game g) {
// board = new Board();
proposedBoard = new ProposedBoard();
game = g;
}
private ProposedBoard buildProposedBoard(Piece.Team t) {
ProposedBoard teamBoard = new ProposedBoard();
for(int i=0;i<8;++i) {
for(int j=0;j<8;++j) {
Piece p = board[i][j];
if (p != null && p.getTeam() == t) {
if(p.getType() == Piece.Type.PAWN) {
Pawn pawn = (Pawn) p;
pawn.setNextPosition(this);
teamBoard.add(pawn.getNextPosition().getX(),pawn.getNextPosition().getY(),p);
}
/*if(p.getType() == Piece.Type.ROOK){
Rook rook = (Rook) p;
rook.setNextPosition(this);
teamBoard.add(rook.getNextPosition().getX(), rook.getNextPosition().getY(), p);
}*/
}
}
}
return teamBoard;
}
private ProposedBoard nextMoveTeam(Piece.Team t) {
ProposedBoard teamBoard = buildProposedBoard(t);
while(checkConflicts(teamBoard)) {
//resolveConflicts();
}
return teamBoard;
}
private ProposedBoard nextMoveRandomTeam(Piece.Team t) {
ProposedBoard teamBoard = buildProposedBoard(t);
while(checkConflicts(teamBoard)) {
resolveConflicts(teamBoard);
}
return teamBoard;
}
private boolean checkConflicts(ProposedBoard teamBoard) {
for(int i=0;i<8;++i) {
for(int j=0;j<8;++j) {
if(teamBoard.countPieces(i, j) > 1) {
return true;
}
}
}
return false;
}
private void resolveConflicts(ProposedBoard teamBoard) {
for(int i=0;i<8;++i) {
for(int j=0;j<8;++j) {
if(teamBoard.countPieces(j,i) > 1) {
// collision has occurred, so process
// make a utility array to collect piece list
ArrayList<Piece> movers = new ArrayList<Piece>(); // pieces that moved to space
Piece sitter = null; //piece that was in the space
System.out.print("Collision on ("+ j + "," + i + ")!!");
for(Piece t:teamBoard.getPieces(j,i)) {
//check if player has moved
System.out.print(t.getID() + ":");
if ((getPositionByID(t.getID()).getX() != j) ||
(getPositionByID(t.getID()).getY() != i)) {
// then add them to the list
System.out.print(" (moved) ");
movers.add(t);
} else {
System.out.print(" (stayed) ");
sitter = new Piece(t.getTeam(),t.getType(),t.getID());
}
}
System.out.println();
teamBoard.clear(j,i);
// if more than one person moved to the spot then send them all back
if (movers.size() > 1) {
// put everyone back to their previous positions
for(Piece p : movers) {
teamBoard.add(getPositionByID(p.getID()).getX(),getPositionByID(p.getID()).getY(),p);
// System.out.print(p.getID()+":");
System.out.println(p.getID() + " sent back");
}
// leave any players in place
if (sitter != null)
teamBoard.add(j,i,sitter);
} else {
teamBoard.add(j, i,movers.get(0));
System.out.println(movers.get(0).getID() + " killed " + sitter.getID());
}
movers.clear();
//System.out.println(" resolved");
}
}
}
}
private void combineBoards(ProposedBoard a, ProposedBoard b) {
proposedBoard.clearAll();
for(int i=0;i<8;++i) {
for(int j=0;j<8;++j) {
for(Piece p: a.getPieces(i,j)) {
proposedBoard.add(i, j, p);
}
for(Piece p: b.getPieces(i,j)) {
proposedBoard.add(i, j, p);
}
}
}
}
//moves all valid moves for players on both teams
public void nextMoveAll() {
black = nextMoveTeam(Piece.Team.BLACK);
while(checkConflicts(black)) {
resolveConflicts(black);
}
white = nextMoveTeam(Piece.Team.WHITE);
while(checkConflicts(white)) {
resolveConflicts(white);
}
combineBoards(black, white);
black.clearAll();
white.clearAll();
while(checkConflicts(proposedBoard)) {
resolveConflicts(proposedBoard);
}
//combine proposed boards
//check and resolve conflicts
}
//moves one random move for each team
public void nextMoveRandom() {
black = nextMoveRandomTeam(Piece.Team.BLACK);
white = nextMoveRandomTeam(Piece.Team.WHITE);
combineBoards(black, white);
black.clearAll();
white.clearAll();
while(checkConflicts(proposedBoard)) {
resolveConflicts(proposedBoard);
}
}
public void applyNextMoves() {
for(int i=0;i<8;++i) {
for(int j=0;j<8;++j) {
if(proposedBoard.getPieces(j,i).size() > 0) {
Pawn p = (Pawn) proposedBoard.getPieces(j,i).get(0); // @mrH can you explain this it's rather cryptic
p.setX(j);
p.setY(i);
board[i][j] = p;
/*Rook r = (Rook) proposedBoard.getPieces(j, i).get(0);
r.setX(j);
r.setY(i);
board[i][j] = r;*/
} else {
board[i][j] = null;
}
}
}
}
public String printProposedBoard() {
return proposedBoard.toString();
}
public String toString() {
return "Current Board: \n\n" + printCurrentBoard() + " \n\nProposed Moves: \n\n" + proposedBoard.toString();
}
}