-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCheckers.java
251 lines (236 loc) · 8.53 KB
/
Checkers.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
import java.util.*;
import java.io.*;
public class Checkers
{
private final int length = 8;
private char[][] main;
private int turn = 0;
private Scanner scan;
/*
* Create a multi dimensional array
* w stands for white
* n stand for none/null
* r stands for red
*/
public Checkers()
{
scan = new Scanner(System.in);
main = new char[8][8];
for(int i = 0; i < length; i++)
{
main[i] = new char[8];
for(int j = 0; j < length; j++){
if(i < 4) main[i][j] = 'w';
else if(i == 4 || i == 3) main[i][j] = 'n';
else if(i < 8) main[i][j] = 'r';
}
}
}
public int getTurn() {return turn;}
public char[][] getBoard() {return main;}
public void setBoard(char[][] main) {this.main = main;}
private int score(char[][] board)
{
int red = 0;
int white = 0;
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++)
{
if(board[i][j] == 'w') white++;
else if(board[i][j] == 'r') red++;
}
}
return white - red;
}
private int whiteLeft(char[][] board)
{
int white = 0;
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++)
{
if(board[i][j] == 'w') white++;
}
}
return white;
}
private int redLeft(char[][] board)
{
int red = 0;
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++)
{
if(board[i][j] == 'w') red++;
}
}
return red;
}
private char opp(char curr)
{
if(curr == 'w') return 'r';
return 'w';
}
public boolean check(char[][] board, int inRow, int inCol, int finalRow, int finalCol)
{
if(finalRow >= 8|| finalCol >= 8 || finalRow < 0 || finalCol < 0) return false;
int rowDiff = finalRow - inRow;
int colDiff = finalCol - inCol;
if(board[finalRow][finalCol] == 'n')
{
if(Math.abs(rowDiff) == 2 && Math.abs(colDiff) == 2)
{
if(rowDiff < 0)
{
if(colDiff < 0)
{
if(board[inRow - 1][inCol - 1] == opp(board[inRow][inCol])) return true;
}
else if(colDiff > 0) if(board[inRow - 1][inCol + 1] == opp(board[inRow][inCol])) return true;;
}
else if(rowDiff > 0)
{
if(colDiff < 0)
{
if(board[inRow + 1][inCol - 1] == opp(board[inRow][inCol])) return true;
}
else if(colDiff > 0) if(board[inRow + 1][inCol + 1] == opp(board[inRow][inCol])) return true;;
}
canJump(board, finalRow, finalCol, false, 1);
}
else if(Math.abs(rowDiff) == 1 && Math.abs(colDiff) == 1)
{
return true;
}
}
return false;
}
public boolean canJump(char[][] board, int row, int col, boolean isKing, int depth)
{
int[] increment = {1,-1};
if(isKing)
{
for(int i = 0; i < 2; i++)
{
for(int x = 0; x < 2; x++)
{
if(board[row + increment[i]][col + increment[x]] == opp(board[row][col]) && board[row + i*2][col + x*2] == 'n') return true;
}
}
}
else
{
int forward = board[row][col] == 'w' ? -1:1;
for(int i = 0; i < 2; i++)
{
for(int x = 0; x < 2; x++)
{
if(board[row + forward][col + increment[i]] == opp(board[row][col]) && board[row + i*2][col + x*2] == 'n') return true;
}
}
}
return false;
}
public char[][] takeTurn(char[][] board, int inRow, int inCol, int finalRow, int finalCol)
{
if(check(board,inRow,inCol,finalRow,finalCol))
{
if(Math.abs(finalCol - inCol) == 2 && Math.abs(finalRow - inRow) == 2)
{
board[finalRow-(finalRow - inRow)/2][finalCol-(finalCol - inCol)/2] = 'n';
}
board[finalRow][finalCol] = board[inRow][inCol];
board[inRow][inCol] = 'n';
turn++;
}
return board;
}
// Add double jump and king pieces king has value 2
// AI chooses how many times to skip pieces
// Remember to implement not run minimax if only one possibility for ai
// Returns optimal value for current player(Initially called for root and maximizer)
public int minimax(int depth, boolean white, char[][] board, int alpha, int beta, int piecesLeft)
{
int score = score(board);
if(white)
{
if(depth == 20 || piecesLeft == 0) return score; // Base Case
int best = Integer.MIN_VALUE;
// Recur for left and right children
List<Integer> indice = new ArrayList<Integer>(piecesLeft*2); //find locations of all remaining white pieces formatted [row,col]
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j+=2)
{
if(board[i][j] == 'w') {
indice.add(i);
indice.add(j);
}
}
}
for(int i = 0; i < piecesLeft*2; i+=2)
{
int x = indice.get(i);
int y = indice.get(i + 1);
for (int j = 0; j < 2; j++)
{
int[] direction = {1,-1};
// if can take
if(check(board, indice.get(i), indice.get(i+1), indice.get(i) + direction[j], indice.get(i+1) + 1)) //if is possible move take move
{
board[x][y] = 'n'; //try move
board[indice.get(i) + direction[j]][indice.get(i+1) + 1] = 'w'; //try move
int val = minimax(depth++, false, board, alpha, beta, redLeft(board));
best = Math.max(best, val);
alpha = Math.max(alpha, best);
board[x][y] = 'w'; //reverse move
board[indice.get(i) + direction[j]][indice.get(i+1) + 1] = 'n'; //reverse move
if (beta <= alpha) // Alpha Beta Pruning
break;
}
}
}
return best;
}
else
{
if(depth == 10 || piecesLeft == 0) return score; // Base Case
int best = Integer.MAX_VALUE;
List<Integer> indice = new ArrayList<Integer>(piecesLeft*2); //find locations of all remaining white pieces formatted [row,col]
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j+=2)
{
if(board[i][j] == 'r') {
indice.add(i);
indice.add(j);
}
}
}
for(int i = 0; i < piecesLeft*2; i+=2)
{
int x = indice.get(i);
int y = indice.get(i + 1);
for (int j = 0; j < 2; j++)
{
int[] direction = {1,-1};
// if can take
if(check(board, indice.get(i), indice.get(i+1), indice.get(i) + direction[j], indice.get(i+1) + 1)) //if is possible move take move
{
board[x][y] = 'n'; //try move
board[indice.get(i) + direction[j]][indice.get(i+1) + 1] = 'w'; //try move
int val = minimax(depth++, false, board, alpha, beta, redLeft(board));
best = Math.max(best, val);
alpha = Math.max(alpha, best);
board[x][y] = 'w'; //reverse move
board[indice.get(i) + direction[j]][indice.get(i+1) + 1] = 'n'; //reverse move
if (beta <= alpha) // Alpha Beta Pruning
break;
}
}
}
return best;
}
}
}