-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChineseCheckerDuckAgent.java
More file actions
116 lines (96 loc) · 4.74 KB
/
Copy pathChineseCheckerDuckAgent.java
File metadata and controls
116 lines (96 loc) · 4.74 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
import java.util.List;
public class ChineseCheckerDuckAgent implements BoardGameAgent{
final int maximumExpansionCount;
public ChineseCheckerDuckAgent(int maximumExpansionCount) {
this.maximumExpansionCount = maximumExpansionCount;
}
@Override
public int estimateDepth(BoardState boardState, Player player) {
int depth = 1;
while (estimateExpansionCount(boardState, depth, player) < maximumExpansionCount){ //depth starting from 1 and increasing until expansionCount exceeds...
depth++; //..the maxExpansionCount value for that depth
}
return depth-1; //returns the previous depth value, the last one before the limit
}
@Override
public long estimateExpansionCount(BoardState boardState, int m, Player player) {
if (m == 0)
return 1;
ChineseCheckerState chineseCheckerState = (ChineseCheckerState) boardState;
long numOfExpansions = 0;
int branchingFactor;
List<BoardState> successors = chineseCheckerState.getSuccessors(player); //get successors of the given state
branchingFactor = successors.size(); //estimate a branching factor
for (int i = 0; i <= m; i++) {
numOfExpansions += Math.pow(branchingFactor, i); //sum the number states for each depth
}
return numOfExpansions;
}
@Override
public double getUtility(BoardState boardState, Player player) {
ChineseCheckerState chineseCheckerState = (ChineseCheckerState) boardState;
double utility = 0;
for (int i = 0; i < chineseCheckerState.boardSize; i++) {
for (int j = 0; j < chineseCheckerState.boardSize; j++) {
if (chineseCheckerState.isInOpponentArea(player, i, j)){
continue;
}
Point2D point = new Point2D(i, j);
if(boardState.get(i, j) == player){
utility -= getDistanceToTarget(boardState, point, player);
}
else if (boardState.get(i, j) == player.getOpponent()) {
utility += getDistanceToTarget(boardState, point, player.getOpponent());
}
}
}
return utility;
}
private int getDistanceToTarget(BoardState boardState, Point2D point, Player player){
ChineseCheckerState state = (ChineseCheckerState) boardState;
int boardSize = ((ChineseCheckerState)boardState).boardSize;
int s = (boardSize - 1) / 2, minimumDistance = Integer.MAX_VALUE;
if (boardSize % 2 == 0) {
for (int y=0; y<s; y++) {
for (int x=0; x<s; x++) {
if (player == Player.One){
Point2D areaPoint = new Point2D(x, y);
if (Math.abs(point.x - areaPoint.x) + Math.abs(point.y - areaPoint.y) < minimumDistance){
minimumDistance = (int)(Math.abs(point.x - areaPoint.x) + Math.abs(point.y - areaPoint.y));
}
}
else if (player == Player.Two){
Point2D areaPoint = new Point2D(boardSize-x-1, boardSize-y-1);
if (Math.abs(point.x - areaPoint.x) + Math.abs(point.y - areaPoint.y) < minimumDistance){
minimumDistance = (int)(Math.abs(point.x - areaPoint.x) + Math.abs(point.y - areaPoint.y));
}
}
}
}
}
else
{
for (int y=0; y<=s; y++) {
for (int x=0; x<=y; x++) {
if (player == Player.One){
Point2D areaPoint = new Point2D(x, s-y);
if (Math.abs(point.x - areaPoint.x) + Math.abs(point.y - areaPoint.y) < minimumDistance){
minimumDistance = (int)(Math.abs(point.x - areaPoint.x) + Math.abs(point.y - areaPoint.y));
}
}
else if (player == Player.Two){
Point2D areaPoint = new Point2D(boardSize-x-1, boardSize-(s-y)-1);
if (Math.abs(point.x - areaPoint.x) + Math.abs(point.y - areaPoint.y) < minimumDistance){
minimumDistance = (int)(Math.abs(point.x - areaPoint.x) + Math.abs(point.y - areaPoint.y));
}
}
}
}
}
return minimumDistance;
}
@Override
public String toString() {
return "Duck Agent ";
}
}