-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.java
74 lines (59 loc) · 1.58 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
import java.lang.Math;
public class ai{
static boolean player1 = true;
int[][] board = new int[3][3];
ai[][] children = new ai[3][3];
int value;
boolean ismin;
public ai(int[][] board, boolean ismin)
{
this.ismin=ismin;
this.board = board;
}
int[][] copyBoard(){
int[][] copy=new int[3][3];
for(int i=0;i<copy.length;i++)
{
for (int j=0;j<copy[i].length;j++)
{
copy[i][j]=board[i][j];
}
}
return copy;
}
public int learn()
{
if(TicTacToe.hasWon(board))
{
value= ismin?1 :-1;
return value;
}
else if(TicTacToe.isFull(board))
{
value=0;
return value;
}
value=ismin?1:-1;
for (int i = 0; i < children.length; i++) {
for (int j = 0; j < children[i].length; j++) {
if (board[i][j] == 0) {
int[][] childField = copyBoard();
childField[i][j] = ismin ? 1 : 2;
children[i][j] = new ai(childField, !ismin);
value = ismin ? Math.min(value, children[i][j].learn()) : Math.max(value, children[i][j].learn());
}
}
}
return value;
}
public ai getChildWithValue() {
for (int i = 0; i < children.length; i++) {
for (int j = 0; j < children[i].length; j++) {
if (children[i][j] != null && children[i][j].value == value) {
return children[i][j];
}
}
}
return null;
}
}