-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIEngine.java
More file actions
204 lines (176 loc) · 6.37 KB
/
AIEngine.java
File metadata and controls
204 lines (176 loc) · 6.37 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
package api;
import boards.TicTacToeBoard;
import game.*;
import stateManager.DefensivePlacement;
import stateManager.OffensivePlacement;
import stateManager.Placement;
import java.util.Optional;
public class AIEngine {
private RuleEngine ruleEngine;
public Cell getBasicMove(Player player, TicTacToeBoard board){
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board.getSymbol(i,j)==null){
return new Cell(i,j);
}
}
}
return null;
}
private Cell offense(Player player, TicTacToeBoard board) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board.getSymbol(i,j)==null){
Move move = new Move(player.flip(), new Cell(i,j));
Board boardCopy = board.dummyMove(move);
if(ruleEngine.getState(boardCopy).isOver()){
return new Cell(i,j);
}
}
}
}
return null;
}
private Cell defense(Player player, TicTacToeBoard board) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board.getSymbol(i, j) == null) {
Move move = new Move(player.flip(), new Cell(i,j));
Board boardCopy = board.dummyMove(move);
if (ruleEngine.getState(boardCopy).isOver()) {
return new Cell(i,j);
}
}
}
}
return null;
}
public Cell getSmartMove(Player player, TicTacToeBoard board){
// Victorious Move = We found a move where Computer Wins
Cell defense = defense(player,board);
if(defense!=null) return defense;
Cell offense = offense(player,board);
if(offense!=null) return offense;
return getBasicMove(player, board);
}
public Cell getOptimalMove(Player player, TicTacToeBoard board){
Placement placement = DefensivePlacement.get();
while (placement.next()!=null){
Optional<Cell> place = placement.place(board,player);
if(place.isPresent()){
return place.get();
}
placement = placement.next();
}
return getBasicMove(player, board);
}
abstract static class State {
protected Player player;
protected TicTacToeBoard board;
protected int threshold;
public State(Player player, TicTacToeBoard board, int threshold) {
this.player = player;
this.board = board;
this.threshold = threshold;
}
abstract Cell placeCell(); // Determine the move
abstract State next(); // Determine the next state
}
// Opening state: Moves based on Basic Strategy
class Opening extends State {
public Opening(Player player, TicTacToeBoard board, int threshold) {
super(player, board, threshold);
}
@Override
Cell placeCell() {
return getBasicMove(player, board);
}
@Override
State next() {
int moveCount = countMoves(board);
if (moveCount < threshold) {
return this; // Remain in Opening
} else if (moveCount < threshold + 1) {
return new MidGame(player, board, threshold);
} else {
return new EndGame(player, board, threshold);
}
}
}
// MidGame state: Moves based on Smart Strategy
class MidGame extends State {
public MidGame(Player player, TicTacToeBoard board, int threshold) {
super(player, board, threshold);
}
@Override
Cell placeCell() {
return getSmartMove(player, board);
}
@Override
State next() {
int moveCount = countMoves(board);
if (moveCount < threshold) {
return new Opening(player, board, threshold);
} else if (moveCount < threshold + 1) {
return this; // Remain in MidGame
} else {
return new EndGame(player, board, threshold);
}
}
}
// EndGame state: Moves based on Optimal Strategy
class EndGame extends State {
public EndGame(Player player, TicTacToeBoard board, int threshold) {
super(player, board, threshold);
}
@Override
Cell placeCell() {
return getOptimalMove(player, board);
}
@Override
State next() {
return this; // Stay in EndGame
}
}
public Move suggestMove(Player player, Board board){
if(board instanceof TicTacToeBoard board1){
Cell suggestedMove = null;
int threshold = 2;
State state;
// Determine initial state based on countMoves()
int moveCount = countMoves(board1);
if (moveCount < threshold) {
state = new Opening(player, board1, threshold);
} else if (moveCount < threshold + 1) {
state = new MidGame(player, board1, threshold);
} else {
state = new EndGame(player, board1, threshold);
}
// Get the suggested move based on the current state
suggestedMove = state.placeCell();
// if(countMoves(board1) < threshold){
// suggestedMove = getBasicMove(player,board1);
// } else if(countMoves(board1)< threshold + 1) {
// suggestedMove = getSmartMove(player,board1);
// } else{
// suggestedMove = getOptimalMove(player, board1);
// }
//NOTE: By using State Design pattern also, we violates OCP, because we don't have clear state transition
if(suggestedMove != null) return new Move(player,suggestedMove);
throw new IllegalStateException();
} else{
throw new IllegalArgumentException();
}
}
public int countMoves(TicTacToeBoard board) {
int count = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board.getSymbol(i, j) != null) {
count++;
}
}
}
return count;
}
}