-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAI.java
More file actions
260 lines (221 loc) · 9.18 KB
/
AI.java
File metadata and controls
260 lines (221 loc) · 9.18 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
import entity.Amazon;
import entity.GameBoard;
import entity.Move;
import evaluation.FunctionEvaluation;
import evaluation.WebEvaluation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
/**
* Created by amareelez on 23.12.16.
*/
@SuppressWarnings("JavaDoc") class AI {
private GameBoard board = null;
public AI(GameBoard b) {
this.board = b;
}
public Move.MoveAndBoard getNextMove(GameBoard b) {
this.board = b;
//TestClass2.AI.Edge nextMove = GreedyBestSearch(2);
return minimaxSearch(1000, 4, 3);
}
// currently not using
private Move.MoveAndBoard GreedyBestSearch(int advance) {
//we need to check to see if any of our amazons are almost blocked in (having a freedom of 1 or 2). If so then we need to move them immediately and not worry about anything else
Amazon current = checkForDireAmazon();
ArrayList<Move.MoveAndBoard> searchResults = new ArrayList<>();
// if we found an amazon that needs to be moved, only look at its possible moves.
if (current != null) {
//get only moves from the amazon
searchResults.addAll(board.getPossibleMoves(current.id));
if (searchResults.size() == 0) {
for (int i = 0; i < 4; i++) {
searchResults.addAll(board.getPossibleMoves(i));
}
}
} else {
searchResults = new ArrayList<>();
for (int i = 0; i < 4; i++) {
searchResults.addAll(board.getPossibleMoves(i));
}
}
Move.MoveAndBoard newState;
if (advance == 0)
newState = defaultHeuristic(searchResults);
else if (advance == 1)
newState = advancedHeuristic(searchResults);
else
newState = superHeuristic(searchResults);
return newState;
}
/**
* @param timer - Amount of seconds to restrict the search to.
* @param depth - The maximum depth to expand within the amount of time.
* @return Recommended next move.
*/
private Move.MoveAndBoard minimaxSearch(int timer, int depth, int advance) {
SearchNode initialNode = new SearchNode(new Move.MoveAndBoard(board, null));
long endtime = System.currentTimeMillis() + timer;
return minimax(initialNode, true, endtime, depth, advance).state;
}
private SearchNode minimax(SearchNode node, boolean max, long endtime, int depth, int advance) {
if ((System.currentTimeMillis() - TestClass2.time > 500) || (depth <= 0))
return new SearchNode(node.state,
new WebEvaluation(node.state.getNewBoard()).Evaluate());
double alpha;
ArrayList<SearchNode> children;
if (advance == 1) {
children = getAdvancedChildren(node, max);
alpha = max ? Double.MIN_VALUE : Double.MAX_VALUE;
} else if (advance == 0) {
children = getChildren(node, max);
alpha = max ? Integer.MIN_VALUE : Integer.MAX_VALUE;
} else {
children = getSuperChildren(node, max);
alpha = max ? Double.MIN_VALUE : Double.MAX_VALUE;
}
SearchNode nextBestNode = null;
for (SearchNode aChildren : children) {
if (System.currentTimeMillis() - TestClass2.time > 500)
break;
nextBestNode = minimax(aChildren, !max, endtime, depth - 1, advance);
alpha = max ?
Math.max(alpha, nextBestNode.heuristic) :
Math.min(alpha, nextBestNode.heuristic);
nextBestNode.heuristic = alpha;
}
return nextBestNode;
}
private ArrayList<SearchNode> getSuperChildren(SearchNode node, boolean ourTurn) {
ArrayList<SearchNode> children = new ArrayList<>();
for (int i = 0; i < 4; i++) {
int j = ourTurn ? i : i + 4;
ArrayList<Move.MoveAndBoard> amazonMoves = node.state.getNewBoard().getPossibleMoves(j);
for (Move.MoveAndBoard amazonMove : amazonMoves) {
children.add(new SearchNode(amazonMove,
new WebEvaluation(amazonMove.getNewBoard()).Evaluate()));
}
}
return children;
}
private ArrayList<SearchNode> getChildren(SearchNode node, boolean ourTurn) {
ArrayList<SearchNode> children = new ArrayList<>();
for (int i = 0; i < 4; i++) {
int j = ourTurn ? i : i + 4;
ArrayList<Move.MoveAndBoard> amazonMoves = node.state.getNewBoard().getPossibleMoves(j);
for (Move.MoveAndBoard amazonMove : amazonMoves) {
children
.add(new SearchNode(amazonMove, evaluateHeuristic(amazonMove.getNewBoard())));
}
}
return children;
}
private ArrayList<SearchNode> getAdvancedChildren(SearchNode node, boolean ourTurn) {
ArrayList<SearchNode> children = new ArrayList<>();
for (int i = 0; i < 4; i++) {
int j = ourTurn ? i : i + 4;
ArrayList<Move.MoveAndBoard> amazonMoves = node.state.getNewBoard().getPossibleMoves(j);
for (Move.MoveAndBoard amazonMove : amazonMoves) {
children.add(new SearchNode(amazonMove,
new FunctionEvaluation(amazonMove.getNewBoard()).Evaluate()));
}
}
return children;
}
private Amazon checkForDireAmazon() {
Amazon dire = null;
for (int j = 0; j < 4; j++) {
if (board.getOurFreedom(board.Amazons[j]) <= 2) {
dire = board.Amazons[j];
}
}
return dire;
}
private Move.MoveAndBoard defaultHeuristic(ArrayList<Move.MoveAndBoard> moves) {
ArrayList<Integer> evaluations = new ArrayList<>(moves.size());
for (int i = 0; i < moves.size(); i++) {
evaluations.add(i, evaluateHeuristic(moves.get(i).getNewBoard()));
}
Integer max = Collections.max(evaluations);
return moves.get(evaluations.indexOf(max));
}
private Move.MoveAndBoard advancedHeuristic(ArrayList<Move.MoveAndBoard> moves) {
ArrayList<Double> evaluations = new ArrayList<>(moves.size());
for (int i = 0; i < moves.size(); i++) {
evaluations.add(i, new FunctionEvaluation(moves.get(i).getNewBoard()).Evaluate());
}
Double max = Collections.max(evaluations);
return moves.get(evaluations.indexOf(max));
}
private Move.MoveAndBoard superHeuristic(ArrayList<Move.MoveAndBoard> moves) {
ArrayList<Double> evaluations = new ArrayList<>(moves.size());
for (int i = 0; i < moves.size(); i++) {
evaluations.add(i, new WebEvaluation(moves.get(i).getNewBoard()).Evaluate());
}
Double max = Collections.max(evaluations);
return moves.get(evaluations.indexOf(max));
}
private Move.MoveAndBoard randomHeuristic(ArrayList<Move.MoveAndBoard> moves) {
ArrayList<Integer> evaluations = new ArrayList<>(moves.size());
for (int i = 0; i < moves.size(); i++) {
evaluations.add(i, evaluateRandomHeuristic(moves.get(i).getNewBoard()));
}
Integer max = Collections.max(evaluations);
return moves.get(evaluations.indexOf(max));
}
private int evaluateRandomHeuristic(GameBoard board) {
Random rand = new Random();
return rand.nextInt(10000);
}
private int evaluateHeuristic(GameBoard board) {
int sum = getSpaceConfiguration(board);
sum += getOurFreedom(board);
return sum;
}
/**
* Returns the score of the board as determined by freedom. For a single amazon their freedom is (8 - the number of arrows/queens beside them). This calculates the freedom of each of our amazons.
*
* @param board
* @return Returns the overall freedom of our amazons. The bigger the better.
*/
private int getOurFreedom(GameBoard board) {
return board.getOurFreedoms();
}
/**
* This will return how many of the spaces are ours minus the number of spaces that are theirs.
*
* @param board
* @return Number of spaces that are ours - spaces theirs. Larger the better
*/
private int getSpaceConfiguration(GameBoard board) {
//Set containing all the board squares movable to by our TestClass2.AI.AmazonUnit players.
HashSet<Integer> A;
A = board.ourSpaces();
//Set containing all the board squares movable to by the opponents players.
HashSet<Integer> B;
B = board.theirSpaces();
HashSet<Integer> C = new HashSet<>(A);
A.removeAll(B);
B.removeAll(C);
return A.size() - B.size();
}
public static class SearchNode {
final Move.MoveAndBoard state;
double heuristic;
public SearchNode(Move.MoveAndBoard state) {
this.state = state;
this.heuristic = 0;
}
public SearchNode(Move.MoveAndBoard state, double heuristic) {
this.state = state;
this.heuristic = heuristic;
}
public Move.MoveAndBoard getState() {
return this.state;
}
public double heuristic() {
return heuristic;
}
}
}