-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolveConnectFour.java
More file actions
254 lines (222 loc) · 7.04 KB
/
Copy pathSolveConnectFour.java
File metadata and controls
254 lines (222 loc) · 7.04 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import java.io.PrintWriter;
import java.io.File;
import java.util.ArrayList;
import java.awt.event.*;
import javax.swing.*;
public class SolveConnectFour implements ActionListener {
static PrintWriter output;
static int wins, losses, draws;
static CFPanel panel;
static Timer timer;
static int times;
public SolveConnectFour() {
timer = new Timer(1000, this);
timer.start();
try {
output = new PrintWriter(new File("ConnectFourSolution.txt"));
} catch (Exception e) {}
}
public void actionPerformed(ActionEvent e) {
times++;
}
public static void main(String[] args) throws Throwable {
SolveConnectFour scf = new SolveConnectFour();
char[][] board = new char[6][7];
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[i].length; j++)
board[i][j] = ' ';
solve(board, true, 0, 5, 0);
}
<<<<<<< HEAD
public static int[] solve(char[][] board, boolean turn, int turns, int x, int y) throws Throwable {
=======
// public static String[] solve(char[][] board, boolean turn, int turns, int x, int y) throws Throwable {
// char result = gameOver(board, x, y);
// // if (turns == 25) System.out.println(x + "\t" + y);
// // if (turns > 0) {
// // for (int i = 0; i < turns; i++) {
// //System.out.print(" ");
// // }
// //System.out.println("(" + x + ", " + y + ")");
// // }
// if (result == 'R' || result == 'Y') {
// // for (int i = 0; i < turns; i++) {
// //System.out.print(" ");
// // }
// //System.out.println(result + " wins");
// if (result == 'R') wins++;
// else losses++;
// return new String[] {result + "", ""};
// }
// if (turns == board.length * board[0].length) {
// draws++;
// return new String[] {result + "", ""};
// }
// String best;
// if (turn) best = "Y";
// else best = "R";
// int bestIndex = 0;
// String prevStr = "";
// for (int i = 0; i < board[0].length; i++) {
// int bottom;
// for (bottom = board.length - 1; bottom >= 0; bottom--) {
// if (board[bottom][i] == ' ') break;
// }
// if (bottom == -1) continue;
// if (turn) board[bottom][i] = 'R';
// else board[bottom][i] = 'Y'; // Thread.sleep(100);
// String[] codes = solve(board, !turn, turns + 1, bottom, i); // code?
// String code = codes[0];
// System.out.println(codes[0] + "\t" + codes[1]);
// board[bottom][i] = ' ';
// if (turn && code.charAt(0) < best.charAt(0)) {
// best = new String(code);
// bestIndex = i;
// prevStr = codes[1];
// }
// if (!turn && code.charAt(0) > best.charAt(0)) {
// best = new String(code);
// bestIndex = i;
// prevStr = codes[1];
// }
// if (turn && code.equals("R")) {
// prevStr = codes[1];
// return new String[] {"R", (i + 1) + "" + prevStr};
// }
// if (!turn && code == "Y") {
// prevStr = codes[1];
// return new String[] {"Y", (i + 1) + "" + prevStr};
// }
// }
// return new String[] {best, (bestIndex + 1) + "" + prevStr};
public static int[] solve(char[][] board, boolean turn, int turns, int x, int y) {
>>>>>>> origin/master
if (turns == board.length * board[0].length) {
draws++;
return new int[] {0, y};
}
char color = turn ? 'R':'Y';
int result = winningMove(board, color);
if (result >= 0)
if (turn) {
wins++;
return new int[] {1, result};
}
else {
losses++;
return new int[] {-1, result};
}
int bottom, outcome;
result = winningMove(board, turn ? 'Y':'R');
if (result >= 0) {
for (bottom = board.length - 1; bottom >= 0 && board[bottom][result] != ' '; bottom--);
board[bottom][result] = color;
outcome = solve(board, !turn, turns + 1, bottom, result)[0];
board[bottom][result] = ' ';
return new int[] {outcome, result};
}
int best = turn ? -1:1;
int bestMove = 0;
for (int i = 0; i < board[0].length; i++) {
if (board[0][i] != ' ')
continue;
for (bottom = board.length - 1; bottom >= 0 && board[bottom][i] != ' '; bottom--);
board[bottom][i] = color;
outcome = solve(board, !turn, turns + 1, bottom, i)[0];
board[bottom][i] = ' ';
if ((turn && outcome > best) || (!turn && outcome < best)) {
best = outcome;
bestMove = i;
if (best == 1 || best == -1)
return new int[] {best, bestMove};
}
}
return new int[] {best, bestMove};
}
public static int winningMove(char[][] tboard, char color) {
int bottom;
char result;
for (int i = 0; i < tboard[0].length; i++) {
if (tboard[0][i] != ' ')
continue;
for (bottom = tboard.length - 1; bottom >= 0 && tboard[bottom][i] != ' '; bottom--);
tboard[bottom][i] = color;
result = gameOver(tboard, bottom, i);
tboard[bottom][i] = ' ';
if (result == color)
return i;
}
return -1;
}
public static void printBoard(char[][] tboard) {
for (char[] row : tboard) {
for (char c : row)
System.out.print(c + " ");
System.out.println();
}
}
public static char gameOver(char[][] tboard, int x, int y) {
int countConsecutive = 0;
char color = ' ';
int i, a;
for (i = x - 3; i <= x + 3; i++) // Horizontal
if (i >= 0 && i < tboard.length && countConsecutive < 4)
if (tboard[i][y] == color)
countConsecutive++;
else if (tboard[i][y] == 'R' || tboard[i][y] == 'Y') {
color = tboard[i][y];
countConsecutive = 1;
}
else color = ' ';
else if (countConsecutive == 4)
return color;
if (countConsecutive == 4)
return color;
countConsecutive = 0;
color = ' ';
for (a = y - 3; a <= y + 3; a++) // Vertical
if (a >= 0 && a < tboard[0].length && countConsecutive < 4)
if (tboard[x][a] == color)
countConsecutive++;
else if (tboard[x][a] == 'R' || tboard[x][a] == 'Y') {
color = tboard[x][a];
countConsecutive = 1;
}
else color = ' ';
else if (countConsecutive == 4)
return color;
if (countConsecutive == 4)
return color;
countConsecutive = 0;
color = ' ';
for (i = x - 3, a = y - 3; i <= x + 3; i++, a++) // diagonal 1 topleft - bottomright
if (a >= 0 && a < tboard[0].length && i >= 0 && i < tboard.length && countConsecutive < 4)
if (tboard[i][a] == color)
countConsecutive++;
else if (tboard[i][a] == 'R' || tboard[i][a] == 'Y') {
color = tboard[i][a];
countConsecutive = 1;
}
else color = ' ';
else if (countConsecutive == 4)
return color;
if (countConsecutive == 4)
return color;
countConsecutive = 0;
color = ' ';
for (i = x - 3, a = y + 3; i <= x + 3; i++, a--) // diagonal 1 topright - bottomleft
if (a >= 0 && a < tboard[0].length && i >= 0 && i < tboard.length && countConsecutive < 4)
if (tboard[i][a] == color)
countConsecutive++;
else if (tboard[i][a] == 'R' || tboard[i][a] == 'Y') {
color = tboard[i][a];
countConsecutive = 1;
}
else color = ' ';
else if (countConsecutive == 4)
return color;
if (countConsecutive == 4)
return color;
return 'f';
}
}