-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBoard.java
331 lines (257 loc) · 8.08 KB
/
Board.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
322
323
324
325
326
327
328
329
330
import java.awt.*;
public class Board {
public static final int red = 1;
public static final int blue = -1;
private Piece board [][] = new Piece[5][5];
private boolean containsBlueKing = true;
private boolean containsRedKing = true;
private boolean blueWin = false;
private boolean redWin = false;
private Card boardCard;
private Player curPlayer;
private Player offPlayer;
private Player aiCurPlayer;
private Player aiOffPlayer;
private Coordinate [] aiCoord;
private Card aiCard;
public Board(Board b) {
boardCard = new Card(b.getBoardCard());
for(int i = 0; i < 25; i++)
if(b.getPiece(i%5,i/5) != null)
board[i/5][i%5] = new Piece(b.getPiece(i%5,i/5));
curPlayer = new Player(b.getCurrentPlayer());
offPlayer = new Player(b.getOffPlayer());
}
public Board(Card inbetween, Player starter, Player off) {
curPlayer = starter;
offPlayer = off;
boardCard = inbetween;
for(int i = 0; i < 5; i++) {
board[0][i] = new Piece(new Coordinate(i,0), red, i == 2);
board[4][i] = new Piece(new Coordinate(i,4), blue, i == 2);
}
}
public Card getBoardCard() {
return boardCard;
}
public Player getCurrentPlayer() {
return curPlayer;
}
public Player getOffPlayer() {
return offPlayer;
}
public void switchPlayer() {
Player temp = curPlayer;
curPlayer = new Player(offPlayer);
offPlayer = new Player(temp);
}
public boolean isComputerTurn() {
return curPlayer.isComputer();
}
public void computerTurn() {
if(isComputerTurn()) {
alphabeta();
play(aiCoord[0], aiCoord[1]);
System.out.println(curPlayer.getColorString()+" ("+aiCoord[0]+"; "+aiCoord[1]+")"+"\t move uses card "+aiCard);
swapCard(aiCard);
}
}
private void alphabeta() {
aiCoord = null;
aiCard = null;
aiCurPlayer = new Player(curPlayer);
aiOffPlayer = new Player(offPlayer);
Board temp = new Board(boardCard, aiCurPlayer, aiOffPlayer);
aiCurPlayer.setBoard(temp);
aiOffPlayer.setBoard(temp);
System.out.println("Selected move score: "+alphabeta(this, aiCurPlayer.getDifficulty(), Integer.MIN_VALUE, Integer.MAX_VALUE, true));
}
public Piece getKingOfColor(int color) {
for(Piece p: getPiecesOfColor(color))
if(p != null && p.isKing())
return p;
return null;
}
public int getPieceCountByColor(int color) {
int count = 0;
for(Piece p: getPiecesOfColor(color))
if(p != null)
++count;
return count;
}
private int alphabeta(Board node, int depth, int alpha, int beta, boolean maximize) {
if(node.checkWin()) {
if(node.getWinner().getColor() == aiCurPlayer.getColor()) {
return 100+depth;
} else {
return -(100+depth);
}
}
if(depth <= 0) {
int curCount = node.getPieceCountByColor(aiCurPlayer.getColor());
int offCount = node.getPieceCountByColor(aiOffPlayer.getColor());
int curCloseness = node.getKingOfColor(aiCurPlayer.getColor()).getCoord().compare(
((aiCurPlayer.getColor() == red) ? Coordinate.blueEnd : Coordinate.redEnd));
int offCloseness = node.getKingOfColor(aiOffPlayer.getColor()).getCoord().compare(
((aiOffPlayer.getColor() == red) ? Coordinate.blueEnd : Coordinate.redEnd));
return (curCount-curCloseness)-(offCount-offCloseness);
}
if(maximize) {
int v = Integer.MIN_VALUE;
for(Card c : node.getCardsOfPlayerColor(aiCurPlayer.getColor())) {
for(Piece p: node.getPiecesOfColor(aiCurPlayer.getColor())) {
if(p == null) continue;
for(int [] move : c.getMoves()) {
Board child = new Board(node);
Coordinate from = new Coordinate(p.getCoord());
Coordinate to = new Coordinate(from);
if(!to.move(move, aiCurPlayer.getColor())) continue;
if(child.getPiece(from) == null) continue;
if(child.getPiece(to) != null && child.getPiece(from).getColor() == child.getPiece(to).getColor()) continue;
child.play(from, to);
child.swapCard(c);
child.switchPlayer();
int retVal = alphabeta(child, depth-1, alpha, beta, false);
if(v < retVal) {
v = retVal;
if(alpha < v) {
if(depth == aiCurPlayer.getDifficulty()) {
aiCoord = new Coordinate[] {from, to};
aiCard = c;
}
alpha = v;
if(beta <= alpha)
return v;
}
}
}
}
}
return v;
} else {
int v = Integer.MAX_VALUE;
for(Card c : node.getCardsOfPlayerColor(aiOffPlayer.getColor())) {
if(c==null) continue;
for(Piece p: node.getPiecesOfColor(aiOffPlayer.getColor())) {
if(p == null) continue;
for(int [] move : c.getMoves()) {
if(move == null) continue;
Board child = new Board(node);
Coordinate from = new Coordinate(p.getCoord());
Coordinate to = new Coordinate(from);
if(!to.move(move, aiOffPlayer.getColor()) || from.equals(to)) continue;
if(child.getPiece(from) == null) continue;
if(child.getPiece(to) != null && child.getPiece(from).getColor() == child.getPiece(to).getColor()) continue;
child.play(from, to);
child.swapCard(c);
child.switchPlayer();
int retVal = alphabeta(child, depth-1, alpha, beta, true);
if(v > retVal) {
v = retVal;
if(beta > v) {
beta = v;
if(beta <= alpha)
return v;
}
}
}
}
}
return v;
}
}
public Piece[][] getBoard() {
return board;
}
public Card[] getCardsOfPlayerColor(int color) {
return getPlayerOfColor(color).getCards();
}
public Piece getPiece(int x, int y) {
return board[y][x];
}
public Piece [] getPiecesOfPlayer(Player p) {
return getPiecesOfColor(p.getColor());
}
public Piece [] getPiecesOfColor(int color) {
int index = 0;
Piece [] pieces = new Piece[5];
for(int i = 0; i < 25; i++)
if(getPiece(i%5,i/5) != null && getPiece(i%5,i/5).getColor() == color)
pieces[index++] = new Piece(getPiece(i%5,i/5));
return pieces;
}
public Piece getPiece(Coordinate c) {
return getPiece(c.getX(),c.getY());
}
public Card exchange(Card c) {
Card temp = boardCard;
boardCard = c;
return temp;
}
public Player getWinner() {
if(blueWin || !containsRedKing)
return getPlayerOfColor(blue);
if(redWin || !containsBlueKing)
return getPlayerOfColor(red);
return null;
}
public boolean checkWin() {
Piece p = getPiece(Coordinate.redEnd);
if(p != null && p.isKing() && p.getColor() == blue)
return (blueWin = true);
p = getPiece(Coordinate.blueEnd);
if(p != null && p.isKing() && p.getColor() == red)
return (redWin = true);
containsBlueKing = (getKingOfColor(blue) != null);
containsRedKing = (getKingOfColor(red) != null);
return !(containsBlueKing && containsRedKing);
}
public void play(Coordinate from, Coordinate to) {
if(getPiece(to) != null && getPiece(to).isKing() && getPiece(to).getColor() != getPiece(from).getColor())
if(getPiece(to).getColor() == blue)
containsBlueKing = false;
else
containsRedKing = false;
board[to.getY()][to.getX()] = new Piece(getPiece(from));
getPiece(to.getX(),to.getY()).setCoord(to);
board[from.getY()][from.getX()] = null;
}
public void printBoard() {
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++)
System.out.printf("|%2s ",(board[i][j] == null)?"":(board[i][j].getColor() == red)? "r":"b");
System.out.println("|");
}
}
public Player getPlayerOfColor(int color) {
if(curPlayer.getColor() == color)
return curPlayer;
return offPlayer;
}
public void swapCard(Card handCard) {
for(int i = 0; i < 2; i++) {
if(curPlayer.getCards()[i].equals(handCard)) {
curPlayer.setCard(i, boardCard);
boardCard = new Card(handCard);
break;
}
if(offPlayer.getCards()[i].equals(handCard)) {
offPlayer.setCard(i, boardCard);
boardCard = new Card(handCard);
break;
}
}
}
public Board copy() {
return new Board(this);
}
public String toString() {
return "Current Player:\n"+curPlayer+"\nOff Player:\n"+offPlayer+"\nBoardCard:"+boardCard+"\nBoard:\n"+getBoardString();
}
private String getBoardString() {
String ret = "";
for(int i = 0; i < 25; i++)
ret+= "|"+(i%5)+((getPiece(i%5,i/5) == null)? " " : ((getPiece(i%5,i/5).getColor() == red) ? "r" : "b"))+(i/5)+((i % 5 == 4)?"|\n":"");
return ret;
}
}