-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPUPlayer.java
316 lines (275 loc) · 10.1 KB
/
CPUPlayer.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
import java.util.ArrayList;
//Ken Schroeder
// August 2nd, 2015, 1101
// Starbucks, 5th Ave Portland, OR
// TicTacToe
// CPUPlayer.java
/*------------------------------------------------------------------------------
Tic-Tac-Toe:
Program Purpose:
This program is intended to simulate artificial intelligence in the game of
tic-tac-toe. The computer will search unti the end of the game and choose
the best move via the minimax method.
Instructions:
This program is a class designed to be run with playTicTacToe.java and
ticTacToe.java.
Program Includes The Following Methods
Public:
getMove -> runs the minimax method to find a move and plays it
Private:
minimax -> runs the minimax algorithm to find the best move
Implementation and Assumptions:
The computer plays perfect tic-tac-toe and sees the game through to the end
The computer does not care about winning quickly, all it cares about is the
end score
Future Development Ideas
-Recommend moves for the human player
-Keep track of possible winning positions, expected value of moves
-Have the computer choose a move more likely to cause disruption (more win possibilites)
-Have the computer choose the quickest winning move
-Alpha Beta Pruning
References
http://neverstopbuilding.com/minimax
http://stackoverflow.com/questions/17907255/make-the-computer-never-lose-at-tic-tac-toe
----------------------------------------------------------------------------*/
public class CPUPlayer {
//recommended moves
//better display (moves that'll win, lose, etc)
//adjust the score so that a quick win is better than a long win
//adjust the scores so that a position more likely to result in a win is better
//randomly choose from all the moves that result in the same score
//Member Variable
private ticTacToe game;
private int oMove;
private int xMove;
public CPUPlayer(ticTacToe g){
game = g;
xMove = g.getXMove();
oMove = g.getOMove();
}
public int minimax(ticTacToe t, int depth){
int score;
move bestMove;
//If the game is won or it is officially a cat (no more moves left), evaluate and return
if(t.isGameOver() || depth == 0){
return t.evaluate();
}
//find all the available moves
ArrayList<move> availableMoves = findAvailableMoves(t);
//fore very move you found, run the minimax algorithm on it...yep, that's it!
for (int i = 0; i < availableMoves.size(); i++){
ticTacToe possibleGame = new ticTacToe();
possibleGame.copy(t);
possibleGame.placePiece(availableMoves.get(i), possibleGame.getTurn());
availableMoves.get(i).setScore(minimax(possibleGame, depth - 1));
}
printPossibleMoves(t,availableMoves);
bestMove = getBestMove(t.getTurn(),availableMoves);
t.placePiece(bestMove,t.getTurn());
return bestMove.getScore();
/*
//Initialize the score
score = availableMoves.get(0).getScore();
//bestMove = availableMoves.get(0);
//Initialize the ArrayList that will hold all the best moves
//Best Moves = moves that return the best score
ArrayList<move> bestPossibleMoves = new ArrayList<move>();
//find the best score for x, looking for the biggest score for x
if(t.getTurn() == t.getXMove()){
//find the best score
for (int i = 0; i < availableMoves.size(); i++){
if(availableMoves.get(i).getScore() > score){
//bestMove = availableMoves.get(i);
score = availableMoves.get(i).getScore();
}
}
//add all scores that equal the best score to the bestPossibleMoves Array
for(int i = 0; i < availableMoves.size(); i++){
if(availableMoves.get(i).getScore() == score){
bestPossibleMoves.add(availableMoves.get(i));
}
}
//randomly choose a "best" move
int randomMove = (int)(Math.random() * bestPossibleMoves.size());
//place that piece
t.placePiece(availableMoves.get(randomMove), t.getTurn());
return score;
}
//Repeat, but with o
else{
for(int i = 0; i < availableMoves.size(); i++){
if(availableMoves.get(i).getScore() < score){
//bestMove = availableMoves.get(i);
score = availableMoves.get(i).getScore();
}
}
for(int i = 0; i < availableMoves.size(); i++){
if(availableMoves.get(i).getScore() == score){
bestPossibleMoves.add(availableMoves.get(i));
}
}
int randomMove = (int)(Math.random() * bestPossibleMoves.size());
t.placePiece(bestPossibleMoves.get(randomMove), t.getTurn());
// should I return the move which has the score?
return score;
}
*/
}
//Alpha Beta
//https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe_AI.html
public int alphaBeta(ticTacToe t, int depth, int alpha, int beta){
//initialize score variable -- why?
int score;
//If the game is won or it is officially a cat (no more moves left), evaluate and return
if(t.isGameOver() || depth == 0){
return t.evaluate();
}
//find all the available moves
ArrayList<move> availableMoves = findAvailableMoves(t);
//fore very move you found, run the minimax algorithm on it...yep, that's it!
for (int i = 0; i < availableMoves.size(); i++){
ticTacToe possibleGame = new ticTacToe();
possibleGame.copy(t);
possibleGame.placePiece(availableMoves.get(i), possibleGame.getTurn());
availableMoves.get(i).setScore(alphaBeta(possibleGame, depth - 1,alpha,beta));
if(possibleGame.getTurn() == xMove){
if(availableMoves.get(i).getScore() > alpha){
alpha = availableMoves.get(i).getScore();
}
}
else{
if(beta < availableMoves.get(i).getScore()){
beta = availableMoves.get(i).getScore();
}
}
if(alpha >= beta){
System.out.println("****\n\n**Alpha >= Beta\n\n****");
break;
}
}
//this code is just for fun, it displays all the possible moves and their respective scores
if(t.isMasterGame()){
System.out.println("---------------------------");
t.displayBoard();
System.out.println("The moves for this board are :");
for (int i = 0; i < availableMoves.size(); i++){
System.out.println("Move #" + i + availableMoves.get(i).toString() + " Score: " + availableMoves.get(i).getScore());
}
System.out.println("---------------------------");
}
//Initialize the score
score = availableMoves.get(0).getScore();
//bestMove = availableMoves.get(0);
//Initialize the ArrayList that will hold all the best moves
//Best Moves = moves that return the best score
ArrayList<move> bestPossibleMoves = new ArrayList<move>();
//find the best score for x, looking for the biggest score for x
if(t.getTurn() == t.getXMove()){
//find the best score
for (int i = 0; i < availableMoves.size(); i++){
if(availableMoves.get(i).getScore() > score){
//bestMove = availableMoves.get(i);
score = availableMoves.get(i).getScore();
}
}
//add all scores that equal the best score to the bestPossibleMoves Array
for(int i = 0; i < availableMoves.size(); i++){
if(availableMoves.get(i).getScore() == score){
bestPossibleMoves.add(availableMoves.get(i));
}
}
//randomly choose a "best" move
int randomMove = (int)(Math.random() * bestPossibleMoves.size());
//place that piece
t.placePiece(availableMoves.get(randomMove), t.getTurn());
return score;
}
//Repeat, but with o
else{
for(int i = 0; i < availableMoves.size(); i++){
if(availableMoves.get(i).getScore() < score){
//bestMove = availableMoves.get(i);
score = availableMoves.get(i).getScore();
}
}
for(int i = 0; i < availableMoves.size(); i++){
if(availableMoves.get(i).getScore() == score){
bestPossibleMoves.add(availableMoves.get(i));
}
}
int randomMove = (int)(Math.random() * bestPossibleMoves.size());
t.placePiece(bestPossibleMoves.get(randomMove), t.getTurn());
// should I return the move which has the score?
return score;
}
}
private ArrayList<move> findAvailableMoves(ticTacToe game){
//System.out.println("Adding all available moves");
ArrayList<move> movesToMake = new ArrayList<move>();
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if(game.getBoardVal(i, j) == 1){
move newMove = new move(i,j);
movesToMake.add(newMove);
}
}
}
return movesToMake;
}
//----------------------------------------------------------------------------
// getMove
// find CPU move and move there!
// logic for move is:
// 1. Can CPU win
// 2. Can opponent win
// 3. Move at random
//----------------------------------------------------------------------------
public void getMove(){
System.out.println("\nLooking for minimax");
minimax(game, 8);
//alphaBeta(game,2,-10000,10000);
}
private void printPossibleMoves(ticTacToe t, ArrayList<move> availableMoves){
//this code is just for fun, it displays all the possible moves and their respective scores
if(t.isMasterGame()){
System.out.println("---------------------------");
t.displayBoard();
System.out.println("The moves for this board are :");
for (int i = 0; i < availableMoves.size(); i++){
System.out.println("Move #" + i + availableMoves.get(i).toString() + " Score: " + availableMoves.get(i).getScore());
}
System.out.println("---------------------------");
}
}
private move getBestMove(int turn, ArrayList<move> availableMoves){
int bestScore = availableMoves.get(0).getScore();
ArrayList<move> bestPossibleMoves = new ArrayList<move>();
if(turn == xMove){
for(int i = 1; i < availableMoves.size(); i++){
if(availableMoves.get(i).getScore() > bestScore){
bestScore = availableMoves.get(i).getScore();
}
}
for(int i = 0; i < availableMoves.size(); i++){
if(availableMoves.get(i).getScore() == bestScore){
bestPossibleMoves.add(availableMoves.get(i));
}
}
}
else{
for(int i = 1; i < availableMoves.size(); i++){
if(availableMoves.get(i).getScore() < bestScore){
bestScore = availableMoves.get(i).getScore();
}
}
for(int i = 0; i < availableMoves.size(); i++){
if(availableMoves.get(i).getScore() == bestScore){
bestPossibleMoves.add(availableMoves.get(i));
}
}
}
//randomly choose a "best" move
int randomMove = (int)(Math.random() * bestPossibleMoves.size());
return bestPossibleMoves.get(randomMove);
}
}