-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.java
82 lines (75 loc) · 2.37 KB
/
state.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
import java.awt.Point;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
public class state
{
private final int MAX=Integer.MAX_VALUE;
private final int MIN=Integer.MIN_VALUE;
private int maxDepth;
Runtime r ;
public state(int maxDepth)
{
this.maxDepth=maxDepth;
r = Runtime.getRuntime();
r.gc();
}
public Move alpha_beta(othelo o)
{
Move m=max_Value(new othelo(o),MIN,MAX,0);
r.gc();
return m;
}
private Move max_Value(othelo o,int a,int b,int depth)
{
if((o.isTerminal())||(depth==maxDepth))
{
Move lastMove=new Move(o.getLastMove().getX(),o.getLastMove().getY(),o.evaluate());
r.gc();
return lastMove;
}
ArrayList<othelo> children =o.getChildren('W');
Move maxMove=new Move(MIN);
for(othelo child : children)
{
Move move=min_Value(child,a,b,depth+1);
if(move.getValue()>=maxMove.getValue())
{
if(move.getValue()>=b){return move;}
maxMove.setX(child.getLastMove().getX());
maxMove.setY(child.getLastMove().getY());
maxMove.setValue(move.getValue());
child=null;
r.gc();
}
a=Math.max(a,move.getValue());
}
return maxMove;
}
private Move min_Value(othelo o,int a,int b,int depth)
{
if((o.isTerminal())||(depth==maxDepth))
{
Move lastMove=new Move(o.getLastMove().getX(),o.getLastMove().getY(),o.evaluate());
r.gc();
return lastMove;
}
ArrayList<othelo> children =o.getChildren('B');
Move minMove=new Move(MAX);
for(othelo child : children)
{
Move move=max_Value(child,a,b,depth+1);
if(move.getValue()<=minMove.getValue())
{
if(move.getValue()<=a) {return move;}
minMove.setX(child.getLastMove().getX());
minMove.setY(child.getLastMove().getY());
minMove.setValue(move.getValue());
child=null;
r.gc();
}
b=Math.min(b,move.getValue());
}
return minMove;
}
}