-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOthelloAI30.java
More file actions
176 lines (141 loc) · 7.56 KB
/
OthelloAI30.java
File metadata and controls
176 lines (141 loc) · 7.56 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
import java.util.*;
/**
* A more advanced OthelloAI-implementation. It implements minimax-algorithm with a pruning operation.
* The method to decide the next move takes favourable positions into account and responds in reasonable
* time when choosing out of all legal positions in a current GameState.
* @author Philine Zeinert, Jowita Julia Podolak, Kim Ida Schild
* @version 14.03.2019
*/
public class OthelloAI30 implements IOthelloAI {
//maxDepth sets the maximal depth until which states in the game tree will be expanded
private static final int maxDepth = 4;
/**
* Evaluates all legal positions at the current GameState and chooses the next possible
* position with the highest chance of winning for the AI according to the called helper methods.
* @param s The current GameState in which it is the AI's turn.
* @return An action (position), which serves as the AI's next move in the game.
*/
public Position decideMove(GameState s) {
int alpha = Integer.MIN_VALUE;
int beta = Integer.MAX_VALUE;
ArrayList<Position> legalPositions = s.legalMoves();
if (legalPositions.isEmpty()) return new Position(-1, -1);
int max = 0;
int i = 0;
Position maxPos = null;
for (Position position : legalPositions) {
i++;
int depth = 0;
// copy to maintain actual game state
GameState tmpState = new GameState(s.getBoard(), s.getPlayerInTurn());
tmpState.insertToken(position);//
int minValue = MinValue(tmpState, alpha, beta, depth);
if (minValue >= max) {
max = minValue;
maxPos = position;
}
}
return maxPos;
}
/**
* Iterates through every legal position in given GameState. Calls a MinValue for each position and
* chooses the highest possible value along the search tree. Updates alpha with a more favourable value
* at current GameState for Max, if there is such.
* @param s The current GameState in which it is the AI's turn.
* @param alpha current most favourable utility value for Max (AI)
* @param beta current most favourable utility value for opponent
* @param depth Depth in search tree when recursive call is supposed to stop to improve response time.
* @return The highest value for Max out of all legal moves at the given GameState.
*/
private int MaxValue(GameState s, int alpha, int beta, int depth) {
if (depth >= maxDepth || s.isFinished()) return Evaluation(s);
depth++;
ArrayList<Position> legalPositions = s.legalMoves();
int max = 0;
for (Position position : legalPositions) {
GameState tmpState = new GameState(s.getBoard(), s.getPlayerInTurn());
tmpState.insertToken(position);
int minValue = MinValue(tmpState, alpha, beta, depth);
if (minValue > max) max = minValue;
if (max >= beta) return max;
alpha = Math.max(alpha, max);
}
return max;
}
/**
* Iterates through every legal position in given GameState. Calls a MaxValue for each position and
* chooses the lowest possible value. Updates beta with a more favourable value
* for Min at current GameState, if there is such.
* @param s The current GameState in which it is the opponent's turn.
* @param alpha current most favourable utility value for AI
* @param beta current most favourable utility value for opponent
* @param depth Depth in search tree when recursive call is supposed to stop to improve response time.
* @return The lowest value for Max (AI) out of all legal moves at the given GameState.
*/
private int MinValue(GameState s, int alpha, int beta, int depth) {
if (depth >= maxDepth || s.isFinished()) return Evaluation(s);
depth++;
ArrayList<Position> legalPositions = s.legalMoves();
int min = Integer.MAX_VALUE;
for (Position position : legalPositions) {
GameState tmpState = new GameState(s.getBoard(), s.getPlayerInTurn());
tmpState.insertToken(position);
int maxValue = MaxValue(tmpState, alpha, beta, depth);
if (maxValue < min) min = maxValue;
if (min <= alpha) return min;
beta = Math.min(beta, min);
}
return min;
}
/**
* Assigns values to each position on the board.
* Evaluates the value of all tokens of player 2 according to an assigned value for each positions.
* @param s The current GameState.
* @return Utility value of all tokens for player 2 in given GameState.
*/
private int Evaluation(GameState s) {
int[][] currentBoard = s.getBoard();
int finalValue = 0;
for (int i = 0; i < currentBoard.length; i++) {
for (int j = 0; j < currentBoard.length; j++) {
if (currentBoard[i][j]==2) {
//corners
if (i == 0 && j == 0) finalValue = finalValue + 5;
else if (i == 0 && j == currentBoard.length - 1) finalValue = finalValue + 5;
else if (i == currentBoard.length - 1 && j == 0) finalValue = finalValue + 5;
else if (i == currentBoard.length - 1 && j == currentBoard.length - 1) finalValue = finalValue + 5;
//xs
if (currentBoard.length != 4) {
//left-upper corner
if (i == 0 && j == 1) finalValue = finalValue + 1;
else if (i == 1 && j == 1) finalValue = finalValue + 1;
else if (i == 1 && j == 0) finalValue = finalValue + 1;
//left-bottom corner
else if (i == currentBoard.length - 2 && j == 0) finalValue = finalValue + 1;
else if (i == currentBoard.length - 2 && j == 1) finalValue = finalValue + 1;
else if (i == currentBoard.length - 1 && j == 1) finalValue = finalValue + 1;
//right-bottom corner
else if (i == currentBoard.length - 1 && j == currentBoard.length - 2)
finalValue = finalValue + 1;
else if (i == currentBoard.length - 2 && j == currentBoard.length - 2)
finalValue = finalValue + 1;
else if (i == currentBoard.length - 2 && j == currentBoard.length - 1)
finalValue = finalValue + 1;
//right-upper corner
else if (i == 0 && j == currentBoard.length - 2) finalValue = finalValue + 1;
else if (i == 1 && j == currentBoard.length - 2) finalValue = finalValue + 1;
else if (i == 1 && j == currentBoard.length - 1) finalValue = finalValue + 1;
//sides
else if (i > 1 && i < currentBoard.length - 2 && j == 0) finalValue = finalValue + 4;
else if (i > 1 && i < currentBoard.length - 2 && j == currentBoard.length - 1)
finalValue = finalValue + 4;
else if (i == 0 && j > 1 && j < currentBoard.length - 2) finalValue = finalValue + 4;
else if (i == currentBoard.length - 1 && j > 1 && j < currentBoard.length - 2)
finalValue = finalValue + 4;
} else finalValue = finalValue + 2;
}
}
}
return finalValue;
}
}