-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPUPlayer.java
166 lines (140 loc) · 5.13 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
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();
}
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(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);
}
}