-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiPlayer.java
164 lines (110 loc) · 3.43 KB
/
aiPlayer.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
import java.awt.Point;
import java.util.*;
public class aiPlayer extends player {
static Point helperPoint=new Point();
static Point bestPoint;
private valueOfMove bestValue=new valueOfMove();
public aiPlayer(){
}
public aiPlayer(board _board, String _playerName, char _playerSymbol){
super(_board, _playerName, _playerSymbol);
}
public Point doPlayerTurn(player player){
System.out.print("Enter your next move player "+getPlayerName()+" : ");
int col;
int row;
Point move=new Point();
valueOfMove a=new valueOfMove(helperPoint, -1000000000);
valueOfMove b=new valueOfMove(helperPoint, 1000000000);
long startTime=System.currentTimeMillis();
if(board.getEmptySpaces()>48){
move=minimax(board, 1,true,a,b).getMove();
}
else if(board.getEmptySpaces()>22){
move=minimax(board, 3,true,a,b).getMove();
}
else if(board.getEmptySpaces()>12){
move=minimax(board, 4,true,a,b).getMove();
}
else{
move=minimax(board, 5,true,a,b).getMove();
}
long endTime=System.currentTimeMillis();
System.out.println("Time: "+(endTime-startTime)/1000.0+" seconds");
col=move.x;
row=move.y;
Point disc=new Point(row, col);
return disc;
}
public ArrayList<board> allMoves(board _board, String _symbol){
ArrayList<board> allMoves = new ArrayList<board>();
for(int i=board.getBoardRows()-1 ; i>=1 ; i--){
for(int x=i ; x<=board.getBoardCols()-i ; x++){
board tempBoard=new board(_board.getBoard());
if(tempBoard.setPosition(i,x,_symbol)){
allMoves.add(tempBoard);
}
}
}
//Collections.shuffle(allMoves);
return allMoves;
}
public valueOfMove minimax(board _board,int _depth, boolean max, valueOfMove a, valueOfMove b){
int depth=_depth;
if(depth>board.getEmptySpaces()){
depth=board.getEmptySpaces();
}
if(depth==0){
if(max==false){
valueOfMove bestValue2=new valueOfMove(_board.getBoardPosition(),winning.heuristic1(String.valueOf(getPlayerSymbol()), String.valueOf(getOpposingPlayerSymbol()),_board));
return bestValue2;
}
else{
valueOfMove bestValue2=new valueOfMove(_board.getBoardPosition(),winning.heuristic1( String.valueOf(getOpposingPlayerSymbol()),String.valueOf(getPlayerSymbol()),_board));
return bestValue2;
}
}
//max player turn
if(max==true){
//valueOfMove bestValue=new valueOfMove();
bestValue.setValue(Integer.MIN_VALUE);
bestValue.setMove(helperPoint);
valueOfMove v=new valueOfMove();
for(board x:allMoves(_board, String.valueOf(getPlayerSymbol()))){
v=minimax(x,depth-1, false, a, b);
if(v.getValue()>bestValue.getValue()){
bestValue=v;
}
valueOfMove a2=a;
if(bestValue.getValue()>a2.getValue()){
a2=new valueOfMove(v);
}
if(b.getValue()<=a2.getValue()){
break;
}
}
return bestValue;
}
//min player turn
else{
//valueOfMove bestValue=new valueOfMove();
bestValue.setValue(Integer.MAX_VALUE);
bestValue.setMove(helperPoint);
valueOfMove v=new valueOfMove();
for(board x:allMoves(_board, String.valueOf(getOpposingPlayerSymbol()))){
v=minimax(x,depth-1, true,a,b);
if(v.getValue()<bestValue.getValue()){
bestValue=v;
}
valueOfMove b2=b;
if(bestValue.getValue()<b2.getValue()){
b2=new valueOfMove(v);
}
if(b2.getValue()<=a.getValue()){
break;
}
}
return bestValue;
}
}
}