-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniMax.java
More file actions
154 lines (132 loc) · 4.13 KB
/
Copy pathMiniMax.java
File metadata and controls
154 lines (132 loc) · 4.13 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
package application;
import java.util.ArrayList;
public class MiniMax {
public ArrayList<State> Successorfunction(State state) {// this method return all state that cane player play it
ArrayList<State> possibleMoves = new ArrayList<>();
int xMoves = 0;
int oMoves = 0;
String player;
for (int i = 0; i < state.getState().length; i++) {
String s = state.getState()[i];
if (s.equals("X")) {
xMoves++;
} else if (s.equals("O")) {
oMoves++;
}
}
if (xMoves <= oMoves) {
player = "X";
} else {
player = "O";
}
// Create all possible states if there are available moves
for (int i = 0; i <= 8; i++) {
if (!state.getStateIndex(i).equals("X") && !state.getStateIndex(i).equals("O")) {
String[] newState = state.getState().clone();
newState[i] = player;
possibleMoves.add(new State(i, newState));
}
}
return possibleMoves;
}
public int Decision(State state, char player) {//return optimal move
// First Player must be X
int bestVal;
if (player == 'X') {
bestVal = Integer.MIN_VALUE;
} else {
bestVal = Integer.MAX_VALUE;
}
int bestMove = 0;
for (State move : Successorfunction(state)) {
int moveVal;
if (player == 'X') {// Ai first
moveVal = minimize(move);
if (moveVal > bestVal) { // Minimize if AI played 1st
bestVal = moveVal;
bestMove = move.getPosition();
}
} else {// user
moveVal = maximize(move); // Maximize if Ai played 2nd
if (moveVal < bestVal) {
bestVal = moveVal;
bestMove = move.getPosition();
}
}
}
return bestMove;
}
public int maximize(State state) {// Maximizes the score
if (isTerminal(state)) {
return Autilityfunction(state);
}
int bestScore = Integer.MIN_VALUE;
for (State move : Successorfunction(state)) {
bestScore = Math.max(bestScore, minimize(move));
}
return bestScore;
}
public int minimize(State state) {// Minimizes the score
if (isTerminal(state)) {
return Autilityfunction(state);
}
int bestScore = Integer.MAX_VALUE;
for (State move : Successorfunction(state)) {
bestScore = Math.min(bestScore, maximize(move));
}
return bestScore;
}
public boolean isTerminal(State state) {// this method return true if either AI or Player win || all spots are
// reserved (draw)
int Reservedspots = 0;
for (int a = 0; a <=8; a++) {
if (state.getStateIndex(a).equals("X") || state.getStateIndex(a).equals("O")) {
Reservedspots++;
}
String line = checkState(state, a); // 012
// Check for Winners
if (line.equals("XXX")) {
return true;
} else if (line.equals("OOO")) {
return true;
}
if (Reservedspots == 9) {
return true;
}
}
return false;
}
public int Autilityfunction(State state) {
for (int a = 0; a < 8; a++) {
String line = checkState(state, a);
// Check for Winners
if (line.equals("XXX")) {
return 1;
} else if (line.equals("OOO")) {
return -1;
}
}
return 0;
}
public String checkState(State state, int a) {//Check if the case is a winner
if (a == 0) {
return state.getStateIndex(0) + state.getStateIndex(1) + state.getStateIndex(2);
} else if (a == 1) {
return state.getStateIndex(3) + state.getStateIndex(4) + state.getStateIndex(5);
} else if (a == 2) {
return state.getStateIndex(6) + state.getStateIndex(7) + state.getStateIndex(8);
} else if (a == 3) {
return state.getStateIndex(0) + state.getStateIndex(3) + state.getStateIndex(6);
} else if (a == 4) {
return state.getStateIndex(1) + state.getStateIndex(4) + state.getStateIndex(7);
} else if (a == 5) {
return state.getStateIndex(2) + state.getStateIndex(5) + state.getStateIndex(8);
} else if (a == 6) {
return state.getStateIndex(0) + state.getStateIndex(4) + state.getStateIndex(8);
} else if (a == 7) {
return state.getStateIndex(2) + state.getStateIndex(4) + state.getStateIndex(6);
} else {
return "";
}
}
}