-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFour.java
More file actions
391 lines (362 loc) · 11.3 KB
/
Copy pathConnectFour.java
File metadata and controls
391 lines (362 loc) · 11.3 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
import java.util.Scanner;
public class ConnectFour
{
private static final String ANSI_RED = "\u001B[31m";
private static final String ANSI_WHITE = "\u001B[37m";
private static final String ANSI_YELLOW = "\u001B[33m";
private static final String ANSI_RESET = "\u001B[0m";
private final String[][] board;
private int turn;
private final String red = ANSI_RED + "O" + ANSI_RESET;
private final String yellow = ANSI_YELLOW + "O" + ANSI_RESET;
private final String redWin = ANSI_RED + "The Soviets won yet again!" + ANSI_RESET;
private final String yellowWin = ANSI_YELLOW + "Oh Jimothy Crickety, the farmers won!" + ANSI_RESET;
public ConnectFour() {
board = new String[6][7];
for (String[] board1 : board)
{
for (int col = 0; col < board[0].length; col++)
{
board1[col] = "-";
}
}
}
public void clearBoard()
{
for(String[] board1 : board)
{
for(int col = 0; col < board[0].length; col++)
{
board1[col] = "-";
}
}
}
public void clearTurn()
{
turn *= 0;
}
public int getTurn()
{
return turn;
}
public String getRedWin()
{
return redWin;
}
public String getYellowWin()
{
return yellowWin;
}
public void printBoard() {
System.out.print(ANSI_WHITE + " ");
for (int col = 0; col < board[0].length; col++)
{
System.out.print(col + " ");
}
System.out.println();
for(int row = 0; row < board.length; row++)
{
System.out.print(ANSI_WHITE + row + " ");
for(String slot : board[row])
{
switch (slot) {
case red -> System.out.print(red + " ");
case yellow -> System.out.print(yellow + " ");
default -> System.out.print(ANSI_WHITE + slot + " ");
}
}
System.out.println();
}
System.out.print(ANSI_RESET);
}
public boolean pickLocation(int col)
{
return board[0][col].equals("-");
}
public void animateDrop(int col, String piece)
{
for (int row = 0; row < board.length; row++)
{
// Find the lowest empty row in the column
if (row == board.length - 1 || !board[row + 1][col].equals("-"))
{
// Animation: drop piece from top to this row
for (int animRow = 0; animRow <= row; animRow++)
{
board[animRow][col] = piece;
if (animRow > 0) board[animRow - 1][col] = "-";
printBoard();
try
{
Thread.sleep(100); // 100 ms delay for animation
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
// Clear console (works in many terminals)
System.out.print("\033[H\033[2J");
System.out.flush();
}
break;
}
}
}
// Use this in takeTurn instead of direct placement:
public void takeTurn(int col) {
String piece = (turn % 2 == 0) ? red : yellow;
animateDrop(col, piece);
turn++;
}
public boolean checkRow()
{
for (String[] board1 : board) {
boolean redWin = true;
boolean yellowWin = true;
for (int col = 0; col < board[0].length; col++) {
if (!board1[col].equals(red)) {
redWin = false;
}
if (!board1[col].equals(yellow)) {
yellowWin = false;
}
}
if (redWin || yellowWin)
{
return true;
}
}
return false;
}
public boolean checkCol()
{
for (int col = 0; col < board[0].length; col++)
{
boolean redWin = true;
boolean yellowWin = true;
for (String[] board1 : board) {
if (!board1[col].equals(red)) {
redWin = false;
}
if (!board1[col].equals(yellow)) {
yellowWin = false;
}
}
if (redWin || yellowWin)
{
return true;
}
}
return false;
}
public boolean checkDiag()
{
// Check main diagonal
boolean redWin = true;
boolean yellowWin = true;
for (int i = 0; i < board.length; i++) {
if (!board[i][i].equals(red)) {
redWin = false;
}
if (!board[i][i].equals(yellow)) {
yellowWin = false;
}
}
if (redWin || yellowWin) {
return true;
}
// Check anti-diagonal
redWin = true;
yellowWin = true;
for (int i = 0; i < board.length; i++) {
if (!board[i][board.length - 1 - i].equals(red)) {
redWin = false;
}
if (!board[i][board.length - 1 - i].equals(yellow)) {
yellowWin = false;
}
}
return redWin || yellowWin;
}
public boolean checkDraw()
{
for (int row = 0; row < board.length; row++)
{
for (int col = 0; col < board[0].length; col++)
{
if (board[row][col].equals("-"))
{
return false; // Found an empty slot, not a draw
}
}
}
// Board is full, check if there is no winner
return !checkWin();
}
public boolean checkWin() {
// Check for 4 in a row horizontally, vertically, and diagonally
for (int row = 0; row < board.length; row++)
{
for (int col = 0; col < board[0].length; col++)
{
String cell = board[row][col];
if (cell.equals("-")) continue;
// Horizontal
if (col <= board[0].length - 4)
{
if (cell.equals(board[row][col+1]) && cell.equals(board[row][col+2]) && cell.equals(board[row][col+3]))
{
return true;
}
}
// Vertical
if (row <= board.length - 4)
{
if (cell.equals(board[row+1][col]) && cell.equals(board[row+2][col]) && cell.equals(board[row+3][col]))
{
return true;
}
}
// Diagonal down-right
if (row <= board.length - 4 && col <= board[0].length - 4)
{
if (cell.equals(board[row+1][col+1]) && cell.equals(board[row+2][col+2]) && cell.equals(board[row+3][col+3]))
{
return true;
}
}
// Diagonal down-left
if (row <= board.length - 4 && col >= 3)
{
if (cell.equals(board[row+1][col-1]) && cell.equals(board[row+2][col-2]) && cell.equals(board[row+3][col-3]))
{
return true;
}
}
}
}
return false;
}
public void takeRedTurn(Scanner input) {
System.out.println("Red's turn");
int c = input.nextInt();
while (!pickLocation(c)) {
System.out.println("Invalid location. Try again.");
c = input.nextInt();
}
takeTurn(c);
printBoard();
}
public void takeYellowTurn(Scanner input) {
System.out.println("Yellow's turn");
int c = input.nextInt();
while (!pickLocation(c)) {
System.out.println("Invalid location. Try again.");
c = input.nextInt();
}
takeTurn(c);
printBoard();
}
public void startPlayer(User currentUser, Scanner input)
{
clearBoard();
clearTurn();
printBoard();
while(!checkWin())
{
if(getTurn() % 2 == 0)
{
System.out.println("Red's turn");
int c = input.nextInt();
while(!pickLocation(c))
{
System.out.println("Invalid location. Try again");
c = input.nextInt();
}
takeTurn(c);
printBoard();
}
else
{
System.out.println("Yellow's turn");
int c = input.nextInt();
while(!pickLocation(c))
{
System.out.println("Invalid location. Try again");
c = input.nextInt();
}
takeTurn(c);
printBoard();
}
if(checkDraw())
{
System.out.println("Neither of you are world class material! SO SAD...");
System.out.println("You can cover it up by playing again, you know");
break;
}
if(checkWin())
{
if ((getTurn() - 1) % 2 == 0)
{
System.out.println(getRedWin());
currentUser.giveWin();
}
else
{
System.out.println(getYellowWin());
}
break;
}
}
}
public void startComputer(User currentUser, Scanner input)
{
clearBoard();
clearTurn();
printBoard();
while(!checkWin() && !checkDraw())
{
if(getTurn() % 2 == 0)
{
System.out.println("Red's turn");
int c = input.nextInt();
while(!pickLocation(c))
{
System.out.println("Invalid location. Try again");
c = input.nextInt();
}
takeTurn(c);
printBoard();
}
else
{
System.out.println("Yellow's turn: ");
int c = (int) (Math.random() * 4);
while (!pickLocation(c))
{
c = (int) (Math.random() * 4);
}
takeTurn(c);
printBoard();
}
}
// After loop, check for win or draw and print appropriate message
if(checkWin())
{
if ((getTurn() - 1) % 2 == 0)
{
System.out.println(getRedWin());
currentUser.giveWin();
}
else
{
System.out.println(getYellowWin());
}
}
else if(checkDraw())
{
System.out.println("Neither of you are world class material! SO SAD...");
System.out.println("You can cover it up by playing again, you know");
}
}
}