-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFour.java
More file actions
449 lines (421 loc) · 15.6 KB
/
ConnectFour.java
File metadata and controls
449 lines (421 loc) · 15.6 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/**
* Brock Francom
* A02052161
* CS-2410
* Andrew Brim
* 3/2/2019
*
* Programming Exercise 4 - JavaFX GUIs
*
* Program to play connect 4
*
* Note Although my program works correctly, my logic is kinda complicated,
* if I was to redo this, I would probably use a 2d
* array and have simpler logic.
*/
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.ArrayList;
import java.util.List;
public class ConnectFour extends Application {
private Color player1 = Color.RED;
private Color player2 = Color.PURPLE;
private Boolean player1Turn = true;
private Pane pane = new Pane();
private Scene scene = new Scene(pane, 900, 800);
private final List<Circle> circles = new ArrayList<>();
private boolean flashVariable = true; //Used to control what color is flashed
private List<Circle> winning = new ArrayList<>(); // used in the recursive functions.
public static void main(String[]args) {
launch(args);
}
@Override
public void start(Stage primaryStage){
primaryStage.setTitle("Connect Four Game");
makeBoard();
primaryStage.setScene(scene);
primaryStage.show();
}
// This creates a board and a list of circles and displays them.
private void makeBoard() {
Rectangle board = new Rectangle(700, 600, Color.BLUE);
board.setX(100);
board.setY(100);
pane.getChildren().add(board);
for (var i=0;i<6;i++) {
for (var j=0; j<7; j++) {
Circle c = new Circle(50, Color.WHITE);
c.setCenterY(((i *100)+150));
c.setCenterX(((j *100)+150));
c.setOnMouseClicked(e -> makeMove(c));
circles.add(c);
pane.getChildren().add(c);
}
}
}
// This code makes sure the clicked circle is valid, then sets it's color, then checks if it is a win.
private void makeMove(Circle c) {
if(isValid(c) && player1Turn) {
c.setFill(player1);
isWinner(c, player1);
player1Turn = false;
}
else if (isValid(c)) {
c.setFill(player2);
isWinner(c, player2);
player1Turn = true;
}
}
// Logic for a valid move
private boolean isValid(Circle c) {
if (c.getFill() == Color.WHITE){
int index = circles.indexOf(c);
if (index >= 35){
return true;
}
else return circles.get(index + 7).getFill() != Color.WHITE;
}
return false;
}
// Checks if winner. Program terminates in the function that returns true if a winner is found
private void isWinner(Circle c, Color playerColor) {
if (checkVertical(c, playerColor) ||
checkRight(c, playerColor) ||
checkLeft(c, playerColor) ||
checkLeftAndRight(c, playerColor, 0) ||
checkDiagonalRight(c, playerColor, 0) ||
checkDiagonalLeft(c, playerColor, 0)) {
// Do nothing, program terminates in the function that won. This was used to print out messages during development,
// if you want the program to print winner, uncomment the line below.
// System.out.println("Winner!!!");
}
}
// This checks left and right, and will return true if the last piece in a horizontial 4 in a row is in the middle.
private boolean checkLeftAndRight(Circle c, Color playerColor, int depth) {
if (depth == 0) { // Resets the winning list on the first call of the function
winning = new ArrayList<>();
}
if (depth >4) { // Makes sure there is no infinite recursion
return false;
}
int index = circles.indexOf(c);
int row = circles.indexOf(c)/7;
winning.add(c);
if (depth > 1) {
if (index+3 < circles.size() && ((index+3)/7) == row && circles.get(index+3).getFill() == playerColor && !winning.contains((circles.get(index + 3)))) {
winning.add((circles.get(index+3)));
}
if (index-3 >0 && ((index-3)/7) == row && circles.get(index-3).getFill() == playerColor && !winning.contains((circles.get(index - 3)))) {
winning.add((circles.get(index-3)));
}
if (index+1 < circles.size() && ((index+1)/7) == row && circles.get(index+1).getFill() == playerColor && !winning.contains((circles.get(index + 1)))) {
winning.add((circles.get(index+1)));
}
if (index-1 >0 && ((index-1)/7) == row && circles.get(index-1).getFill() == playerColor && !winning.contains((circles.get(index - 1)))) {
winning.add((circles.get(index-1)));
}
if (winning.size() == 4) {
makeFlash(winning, playerColor);
return true;
}
}
if (index-1 > 0 && circles.get(index-1).getFill() != playerColor && ((index-1)/7) == row) { //all the way to the left,
winning.remove(c);
return checkRight(c, playerColor);
}
if (index+1 < circles.size() && circles.get(index+1).getFill() != playerColor && ((index+1)/7) == row) { // all the way to the right
winning.remove(c);
return checkLeft(c, playerColor);
}
if (index-1 >0 && index + 1 < circles.size() && ((index+1)/7) == row && ((index-1)/7) == row) {
return checkLeftAndRight(circles.get(index-1), playerColor, depth +1) || checkLeftAndRight(circles.get(index+1), playerColor, depth+1);
}
else {
return false;
}
}
// Checks diagonals to the right
private boolean checkDiagonalRight(Circle c, Color playerColor, int depth) {
if (depth == 0) { // resets the winning list
winning = new ArrayList<>();
}
if (depth >4) { // prevents infinite recursion
return false;
}
int index = circles.indexOf(c);
int column = circles.indexOf(c)%7;
winning.add(c);
if (depth > 2) {
if (index+8 < circles.size() && circles.get(index+8).getFill() == playerColor && (index+8)%7 != (column+1)) {
winning.add((circles.get(index+8)));
}
if (index-8 > 0 && circles.get(index-8).getFill() == playerColor && (index-8)%7 != (column-1)) {
winning.add((circles.get(index-8)));
}
if (winning.size() == 4) {
makeFlash(winning, playerColor);
return true;
}
}
if ((index-8 > 0 && circles.get(index-8).getFill() != playerColor)) { //top left
winning.remove(c);
return checkRightDownDiagonal(c, playerColor);
}
if ((index+8 < circles.size() && circles.get(index+8).getFill() != playerColor)) { //bottom left
winning.remove(c);
return checkRightUpDiagonal(c, playerColor);
}
if (index-8 >0 && index+8 < circles.size()) {
return checkDiagonalRight(circles.get(index-8), playerColor, depth +1) || checkDiagonalRight(circles.get(index+8), playerColor, depth+1);
}
else {
return false;
}
}
// Checks diagonals to the right
private boolean checkDiagonalLeft(Circle c, Color playerColor, int depth) {
if (depth == 0) { // resets the winning list
winning = new ArrayList<>();
}
if (depth >4) { // prevents infinite recursion
return false;
}
int index = circles.indexOf(c);
int column = circles.indexOf(c)%7;
winning.add(c);
if (depth > 2) {
if (index+6 < circles.size() && circles.get(index+6).getFill() == playerColor && (index+6)%7 != (column-1)) {
winning.add((circles.get(index+6)));
}
if (index-6 > 0 && circles.get(index-6).getFill() == playerColor && (index-6)%7 != (column+1)) {
winning.add((circles.get(index-6)));
}
if (winning.size() == 4) {
makeFlash(winning, playerColor);
return true;
}
}
if ((index-6 > 0 && circles.get(index-6).getFill() != playerColor)) { //bottom right
winning.remove(c);
return checkLeftUpDiagonal(c, playerColor) || checkLeftDownDiagonal(c, playerColor);
}
if ((index+6 < circles.size() && circles.get(index+6).getFill() != playerColor)) { //top right
winning.remove(c);
return checkLeftDownDiagonal(c, playerColor) || checkLeftUpDiagonal(c, playerColor);
}
if (index-6 >0 && index+6 < circles.size()) {
return checkDiagonalLeft(circles.get(index-6), playerColor, depth+1) || checkDiagonalLeft(circles.get(index+6), playerColor, depth+1);
}
else {
return false;
}
}
// Checks left down diagonal
private boolean checkLeftDownDiagonal(Circle c, Color playerColor) {
List<Circle> winning = new ArrayList<>();
winning.add(c);
int index = circles.indexOf(c);
int column = circles.indexOf(c)%7;
int diagonal = 1;
try {
for (var i=0; i<3;i++) {
index += 6;
column -= 1;
if (circles.get(index).getFill() == playerColor && (index%7) == column) {
winning.add(circles.get(index));
diagonal +=1;
}
}
if (diagonal ==4) {
makeFlash(winning, playerColor);
return true;
}
}
catch (IndexOutOfBoundsException e) {
//Do nothing
}
return false;
}
// checks right down diagonal
private boolean checkRightDownDiagonal(Circle c, Color playerColor) {
List<Circle> winning = new ArrayList<>();
winning.add(c);
int index = circles.indexOf(c);
int column = circles.indexOf(c)%7;
int diagonal = 1;
try {
for (var i=0; i<3;i++) {
index += 8;
column += 1;
if (circles.get(index).getFill() == playerColor && (index%7) == column) {
winning.add(circles.get(index));
diagonal +=1;
}
}
if (diagonal ==4) {
makeFlash(winning, playerColor);
return true;
}
}
catch (IndexOutOfBoundsException e) {
//Do nothing
}
return false;
}
// checks left up diagonal
private boolean checkLeftUpDiagonal(Circle c, Color playerColor) {
List<Circle> winning = new ArrayList<>();
winning.add(c);
int index = circles.indexOf(c);
int column = circles.indexOf(c)%7;
int diagonal = 1;
try {
for (var i=0; i<3;i++) {
index -= 8;
column -= 1;
if (circles.get(index).getFill() == playerColor && (index%7) == column) {
winning.add(circles.get(index));
diagonal +=1;
}
}
if (diagonal ==4) {
makeFlash(winning, playerColor);
return true;
}
}
catch (IndexOutOfBoundsException e) {
//Do nothing
}
return false;
}
// checks right up diagonal
private boolean checkRightUpDiagonal(Circle c, Color playerColor) {
List<Circle> winning = new ArrayList<>();
winning.add(c);
int index = circles.indexOf(c);
int column = circles.indexOf(c)%7;
int diagonal = 1;
try {
for (var i=0; i<3;i++) {
index -= 6;
column += 1;
if (circles.get(index).getFill() == playerColor && (index%7) == column) {
winning.add(circles.get(index));
diagonal +=1;
}
}
if (diagonal ==4) {
makeFlash(winning, playerColor);
return true;
}
}
catch (IndexOutOfBoundsException e) {
//Do nothing
}
return false;
}
// checks the vertical
private boolean checkVertical(Circle c, Color playerColor) {
List<Circle> winning = new ArrayList<>();
winning.add(c);
int index = circles.indexOf(c);
int vertical = 1;
try {
for (var i=0; i<3;i++) {
index += 7;
if (circles.get(index).getFill() == playerColor) {
winning.add(circles.get(index));
vertical +=1;
}
}
if (vertical ==4) {
makeFlash(winning, playerColor);
return true;
}
}
catch (IndexOutOfBoundsException e) {
//Do nothing
}
return false;
}
//checks the left horizontial
private boolean checkLeft(Circle c, Color playerColor) {
int index = circles.indexOf(c);
int row = circles.indexOf(c)/7;
List<Circle> winning = new ArrayList<>();
winning.add(c);
int horizontial = 1;
try {
for (var i = 0; i < 3; i++) {
index += 1;
if (circles.get(index).getFill() == playerColor && (index/7) == row) {
winning.add(circles.get(index));
horizontial += 1;
}
}
if (horizontial == 4) {
makeFlash(winning, playerColor);
return true;
}
}
catch (IndexOutOfBoundsException e) {
//Do nothing
}
return false;
}
// checks the right horizontial
private boolean checkRight(Circle c, Color playerColor) {
int index = circles.indexOf(c);
int row = circles.indexOf(c)/7;
List<Circle> winning = new ArrayList<>();
winning.add(c);
int horizontial = 1;
try {
for (var i = 0; i < 3; i++) {
index -= 1;
if (circles.get(index).getFill() == playerColor && (index/7) == row) {
winning.add(circles.get(index));
horizontial += 1;
}
}
if (horizontial == 4) {
makeFlash(winning, playerColor);
return true;
}
}
catch (IndexOutOfBoundsException e) {
//Do nothing
}
return false;
}
// This sets up the timeline and flashes winning circles. On finishing, the program terminates.
private void makeFlash(List winningCircles, Color playerColor) {
Timeline t1 = new Timeline(new KeyFrame(Duration.millis(500), e -> makeFlash2(winningCircles, playerColor)));
t1.setCycleCount(5);
t1.play();
t1.setOnFinished(e -> System.exit(0));
}
// This gets called 5 times in the make flash function.
private void makeFlash2(List winningCircles, Color playerColor) {
if (flashVariable) {
for (var circle : winningCircles) {
((Circle) circle).setFill(Color.CYAN);
}
flashVariable = false;
}
else {
for (var circle : winningCircles) {
((Circle) circle).setFill(playerColor);
}
flashVariable = true;
}
}
}