-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.java
236 lines (217 loc) · 7.73 KB
/
engine.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import java.util.*;
public class engine{
public static int botSide = 2;
public static int depth = 5;
public static int turn = 1;
public static int[][][] board = new int[4][4][4];
public engine(){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
for(int k = 0; k < 4; k++){
board[i][j][k] = 0;
}
}
}
}
public static void playMove(int x, int y){
board[getLowest(x, y)][x][y] = turn;
turn = 3 - turn;
}
public static void undo(int x, int y){
board[getLowest(x, y)-1][x][y] = 0;
turn = 3 - turn;
}
public static int getLowest(int x, int y){
for(int i = 0; i < 4; i++){
if(board[i][x][y] == 0){
return i;
}
}
return 4;
}
public static boolean isValidMove(int x, int y){
return (getLowest(x, y) < 4);
}
public static ArrayList<int[][]> getSlices(int[][][] currentBoard){ //returns all 2D slices of the board
ArrayList<int[][]> slices = new ArrayList<int[][]>();
// horizontal
for (int d = 0; d < 4; d++) {
int[][] slice = new int[4][4];
for (int h = 0; h < 4; h++) {
for (int w = 0; w < 4; w++) {
slice[h][w] = board[h][d][w];
}
}
slices.add(slice);
}
// other
for (int w = 0; w < 4; w++) {
int[][] slice = new int[4][4];
for (int h = 0; h < 4; h++) {
for (int d = 0; d < 4; d++) {
slice[h][d] = board[h][d][w];
}
}
slices.add(slice);
}
// diagonal
for (int s = 0; s < 2; s++) {
int[][] slice = new int[4][4];
for (int h = 0; h < 4; h++) {
for (int i = 0; i < 4; i++) {
slice[h][i] = board[h][i][(s == 0) ? i : 3 - i];
}
}
slices.add(slice);
}
return slices;
}
public static int colRowStreaks(int[][] slice, int length, int side){ //returns the number of unblocked streaks of length n on rows or columns
int count = 0;
for(int i = 0; i < 4; i++){
boolean col = true;
int colC = 0;
boolean row = true;
int rowC = 0;
for(int j = 0; j < 4; j++){
// System.out.print(slice[i][j] + " "); //check
if(slice[i][j] == side){
colC += 1;
}
else if(slice[i][j] == 3 - side){
col = false;
}
if(slice[j][i] == side){
rowC += 1;
}
else if(slice[j][i] == 3 - side){
row = false;
}
}
if(row && rowC == length){
count += 1;
}
if(col && colC == length){
count += 1;
}
rowC = 0;
colC = 0;
}
return count;
}
public static int diagStreaks(int[][] slice, int length, int side){ //returns the number of unblocked streaks of length n on diagonals
int count = 0;
boolean d1 = true;
int d1c = 0;
boolean d2 = true;
int d2c = 0;
for(int i = 0; i < 4; i ++){
if(slice[i][i] == side){
d1c += 1;
}
else if(slice[i][i] == 3 - side){
d1 = false;
}
if(slice[i][3-i] == side){
d2c += 1;
}
else if(slice[i][3-i] == 3 - side){
d2 = false;
}
}
if(d1 && d1c == length){
count += 1;
}
if(d2 && d2c == length){
count += 1;
}
return count; //some double counting going on here potentially
}
public static int numImportant(int[][][] currentBoard, int side){
int count = 0;
int[][] locations = new int[][]{new int[]{0, 0, 0}, new int[]{0, 0, 3}, new int[]{0, 3, 0}, new int[]{0, 3, 3}};
for(int i = 0; i < locations.length; i++){
if(currentBoard[locations[i][0]][locations[i][1]][locations[i][2]] == side){
count++;
}
}
return count;
}
public static double scoreBoard(){ //Scoring created through personal experience playing the game
double scoreUs = 0;
double scoreThem = 0;
ArrayList<int[][]> slices = getSlices(board);
for(int[][] slice : slices){
if(diagStreaks(slice, 4, botSide) > 0 || colRowStreaks(slice, 4, botSide) > 0){
return Double.POSITIVE_INFINITY;
}
if(diagStreaks(slice, 4, 3-botSide) > 0 || colRowStreaks(slice, 4, 3-botSide) > 0){
return Double.NEGATIVE_INFINITY;
}
}
for(int[][] slice : slices)
for(int i = 2; i <= 4; i++){
scoreUs += Math.pow(colRowStreaks(slice, i, botSide), i);
scoreUs += Math.pow(diagStreaks(slice, i, botSide), i);
scoreThem += Math.pow(colRowStreaks(slice, i, 3 - botSide), i);
scoreThem += Math.pow(diagStreaks(slice, i, 3 - botSide), i);
}
//Important locations should be given a bias
scoreUs += 10*numImportant(board, botSide);
scoreThem += 10*numImportant(board, 3- botSide);
return scoreUs - scoreThem;
}
// calculate best move using minimax
public static double[] bestMove(boolean maximizingPlayer, int currentDepth, int maxDepth){
double score = scoreBoard();
if(score == Double.POSITIVE_INFINITY){
return new double[]{score};
}
else if(score == Double.NEGATIVE_INFINITY){
return new double[]{score};
}
if(currentDepth == maxDepth){
return new double[]{score};
}
if(maximizingPlayer){
double bestValue = Double.NEGATIVE_INFINITY;
double[] bestMove = new double[3];
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if(isValidMove(i, j)){
playMove(i, j);
double val = bestMove(false, currentDepth + 1, maxDepth)[0];
if(val >= bestValue){
bestValue = val;
bestMove[0] = val;
bestMove[1] = i;
bestMove[2] = j;
}
undo(i, j);
}
}
}
return bestMove;
}
else{
double bestValue = Double.POSITIVE_INFINITY;
double[] bestMove1 = new double[3];
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if(isValidMove(i, j)){
playMove(i, j);
double val = bestMove(true, currentDepth + 1, maxDepth)[0];
if(val <= bestValue){
bestValue = val;
bestMove1[0] = val;
bestMove1[1] = i;
bestMove1[2] = j;
}
undo(i, j);
}
}
}
return bestMove1;
}
}
}