-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathComputerConnect4Player.java
More file actions
312 lines (250 loc) · 9.47 KB
/
ComputerConnect4Player.java
File metadata and controls
312 lines (250 loc) · 9.47 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
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
public class ComputerConnect4Player extends Player {
private int depth; // depth to search at
// my weights
// need to be public/package for testing static evaluation func in Connect4Game.java
public static final int[] HOW_GOOD = {0, 2, 10^2, 10^3, 10^8}; // index is # of unblocked four-in-row potentials
// the closer a piece is to the center, the more 4-in-row permutations available.
// i.e.., generally center piece is most valuable
private static final int[] movesByCol = { 3, 4, 2, 5, 1, 6, 0 };
/**
* Create a computer player with a given name
* @param name name of computer player
*/
public ComputerConnect4Player(String name, int depth){
super(name);
this.depth = depth;
}
@Override
public int getMove(Connect4State state, Connect4View view) {
// First copy the game instance
Connect4Game stateCopy = new Connect4Game(state.getPlayerNum(), state.getPlayers(), state.getBoard(), evaluate(state), movesDone(state));
// pick the move
// start alpha-beta with neg and pos infinities
Connect4Move chosenMoveObj = pickMove(stateCopy, depth, -Integer.MAX_VALUE, Integer.MAX_VALUE, view);
int chosenMove = chosenMoveObj.move;
view.reportMove(chosenMove, state.getPlayerToMove().getName());
return chosenMove;
}
/**
* Uses game tree search with alpha-beta pruning to pick player's move
* low and high define the current range for the best move
*
* @param state the current state of the game
* @param depth the number of moves to look ahead in game tree search
* @param low a value that the player can achieve by some other move
* @param high a value that the opponent can force by a different line of play
* @param view view for testing purposes
*
* @return the move chosen
*/
private static Connect4Move pickMove(Connect4Game state, int depth, int low, int high, Connect4View view){
Connect4Move[] movesArray; // order of moves
// grab the available moves, sorted by value
movesArray = checkMoves(state);
// dummy move that will be replaced with evaluation
Connect4Move bestMove = new Connect4Move(-Integer.MAX_VALUE, -10);
// Use alpha-beta pruning to pick the move
for (int i = 0; i < 7 && bestMove.value < high; i++){
// grab the move from list
int column = movesArray[i].move;
if (state.isValidMove(column)){
Connect4Move currentMove;
// grab value of current position to restore later
int evalValue = state.grabEvalValue();
state.makeMove(column);
// testing
view.display(state);
System.out.println("===============");
System.out.println("Position Eval # :" + evaluate(state));
System.out.println("===============");
if (state.gameIsOver()){
// Is game over because board is full?
if (state.isFull()){
currentMove = new Connect4Move(0, column); // assign value of 0
}
// if it's comp's turn, then this must be a win scenario
currentMove = new Connect4Move(HOW_GOOD[4], column);
}
// keep going if depth available
else if (depth >= 1){
// Switch player perspective
// Reduce depth by 1
currentMove = pickMove(state, depth - 1, -high, -low, view);
// transfer values back while changing perspective
currentMove.value = (currentMove.value * -1);
currentMove.move = column;
} else {
currentMove = new Connect4Move(state.grabEvalValue(), column);
}
// Is the current move better than what we've found so far?
if (currentMove.value > bestMove.value){
bestMove = currentMove; // replace
low = Math.max(bestMove.value, low); // update the achievable lower bound value
}
// undo move before trying next move
state.undoMove(column, evalValue);
}
}
return bestMove;
}
/**
* Check the move list for their associated values
* Then sort them by value
*
* @param state the current state of the game
* @return an array of moves sorted by their values
*/
private static Connect4Move[] checkMoves(Connect4Game state){
int stateEval; // evaluation of current state based on unblocked 4 in rows
Connect4Move[] movesArray = new Connect4Move[Connect4Game.COLS];
stateEval = state.grabEvalValue();
// go through each column in move list
for (int i = 0; i < Connect4Game.COLS; i++){
int theMove = movesByCol[i];
movesArray[i] = new Connect4Move(-Integer.MAX_VALUE, theMove);
if (state.isValidMove(theMove)){
// try the move
state.makeMove(theMove);
// now evaluate the new state and store value to check against later
movesArray[i].value = state.grabEvalValue();
// undo the state before checking again
state.undoMove(theMove, stateEval);
}
}
// sort the move lists by values
for (int i = 1; i < Connect4Game.COLS; i++){
for (int compare = i; (compare >=1 && movesArray[compare].value >
movesArray[compare - 1].value);
compare--){
// placeholder to prevent clobbering
Connect4Move placeholder = movesArray[compare];
movesArray[compare] = movesArray[compare - 1];
movesArray[compare - 1] = placeholder;
}
}
// new set of moves with updated values
return movesArray;
}
/**
* Helper method that counts the moves made
*
* @param state the input state of the board
* @return the number of moves already made
*/
private static int movesDone(Connect4State state){
// count the pieces
int counter = 0;
for (int row = 0; row < Connect4Game.ROWS; row++){
for (int column = 0; column < Connect4Game.COLS; column++){
if (state.getBoard()[row][column] != Connect4Game.EMPTY) counter++;
}
}
return counter;
}
/**
* Evaluate position by finding unblocked 4 in a rows
*
* @param state the input state of the board
* @return a total int evaluation of unblocked four-in-rows for opp and computer
*/
public static int evaluate(Connect4State state){
// grab the checker pieces and board
char opponent = Connect4State.CHECKERS[1 - state.getPlayerNum()];
char player = Connect4State.CHECKERS[state.getPlayerNum()];
char[][] board = state.getBoard();
// value that evaluates the unblocked four-in-rows
int totalEvaluation = 0;
// Evaluate patterns for winning
//
// . X X . . => unblocked on both sides so we can connect 4
// by placing another piece to become
// . X X X .
for (int checkColumn = 0; checkColumn < 3; checkColumn ++){
// if 0 is empty, followed by 2 of my pieces and two more empty, this is a pattern
if (board[0][checkColumn] == Connect4State.EMPTY &&
board[0][checkColumn + 1] == player &&
board[0][checkColumn + 2] == player &&
board[0][checkColumn + 3] == Connect4State.EMPTY &&
board[0][checkColumn + 4] == Connect4State.EMPTY){
totalEvaluation += HOW_GOOD[3];
} else if (board[0][checkColumn] == Connect4State.EMPTY &&
board[0][checkColumn + 1] == Connect4State.EMPTY &&
board[0][checkColumn + 2] == player &&
board[0][checkColumn + 3] == player &&
board[0][checkColumn + 4] == Connect4State.EMPTY){
totalEvaluation += HOW_GOOD[3];
}
}
// Evaluate unblocked verticals
// all potential ver 4-in-row start from at most from row 2
for (int column = 0; column < Connect4Game.COLS; column++){
for (int row = 0; row < 3; row++){
int compCount = 0;
int oppCount = 0;
for (int checkRow = row; checkRow < row + 4; checkRow++){
if (board[checkRow][column] == player){
compCount++;
} else if (board[checkRow][column] == opponent){
oppCount++;
}
}
totalEvaluation = Connect4Game.applyWeights(oppCount, compCount, totalEvaluation);
}
}
// Evaluate unblocked horizontals
// all potential hor 4-in-row start from at most from halfway col
for (int column = 0; column <= 3; column++){
for(int row = 0; row < Connect4Game.ROWS; row++){
// counters for computer and opponent
int compCount = 0;
int oppCount = 0;
for (int checkColumn = column; checkColumn < column + 4; checkColumn++){
// check whose checker it is and increment their counter
if (board[row][checkColumn] == player){
compCount++;
} else if (board[row][checkColumn] == opponent){
oppCount++;
}
}
totalEvaluation = Connect4Game.applyWeights(oppCount, compCount, totalEvaluation);
}
}
// Evaluate unblocked diagonals (up to right)
// up to right diagonal start at most from row 2, column 3
for (int column = 0; column < 4; column++){
for (int row = 0; row < 3; row++){
int compCount = 0;
int oppCount = 0;
int checkRow = row; // need a checkrow parameter for diag
for (int checkColumn = column; checkRow < row + 4; checkColumn++){
if (board[checkRow][checkColumn] == player){
compCount++;
} else if (board[checkRow][checkColumn] == opponent){
oppCount++;
}
checkRow++; // adjust for diagonal
}
totalEvaluation = Connect4Game.applyWeights(oppCount, compCount, totalEvaluation);
}
}
// Evaluate unblocked diagonals (down to right)
// down to right diagonal start at most from row 3, column 3
for (int column = 0; column < 4; column++){
for (int row = 3; row <= 5; row++){
int compCount = 0;
int oppCount = 0;
int checkRow = row; // need a checkrow parameter for diag
for (int checkColumn = column; checkColumn < column + 4; checkColumn++){
if (board[checkRow][checkColumn] == player){
compCount++;
} else if (board[checkRow][checkColumn] == opponent){
oppCount++;
}
checkRow--; // adjust for diagonal
}
totalEvaluation = Connect4Game.applyWeights(oppCount, compCount, totalEvaluation);
}
}
return totalEvaluation;
}
}