-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimax.java
More file actions
166 lines (141 loc) · 6.26 KB
/
Minimax.java
File metadata and controls
166 lines (141 loc) · 6.26 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
/*
Thuật toán Minimax - cắt tỉa Alpha Beta
Chức năng các hàm:
- possibleMoves() : trả về danh sách các vị trí, tương ứng với các nước đi khả dĩ
(vì dùng vét cạn nên ta chỉ xét các nước đi có quân bên cạnh mà thôi)
- heuristic() : trả về điểm của bot so với player
- minimax() : chạy thuật toán Minimax, kèm cắt tỉa alpha-beta
- best_move() : lựa chọn trong các nước đi khả dĩ, nước nào có kết quả khả quan nhất
*/
package Caro;
import java.util.ArrayList;
import java.util.List;
public class Minimax {
private static int n = 15;
private static List<int[]> possibleMoves(int[][] position) {
List<int[]> possibleMoves = new ArrayList<>();
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(position[i][j] > 0) continue;
if(i > 0) {
if(j > 0) {
if(position[i-1][j-1] > 0 || position[i][j-1] > 0) {
int[] move = {i, j};
possibleMoves.add(move);
continue;
}
}
if(j < n-1) {
if(position[i-1][j+1] > 0 || position[i][j+1] > 0) {
int[] move = {i, j};
possibleMoves.add(move);
continue;
}
}
if(position[i-1][j] > 0) {
int[] move = {i, j};
possibleMoves.add(move);
continue;
}
}
if(i < n-1) {
if(j > 0) {
if(position[i+1][j-1] > 0 || position[i][j-1] > 0) {
int[] move = {i, j};
possibleMoves.add(move);
continue;
}
}
if(j < n-1) {
if(position[i+1][j+1] > 0 || position[i][j+1] > 0) {
int[] move = {i, j};
possibleMoves.add(move);
continue;
}
}
if(position[i+1][j] > 0) {
int[] move = {i, j};
possibleMoves.add(move);
continue;
}
}
}
}
return possibleMoves;
}
private static double heuristic(int[][] position, boolean player1Turn) {
double playerScore = Heuristic.getScore(position, true, player1Turn);
double botScore = Heuristic.getScore(position, false, player1Turn);
if(playerScore == 0) playerScore = 1.0;
return botScore / playerScore;
}
private static double minimax(int[][] position, int depth, double alpha, double beta, boolean player1Turn) {
if(depth == 0 || possibleMoves(position).size() == 0) {
return heuristic(position, !player1Turn);
}
if(player1Turn) {
double maxValue = -1;
for(int i = 0; i < possibleMoves(position).size(); i++) {
int[][] current_board = new int[n][n];
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
current_board[j][k] = position[j][k];
current_board[possibleMoves(position).get(i)[0]][possibleMoves(position).get(i)[1]] = 1;
double value = minimax(current_board, depth-1, alpha, beta, false);
alpha = Math.max(alpha, value);
if(value >= beta) return value;
maxValue = Math.max(value, maxValue);
}
return maxValue;
}
else {
double minValue = 10000000;
for(int i = 0; i < possibleMoves(position).size(); i++) {
int[][] current_board = new int[n][n];
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
current_board[j][k] = position[j][k];
current_board[possibleMoves(position).get(i)[0]][possibleMoves(position).get(i)[1]] = 2;
double value = minimax(current_board, depth-1, alpha, beta, true);
beta = Math.min(beta, value);
if(value <= alpha) return value;
minValue = Math.min(value, minValue);
}
return minValue;
}
}
public static int[] winning_move(int[][] position, boolean player1Turn) {
int[] location = new int[2];
for(int i = 0; i < possibleMoves(position).size(); i++) {
int[][] current_board = new int[n][n];
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
current_board[j][k] = position[j][k];
current_board[possibleMoves(position).get(i)[0]][possibleMoves(position).get(i)[1]] = player1Turn ? 1 : 2;
if(Heuristic.getScore(current_board, false, player1Turn) >= Heuristic.winScore) {
location = possibleMoves(position).get(i);
return location;
}
}
return null;
}
public static int[] best_move(int[][] position, boolean player1Turn) {
int[] location = new int[2];
double value = -1;
if(winning_move(position, player1Turn) != null) {
return winning_move(position, player1Turn);
}
for(int i = 0; i < possibleMoves(position).size(); i++) {
int[][] current_board = new int[n][n];
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
current_board[j][k] = position[j][k];
current_board[possibleMoves(position).get(i)[0]][possibleMoves(position).get(i)[1]] = 2;
if(minimax(current_board, 2, -1, Heuristic.winScore, false) >= value) {
value = minimax(current_board, 2, -1, Heuristic.winScore, false);
location = possibleMoves(position).get(i);
}
}
return location;
}
}