-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniMaxAgent.java
138 lines (87 loc) · 3.37 KB
/
MiniMaxAgent.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
import java.util.ArrayList;
public class MiniMaxAgent {
int depth;
ArrayList<Integer> x;
public MiniMaxAgent(int depth){
this.depth = depth;
}
public void setDepth(int depth) {
this.depth = depth;
}
// public void testDepth() {
// if(this.depth == 2) {
// System.out.println("Defence");
// }
// if(this.depth == 3) {
// System.out.println("Attack");
// }
// }
//--------------------------------------------------------------------------------------------------// start Minimax algorithm with pruning
public Board Action (Board board) throws CloneNotSupportedException{
int alpha = -999999, beta = 999999;
int val = max(board, depth, alpha, beta,0 ,0);
ArrayList<ArrayList<Integer>> children = new ArrayList<ArrayList<Integer>>();
children = board.getChildren();
board = board.generateSuccessor('X',x.get(0),x.get(1));
System.out.println("-------- AI -------");
board.printBoard();
System.out.println("AI move: " + ((char)(x.get(0)+97)) + (x.get(1)+1));
System.out.println();
return board;
}
//--------------------------------------------------------------------------------------------------// end Minimax algorithm with pruning
//--------------------------------------------------------------------------------------------------// algorithm "MAX" section
public int max(Board board, int depth, int alpha, int beta, int next_X, int next_Y) throws CloneNotSupportedException {
ArrayList<ArrayList<Integer>> children = new ArrayList<ArrayList<Integer>>();
if(depth == 0) {
return board.evaluation(next_X, next_Y);
}
children = board.getChildren();
int value = -999999;
for(int i =0; i<children.size();i++)
{
int min_val = min(board.generateSuccessor('X',children.get(i).get(0),children.get(i).get(1)), depth-1, alpha, beta, children.get(i).get(0), children.get(i).get(1));
if(value < min_val)
{
value = min_val;
this.x = children.get(i);
}
if(value > alpha) {
alpha = value;
}
if(alpha >= beta) {
break;
}
}
return value;
}
//--------------------------------------------------------------------------------------------------// end algorithm "MAX" section
//--------------------------------------------------------------------------------------------------// algorithm "MIN" section
public int min(Board board, int depth, int alpha, int beta, int next_X, int next_Y) throws CloneNotSupportedException {
ArrayList<ArrayList<Integer>> children = new ArrayList<ArrayList<Integer>>();
if(depth == 0) {
return board.evaluation(next_X, next_Y);
}
children = board.getChildren();
int value = 999999;
for(int i =0; i<children.size();i++)
{
int max_val = max(board.generateSuccessor('O',children.get(i).get(0),children.get(i).get(1)), depth-1, alpha, beta, children.get(i).get(0), children.get(i).get(1));
if(max_val < value)
{
value = max_val;
}
if(value < beta) {
beta = value;
}
if(alpha >= beta) {
break;
}
}
return value;
}
//--------------------------------------------------------------------------------------------------// end algorithm "MIN" section
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//End.