-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
174 lines (154 loc) · 5.19 KB
/
Player.java
File metadata and controls
174 lines (154 loc) · 5.19 KB
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
import java.util.*;
public class Player {
int value_loss=-1000;
int value_win=1000;
int value_tie=0;
int boardSize=4;
public GameState play(final GameState gameState, final Deadline deadline) {
Vector<GameState> nextStates = new Vector<GameState>();
gameState.findPossibleMoves(nextStates);
if (nextStates.size() == 0) {
// Must play "pass" move if there are no other moves possible.
return new GameState(gameState, new Move());
}
int best_value_move=value_loss;
int tmp_value_move;
GameState best_move=nextStates.firstElement();
for(GameState tmp_state: nextStates){
if(gameState.getNextPlayer()==Constants.CELL_O){
tmp_value_move=-negaMax(tmp_state, 5,value_loss, value_win, -1);
}
else{
tmp_value_move=negaMax(tmp_state, 5,value_loss, value_win, -1);
}
if( tmp_value_move>=best_value_move){
if(tmp_state.isXWin()){
best_move=tmp_state;
break;
}
best_value_move=tmp_value_move;
best_move=tmp_state;
}
}
return best_move;
}
public int valueEnd(GameState game_state){
if(game_state.isXWin()){
return value_win;
}
else if(game_state.isOWin()){
return value_loss;
}
else{
return value_tie;
}
}
public int value(GameState game_state){
return valueRows(game_state, Constants.CELL_X)
+ valueColumns(game_state, Constants.CELL_X)
+valueDiagonals(game_state, Constants.CELL_X)
-valueRows(game_state, Constants.CELL_O)
-valueColumns(game_state, Constants.CELL_O)
-valueDiagonals(game_state, Constants.CELL_O);
}
/**
public int valueDiagonals(GameState game_state, int player){
return valueDiagonal(game_state, player, 0,-1)+
valueDiagonal(game_state, player, 3,1);
}
* */
public int valueDiagonals(GameState game_state, int player){
// 1 for go up and -1 for go down
int value_diagonals=0;
int tmp_value=0;
int cell;
for(int index=0;index<boardSize; index++){
cell=game_state.at(index, index);
if(cell==player){
tmp_value++;
}
else if(cell!= Constants.CELL_EMPTY){
tmp_value=0;
break;
}
}
value_diagonals+= tmp_value*tmp_value;
tmp_value=0;
for(int index=boardSize-1;index>-1; index--){
cell=game_state.at(index, index);
if(cell==player){
tmp_value++;
}
else if(cell!= Constants.CELL_EMPTY){
tmp_value=0;
break;
}
}
value_diagonals+= tmp_value*tmp_value;
return value_diagonals;
}
public int valueRows(GameState game_state, int player){
int tmp_value=0;
int tmp_value_row;
int cell;
for(int row=0; row<boardSize;row++){
tmp_value_row=0;
for(int col=0;col<boardSize;col++){
cell=game_state.at(row, col);
if(cell==player){
tmp_value_row++;
}
else if(cell!=Constants.CELL_EMPTY){
tmp_value_row=0;
break;
}
}
tmp_value+=tmp_value_row*tmp_value_row;
}
return tmp_value;
}
public int valueColumns(GameState game_state, int player){
int tmp_value=0;
int tmp_value_column;
int cell;
for(int col=0;col<boardSize;col++){
tmp_value_column=0;
for(int row=0;row<boardSize;row++){
cell=game_state.at(row, col);
if(cell==player){
tmp_value_column++;
}
else if(cell!=Constants.CELL_EMPTY){
tmp_value_column=0;
break;
}
}
tmp_value+=tmp_value_column*tmp_value_column;
}
return tmp_value;
}
public int negaMax(GameState gameState, int depth, int alpha, int beta,
int colour){
int tmpValue;
if(gameState.isEOG()){
return valueEnd(gameState)*colour;
}
else if(depth==0){
return value(gameState)*colour;
}
else{
Vector<GameState> lNextStates = new Vector<>();
gameState.findPossibleMoves(lNextStates);
int bestMove=-1000;
for(GameState childState:lNextStates){
tmpValue=-negaMax(childState, depth-1,-beta,-alpha, -colour);
bestMove=Math.max(bestMove, tmpValue);
alpha=Math.max(alpha, tmpValue);
if(alpha>=beta){
break;
}
}
return bestMove;
}
}
}