forked from otakaran/tic-tac-toe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ai.java
209 lines (191 loc) · 6.07 KB
/
Ai.java
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
205
206
207
208
209
import java.util.*;
/**
* The main AI for tic-tac-toe. Attempts to win my getting three in a row.
*
* @author Ping-Chun Chung and Otakar Andrysek
* @version v1.0.1
*/
public class Ai
{
// Generate a line to win
public String[] createLineArr(String[][] b)
{
String[] arr = new String[8];
for(int i=0; i<3; i++)
{
arr[i] = b[i][0]+b[i][1]+b[i][2];
arr[i+3] = b[0][i]+b[1][i]+b[2][i];
}
arr[6] = b[0][0]+b[1][1]+b[2][2];
arr[7] = b[0][2]+b[1][1]+b[2][0];
return arr;
}
// Find the next location to place symbol
public int[] absLocation(String[][] b)
{
String[] arr = new String[8];
int[] move = new int[2];
for(int i=0; i<8; i++)
{
arr[i] = createLineArr(b)[i];
if (countLetter(arr[i],"O") ==2 && countLetter(arr[i],"-")==1)
{
if (i<=2)
{
move[0] = i;
move[1] = arr[i].indexOf("-");
return move;
}
else if (i<=5)
{
move[0] = arr[i].indexOf("-");
move[1] = i-3;
return move;
}
else if (i==6)
{
move[0] = arr[i].indexOf("-");
move[1] = arr[i].indexOf("-");
return move;
}
else if (i==7)
{
move[0] = arr[i].indexOf("-");
move[1] = 2 - move[0];
return move;
}
}
}
return null;
}
// Calculated out the next move
public int calculatePoint(String[][] b, int firstPlay)
{
String[] arr = new String[8];
int totalPoint = 0;
for(int i=0; i<8; i++)
{
arr[i] = createLineArr(b)[i];
int x,o,e,point;
x = countLetter(arr[i],"X");
o = countLetter(arr[i],"O");
e = countLetter(arr[i],"-");
if (o == 3){
point = 500;
}
else if (o == 2 & x == 0){
point = 10;
}
else if (o == 1 && x == 0){
point = 1;
}
else if (x == 3){
point = -100;
}
else if (x == 2 && o == 0){
point = -50;
}
else if (x == 1 && o == 0){
point = -3;
}
else {
point = 0;
}
totalPoint+=point;
}
return totalPoint;
}
// Different logic for when game is nearing its end
public boolean almostLose(String[][] b, int y, int x)
{
String[] arr = new String[8];
String[][] newB = new String[3][3];
for (int i=0; i<3; i++)
{
for (int n=0; n<3; n++)
{
newB[i][n] = b[i][n];
}
}
newB[y][x] = "A";
for(int i=0; i<8; i++)
{
arr[i] = createLineArr(newB)[i];
if (countLetter(arr[i],"X") == 2 && countLetter(arr[i],"A") == 1)
return true;
}
return false;
}
public int countLetter(String str, String letter)
{
int counter = 0;
for (int i = 0; i < 3; i++) {
if (str.substring(i,i+1).equals(letter))
counter++;
}
return counter;
}
public int[] getMove(String[][] b, int firstPlay)
{
int maxPoint = -9999;
int point;
int newPoint;
int[] move = new int[2];
boolean boardEmpty = true;
String[][] newBoard = new String[3][3];
for (int i=0; i<3; i++)
{
for (int n=0; n<3; n++)
{
for (int ii=0; ii<3; ii++)
{
for (int nn=0; nn<3; nn++)
{
newBoard[ii][nn] = b[ii][nn];
if (!b[ii][nn].equals("-"))
boardEmpty = false;
}
}
if (newBoard[i][n].equals("-"))
{
newBoard[i][n] = "O";
point = calculatePoint(newBoard, firstPlay);
if (absLocation(newBoard) != null)
{
String[][] newNewBoard = new String[3][3];
int y,x;
for (int ii=0; ii<3; ii++)
{
for (int nn=0; nn<3; nn++)
{
newNewBoard[ii][nn] = newBoard[ii][nn];
}
}
y = absLocation(newBoard)[0];
x = absLocation(newBoard)[1];
newNewBoard[y][x] = "X";
newPoint = calculatePoint(newNewBoard, firstPlay);
if (newPoint < point && newPoint < -30)
point = newPoint;
}
if (almostLose(newBoard,i,n))
point = 300;
if (point > maxPoint)
{
maxPoint = point;
move[0] = i;
move[1] = n;
}
}
try{Thread.sleep(50);}catch(InterruptedException e){System.out.println(e);}
}
}
// We have nothing to work off of, let's just be random
if (boardEmpty)
{
move[0]=(int)(Math.random()*3);
move[1]=(int)(Math.random()*3);
}
return move;
}
}