-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOthello.java
executable file
·134 lines (113 loc) · 4.1 KB
/
Othello.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
import java.util.ArrayList;
/**
*
* @author leyantong
*/
public class Othello {
public static final int SQUARESIZE= 60; // Basic dimensions of board
public static final double PIECERATIO= 0.4; // ration of radius of piece to square size
public static final int xBOARDpos= 100; // Position of board
public static final int yBOARDpos= 100; // Position of board
public static final int xMARGIN= 50; // Position of board
public static final int yMARGIN= 50; // Position of board
public static final int searchDepth= 8; // Depth of minimax search
public static int mat[][] = { {120, -20, 20, 5, 5, 20, -20, 120},
{-20, -40, -5, -5, -5, -5, -40, -20},
{20, -5, 15, 3, 3, 15, -5, 20},
{5, -5, 3, 3, 3, 3, -5, 5},
{5, -5, 3, 3, 3, 3, -5, 5},
{20, -5, 15, 3, 3, 15, -5, 20},
{-20, -40, -5, -5, -5, -5, -40, -20},
{120, -20, 20, 5, 5, 20, -20, 120},
};
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
BoardState initialState= new BoardState();
initialState.setContents(3, 3, 1);
initialState.setContents(3, 4, -1);
initialState.setContents(4, 3, -1);
initialState.setContents(4, 4, 1);
initialState.colour= -1; // Black to start
OthelloDisplay othelloDisplay= new OthelloDisplay();
othelloDisplay.boardState= initialState;
othelloDisplay.repaint();
}
public static Move chooseMove(BoardState boardState){
ArrayList<Move> moves= boardState.getLegalMoves();
if(moves.isEmpty())
return null;
// participant version: replace this line with the following code
// and provide the routines as directed in the lab exercise script.
// return moves.get(0);
return toplevel(boardState, moves);
}
public static int staticEvaluation(BoardState boardState){
int value = 0;
for(int i=0; i<8; i++){
for(int j=0; j<8;j++){
if(boardState.getContents(i, j)==1){
value+=mat[i][j];
}
else if(boardState.getContents(i,j)==-1){
value-=mat[i][j];
}
}
}
return value;
}
public static int miniMax(BoardState boardState, int searchDepth, int alpha, int beta, Boolean max_player){
ArrayList<Move> move = boardState.getLegalMoves();
if(searchDepth==0){
return staticEvaluation(boardState);
}
else if(max_player){
alpha = Integer.MIN_VALUE;
for(int i =0 ; i<move.size();i++){
if(alpha >= beta||move.isEmpty()){
break;
}
BoardState temp = boardState.deepCopy();
temp.makeLegalMove(move.get(i).x, move.get(i).y);
int miniVal = miniMax(temp, searchDepth-1, alpha, beta, false);
if(alpha<miniVal){
alpha=miniVal;
}
}
return alpha;
}
else{
beta=Integer.MAX_VALUE;
for(int i = 0;i<move.size();i++){
if(alpha >= beta||move.isEmpty()){
break;
}
BoardState temp = boardState.deepCopy();
temp.makeLegalMove(move.get(i).x, move.get(i).y);
int maxVal = miniMax(temp, searchDepth-1, alpha, beta, true);
if(maxVal<beta){
beta=maxVal;
}
}
return beta;
}
}
public static Move toplevel(BoardState bs, ArrayList<Move> move){
int valueIndex = 0;
int a = Integer.MIN_VALUE;
int b = Integer.MAX_VALUE;
Move bestmove = null;
for(int i=0; i<move.size();i++){
BoardState temp = bs.deepCopy();
temp.makeLegalMove(move.get(i).x, move.get(i).y);
int minimax = miniMax(temp, searchDepth-1, a, b, false);
if(a<minimax){
a=minimax;
valueIndex = i;
}
}
bestmove = move.get(valueIndex);
return bestmove;
}
}