-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniMax.java
More file actions
156 lines (138 loc) · 2.89 KB
/
Copy pathMiniMax.java
File metadata and controls
156 lines (138 loc) · 2.89 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
/**
* Class name: MiniMax
* Class to execute minimax algorithm on a tic tac toe board state
*
* @author Christian Autor
* @version 1.0
* @since 3/30/2021
*/
public class MiniMax
{
private final char BLANK_SPOT = '-';
private final char PLAYER_CHAR;
private final char COMPUTER_CHAR;
private final int PLAYERWIN = -1;
private final int COMPUTERWIN = 1;
private final int TIE = 0;
private char value;
/**
* MiniMax constructor
*
* @param none
* @return none
*/
public MiniMax()
{
PLAYER_CHAR = 'O';
COMPUTER_CHAR = 'X';
}
/**
* MiniMax constructor
*
* @param char p, char c
* @return none
*/
public MiniMax(char p, char c)
{
PLAYER_CHAR = p;
COMPUTER_CHAR = c;
}
/**
* miniMax method
*
* @param Board state, boolean MaxPlayer
* @return int
*/
public int miniMax(Board state, boolean MaxPlayer)
{
if(termTest(state))
{
if(value == COMPUTER_CHAR)
return COMPUTERWIN;
if(value == PLAYER_CHAR)
return PLAYERWIN;
return TIE;
}
else if(MaxPlayer)
{
int maxEval = Integer.MIN_VALUE;
for(int i = 0; i<9; i++)
{
if(state.getSpot(i) == BLANK_SPOT)
{
int eval = miniMax(state.returnMove(COMPUTER_CHAR,i), false);
if(eval > maxEval)
maxEval = eval;
}
}
return maxEval;
}
else
{
int maxEval = Integer.MAX_VALUE;
for(int i = 0; i<9; i++)
{
if(state.getSpot(i) == BLANK_SPOT)
{
int eval = miniMax(state.returnMove(PLAYER_CHAR,i), true);
if(eval < maxEval)
maxEval = eval;
}
}
return maxEval;
}
}
/**
* termTest method
*
* @param Board state
* @return boolean
*/
protected boolean termTest(Board state)
{
char centerSquare;
for(int i = 0; i<3; i++)
{
if(state.getSpot(i) != BLANK_SPOT && state.getSpot(i) == state.getSpot(i+3) && state.getSpot(i) == state.getSpot(i+6))
{
value = state.getSpot(i);
return true;
}
}
for(int i = 0; i<9; i+=3)
{
if(state.getSpot(i) != BLANK_SPOT && state.getSpot(i) == state.getSpot(i+1) && state.getSpot(i) == state.getSpot(i+2))
{
value = state.getSpot(i);
return true;
}
}
centerSquare = state.getSpot(4);
if(centerSquare != BLANK_SPOT && centerSquare == state.getSpot(0) && centerSquare == state.getSpot(8))
{
value = centerSquare;
return true;
}
else if(centerSquare != BLANK_SPOT && centerSquare == state.getSpot(6) && centerSquare == state.getSpot(2))
{
value = centerSquare;
return true;
}
else if(!state.contains(BLANK_SPOT))
{
value = BLANK_SPOT;
return true;
}
return false;
}
/**
* getValue method
*
* @param none
* @return char
*/
protected char getValue()
{
return value;
}
}