/
ConnectFourAIPlayer.java
71 lines (64 loc) · 2.16 KB
/
ConnectFourAIPlayer.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
package aima.core.environment.connectfour;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import aima.core.search.adversarial.Game;
import aima.core.search.adversarial.IterativeDeepeningAlphaBetaSearch;
/**
* Implements an iterative deepening Minimax search with alpha-beta pruning and
* a special action ordering optimized for the Connect Four game.
*
* @author Ruediger Lunde
*/
public class ConnectFourAIPlayer extends
IterativeDeepeningAlphaBetaSearch<ConnectFourState, Integer, String> {
public ConnectFourAIPlayer(Game<ConnectFourState, Integer, String> game,
int time) {
super(game, 0.0, 1.0, time);
}
@Override
protected boolean isSignificantlyBetter(double newUtility, double utility) {
return newUtility - utility > (utilMax - utilMin) * 0.4;
}
@Override
protected boolean hasSafeWinner(double resultUtility) {
return Math.abs(resultUtility - (utilMin + utilMax) / 2) > 0.4
* utilMax - utilMin;
}
/**
* Modifies the super implementation by making safe winner values even more
* attractive if depth is small.
*/
@Override
protected double eval(ConnectFourState state, String player) {
double value = super.eval(state, player);
if (hasSafeWinner(value)) {
if (value > (utilMin + utilMax) / 2)
value -= state.getMoves() / 1000.0;
else
value += state.getMoves() / 1000.0;
}
return value;
}
/**
* Orders actions with respect to the number of potential win positions
* which profit from the action.
*/
@Override
public List<Integer> orderActions(ConnectFourState state,
List<Integer> actions, String player, int depth) {
List<Integer> result = actions;
if (depth == 0) {
List<ActionValuePair<Integer>> actionEstimates = new ArrayList<ActionValuePair<Integer>>(
actions.size());
for (Integer action : actions)
actionEstimates.add(ActionValuePair.createFor(action,
state.analyzePotentialWinPositions(action)));
Collections.sort(actionEstimates);
result = new ArrayList<Integer>();
for (ActionValuePair<Integer> pair : actionEstimates)
result.add(pair.getAction());
}
return result;
}
}