-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimax.java
171 lines (157 loc) · 7.11 KB
/
Minimax.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
package model.artificialintelligence;
import com.google.common.annotations.VisibleForTesting;
import model.board.Board;
import model.board.BoardUtils;
import model.board.Move;
import model.board.MoveTransition;
import java.util.concurrent.atomic.AtomicLong;
public class Minimax implements MoveStrategy {
private final BoardEvaluator evaluator;
private final int searchDepth;
private long numEvaluatedBoards;
private FreqTableRow[] freqTable;
private int freqTableIndex;
@VisibleForTesting
private boolean seeEvaluationDetails = false;
public Minimax(final int searchDepth) {
this.evaluator = StandardBoardEvaluator.get();
this.searchDepth = searchDepth;
this.numEvaluatedBoards = 0;
}
@Override
public String toString() {
return "Minimax";
}
@Override
public long getNumEvaluatedBoards() {
return this.numEvaluatedBoards;
}
@Override
public Move execute(Board board) {
final long startTime = System.currentTimeMillis();
Move optimalMove = Move.MoveFactory.getNullMove();
int highestSeenValue = Integer.MIN_VALUE;
int lowestSeenValue = Integer.MAX_VALUE;
int currentValue;
System.out.println(board.getCurrentPlayer() + " is thinking with a depth of " + searchDepth);
this.freqTable = new FreqTableRow[board.getCurrentPlayer().getValidMoves().size()];
this.freqTableIndex = 0;
int moveCounter = 1;
final int numMoves = board.getCurrentPlayer().getValidMoves().size();
for (final Move move : board.getCurrentPlayer().getValidMoves()) {
if (!move.isBannedRepetitiveMove()) {
final MoveTransition moveTransition = board.getCurrentPlayer().makeMove(move);
if (moveTransition.getMoveStatus().isDone()) {
final FreqTableRow row = new FreqTableRow(move);
this.freqTable[this.freqTableIndex] = row;
currentValue = board.getCurrentPlayer().getAllyColor().isBlue() ?
min(moveTransition.getToBoard(), this.searchDepth - 1) :
max(moveTransition.getToBoard(), this.searchDepth - 1);
System.out.println("\t" + this + " is analyzing move (" + moveCounter + "/" + numMoves + ") " + move +
" scores " + currentValue + " " + this.freqTable[this.freqTableIndex]);
this.freqTableIndex++;
if (board.getCurrentPlayer().getAllyColor().isBlue() && currentValue >= highestSeenValue) {
highestSeenValue = currentValue;
optimalMove = move;
} else if (board.getCurrentPlayer().getAllyColor().isRed() && currentValue <= lowestSeenValue) {
lowestSeenValue = currentValue;
optimalMove = move;
}
} else {
System.out.println(this + " is analyzing move (" + moveCounter + "/" + numMoves + ") " + move + " is illegal!");
}
moveCounter++;
}
}
final long executionTime = System.currentTimeMillis() - startTime;
System.out.printf("%s selects %s [#total boards evaluated = %d, time taken = %dms, evaluation rate = %.1fboards/second]\n",
board.getCurrentPlayer(), optimalMove, this.numEvaluatedBoards, executionTime, (1000 * ((double) this.numEvaluatedBoards / executionTime)));
long total = 0;
for (final FreqTableRow row : this.freqTable) {
if (row != null) {
total += row.getCount();
}
}
if (this.numEvaluatedBoards != total) {
System.out.println("Something is wrong with the # of boards evaluated!");
}
return optimalMove;
}
private int max(final Board board, final int depth) {
if (depth == 0) {
this.numEvaluatedBoards++;
this.freqTable[this.freqTableIndex].increment();
if (seeEvaluationDetails) {
System.out.println(StandardBoardEvaluator.get().evaluationDetails(board, depth));
}
return this.evaluator.evaluate(board, depth);
}
if (BoardUtils.isGameOverScenarioStandardConditions(board)) {
this.numEvaluatedBoards++;
this.freqTable[this.freqTableIndex].increment();
if (seeEvaluationDetails) {
System.out.println(StandardBoardEvaluator.get().evaluationDetails(board, depth));
}
return this.evaluator.evaluate(board, depth);
}
int highestSeenValue = Integer.MIN_VALUE;
for (final Move move : board.getCurrentPlayer().getValidMoves()) {
final MoveTransition moveTransition = board.getCurrentPlayer().makeMove(move);
if (moveTransition.getMoveStatus().isDone()) {
final int currentValue = min(moveTransition.getToBoard(), depth - 1);
if (currentValue >= highestSeenValue) {
highestSeenValue = currentValue;
}
}
}
return highestSeenValue;
}
private int min(final Board board, final int depth) {
if (depth == 0) {
this.numEvaluatedBoards++;
this.freqTable[this.freqTableIndex].increment();
if (seeEvaluationDetails) {
System.out.println(StandardBoardEvaluator.get().evaluationDetails(board, depth));
}
return this.evaluator.evaluate(board, depth);
}
if (BoardUtils.isGameOverScenarioStandardConditions(board)) {
this.numEvaluatedBoards++;
this.freqTable[this.freqTableIndex].increment();
if (seeEvaluationDetails) {
System.out.println(StandardBoardEvaluator.get().evaluationDetails(board, depth));
}
return this.evaluator.evaluate(board, depth);
}
int lowestSeenValue = Integer.MAX_VALUE;
for (final Move move : board.getCurrentPlayer().getValidMoves()) {
final MoveTransition moveTransition = board.getCurrentPlayer().makeMove(move);
if (moveTransition.getMoveStatus().isDone()) {
final int currentValue = max(moveTransition.getToBoard(), depth - 1);
if (currentValue <= lowestSeenValue) {
lowestSeenValue = currentValue;
}
}
}
return lowestSeenValue;
}
private static class FreqTableRow {
private final Move move;
private final AtomicLong count;
FreqTableRow(final Move move) {
this.count = new AtomicLong();
this.move = move;
}
long getCount() {
return this.count.get();
}
void increment() {
this.count.incrementAndGet();
}
@Override
public String toString() {
return BoardUtils.getPositionAtCoordinate(this.move.getCurrentCoordinate()) + " to " +
BoardUtils.getPositionAtCoordinate(this.move.getDestinationCoordinate()) + " with frequency of " + this.count;
}
}
}