-
Notifications
You must be signed in to change notification settings - Fork 1
/
Connect4.java
497 lines (414 loc) · 13.7 KB
/
Connect4.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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Connect4 {
static Scanner scanner = new Scanner(System.in);
// The game board is represented as a 2-dimensional array
// with 6 rows and 7 columns
private static int dth = 5;
private static final int ROWS = 6;
private static final int COLS = 7;
private static char[][] board = new char[ROWS][COLS];
public static void main(String[] args) {
// Initialize the game board to be empty
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
board[i][j] = ' ';
}
}
// Start the game by printing the initial board state
printBoard();
System.out.println("Do you want to play against another human (Y/N)?");
String response = scanner.nextLine();
boolean againstHuman = response.equalsIgnoreCase("y");
// If the user doesn't want to play against another human, ask whether they want
// to go first
if (!againstHuman) {
System.out.println("Do you want to go first (Y/N)?");
response = scanner.nextLine();
boolean humanGoesFirst = response.equalsIgnoreCase("y");
// If the human doesn't want to go first, let the AI go first
if (!humanGoesFirst) {
aiTurn();
}
}
// The game is played by alternating turns between the human
// player and the AI opponent
char currentPlayer = againstHuman ? '1' : 'H';
while (true) {
if (currentPlayer == '1') {
// Human player 1's turn
humanTurn(1);
// Check if the game is over
if (isGameOver()) {
break;
}
// Switch to the other player's turn
currentPlayer = '2';
} else if (currentPlayer == '2') {
// Human player 2's turn
humanTurn(2);
// Check if the game is over
if (isGameOver()) {
break;
}
// Switch to the other player's turn
currentPlayer = '1';
} else if (currentPlayer == 'H') {
// Human player's turn
humanTurn();
// Check if the game is over
if (isGameOver()) {
break;
}
// If the user is playing against another human, skip the AI opponent's turn
if (againstHuman) {
continue;
}
// AI opponent's turn
aiTurn();
// Check if the game is over
if (isGameOver()) {
break;
}
}
}
}
// This method is called during the human player's turn
private static void humanTurn() {
// Prompt the player to enter a column where they want to place their piece
System.out.println("Enter a column (1-7): ");
int col = scanner.nextInt();
// Place the piece in the selected column
placePiece(col, 'H');
// Print the updated board
printBoard();
}
// This method is called during the human player's turn when playing against
// another human
private static void humanTurn(int player) {
// Prompt the player to enter a column where they want to place their piece
System.out.println("Player " + player + ", enter a column (1-7): ");
int col = scanner.nextInt();
// Place the piece in the selected column
placePiece(col, (char) (player + '0'));
// Print the updated board
printBoard();
}
// This method is called during the AI opponent's turn
private static void aiTurn() {
// Use the minimax algorithm to find the best move for the AI opponent
int col = getBestMove(dth);
// Place the piece in the selected column
placePiece(col, 'A');
// Print the updated board
printBoard();
}
public static int getBestMove(int depth) {
int bestScore = Integer.MIN_VALUE;
int bestCol = -1;
for (int col : getMoves()) {
placePiece(col, 'A');
int score = minimize(depth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
removePiece(col);
if (score > bestScore) {
bestScore = score;
bestCol = col;
}
}
return bestCol;
}
private static int maximize(int depth, int alpha, int beta) {
if (depth == 0 || hasWon('H') || hasWon('A') || getMoves().isEmpty()) {
return evaluate();
}
int bestScore = Integer.MIN_VALUE;
for (int col : getMoves()) {
placePiece(col, 'A');
bestScore = Math.max(bestScore, minimize(depth - 1, alpha, beta));
removePiece(col);
if (bestScore >= beta) {
return bestScore;
}
alpha = Math.max(alpha, bestScore);
}
return bestScore;
}
private static int minimize(int depth, int alpha, int beta) {
if (depth == 0 || hasWon('H') || hasWon('A') || getMoves().isEmpty()) {
return evaluate();
}
int bestScore = Integer.MAX_VALUE;
for (int col : getMoves()) {
placePiece(col, 'H');
bestScore = Math.min(bestScore, maximize(depth - 1, alpha, beta));
removePiece(col);
if (bestScore <= alpha) {
return bestScore;
}
beta = Math.min(beta, bestScore);
}
return bestScore;
}
private static void printBoard() {
// Print the column numbers
System.out.print(" _");
for (int i = 0; i < COLS; i++) {
System.out.print("|" + (i + 1));
}
System.out.println("|");
// Print the game board
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (j == 0) {
// Print the row number
System.out.print("|" + (i + 1));
}
System.out.print("|" + board[i][j]);
}
System.out.println("|");
}
System.out.println("---------------");
}
// This method returns true if the game is over, false otherwise
private static boolean isGameOver() {
// Check if player 1 has won
if (hasWon('1')) {
System.out.println("Player 1 has won!");
return true;
}
// Check if player 2 has won
if (hasWon('2')) {
System.out.println("Player 2 has won!");
return true;
}
// Check if the human player has won
if (hasWon('H')) {
System.out.println("Human player has won!");
return true;
}
// Check if the AI opponent has won
if (hasWon('A')) {
System.out.println("AI opponent has won!");
return true;
}
// Check if the board is full (i.e. a tie)
if (isFull()) {
System.out.println("The game is a tie!");
return true;
}
// If none of the above conditions are true, the game is not over
return false;
}
// This method places a piece on the game board
private static void placePiece(int col, char player) {
// Check if the selected column is valid
if (col < 1 || col > COLS) {
System.out.println("Invalid column!");
return;
}
// Check if the selected column is full
if (board[0][col - 1] != ' ') {
System.out.println("Column is full!");
return;
}
// Loop through the rows from bottom to top
for (int i = ROWS - 1; i >= 0; i--) {
// If the current cell is empty, place the piece in this cell
if (board[i][col - 1] == ' ') {
board[i][col - 1] = player;
return;
}
}
}
// This method generates a list of all possible moves that the AI player can
// make
private static List<Integer> getMoves() {
List<Integer> moves = new ArrayList<>();
// Loop through each column
for (int i = 0; i < COLS; i++) {
// Check if the selected column is not full
if (board[0][i] == ' ') {
moves.add(i + 1);
}
}
return moves;
}
// This method removes a piece from the game board
private static void removePiece(int col) {
// Loop through the rows from top to bottom
for (int i = 0; i < ROWS; i++) {
// If the current cell is not empty, remove the piece from this cell
if (board[i][col - 1] != ' ') {
board[i][col - 1] = ' ';
return;
}
}
}
private static int evaluate() {
if (hasWon('A')) {
return 1000;
} else if (hasWon('H')) {
return -1000;
}
int score = 0;
// Loop through each cell on the game board
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
// If the cell is empty, skip it
if (board[i][j] == ' ') {
continue;
}
// Calculate the score for the current cell based on the number
// of consecutive pieces in the horizontal, vertical, and diagonal
// directions
int h = horizontal(i, j);
int v = vertical(i, j);
int d1 = diagonal1(i, j);
int d2 = diagonal2(i, j);
// Add a weighting factor based on the position of the cell
int weight = (3 - i) * (3 - j) + (i + 1) * (j + 1);
// Add the weighted sum of these scores to the total score
score += weight * (h + v + d1 + d2);
}
}
return score;
}
// This method returns true if the specified player has won the game, false
// otherwise
private static boolean hasWon(char player) {
// Loop through each cell on the game board
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
// If the current cell does not belong to the specified player, skip it
if (board[i][j] != player) {
continue;
}
// Check if the specified player has four consecutive pieces in the
// horizontal, vertical, or diagonal direction starting from the current cell
if (horizontal(i, j) >= 4 || vertical(i, j) >= 4 || diagonal1(i, j) >= 4 || diagonal2(i, j) >= 4) {
return true;
}
}
}
return false;
}
// This method returns true if the game board is full, false otherwise
private static boolean isFull() {
// Loop through each cell on the game board
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
// If the current cell is empty, the board is not full
if (board[i][j] == ' ') {
return false;
}
}
}
// If all cells have been checked and none of them is empty, the board is full
return true;
}
// This method returns the number of consecutive pieces in the horizontal
// direction
// starting from the specified cell
private static int horizontal(int row, int col) {
// Initialize the counter to 1 (for the current cell)
int count = 1;
// Check the cells to the left of the current cell
for (int i = col - 1; i >= 0; i--) {
// If the cell is empty or belongs to the other player, stop counting
if (board[row][i] == ' ' || board[row][i] != board[row][col]) {
break;
}
// Otherwise, increment the counter
count++;
}
// Check the cells to the right of the current cell
for (int i = col + 1; i < COLS; i++) {
// If the cell is empty or belongs to the other player, stop counting
if (board[row][i] == ' ' || board[row][i] != board[row][col]) {
break;
}
// Otherwise, increment the counter
count++;
}
return count;
}
// This method returns the number of consecutive pieces in the vertical
// direction
// starting from the specified cell
private static int vertical(int row, int col) {
// Initialize the counter to 1 (for the current cell)
int count = 1;
// Check the cells above the current cell
for (int i = row - 1; i >= 0; i--) {
// If the cell is empty or belongs to the other player, stop counting
if (board[i][col] == ' ' || board[i][col] != board[row][col]) {
break;
}
// Otherwise, increment the counter
count++;
}
// Check the cells below the current cell
for (int i = row + 1; i < ROWS; i++) {
// If the cell is empty or belongs to the other player, stop counting
if (board[i][col] == ' ' || board[i][col] != board[row][col]) {
break;
}
// Otherwise, increment the counter
count++;
}
return count;
}
// This method returns the number of consecutive pieces in the diagonal
// direction
// (top-left to bottom-right) starting from the specified cell
private static int diagonal1(int row, int col) {
// Initialize the counter to 1 (for the current cell)
int count = 1;
// Check the cells in the top-left direction
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
// If the cell is empty or belongs to the other player, stop counting
if (board[i][j] == ' ' || board[i][j] != board[row][col]) {
break;
}
// Otherwise, increment the counter
count++;
}
// Check the cells in the bottom-right direction
for (int i = row + 1, j = col + 1; i < ROWS && j < COLS; i++, j++) {
// If the cell is empty or belongs to the other player, stop counting
if (board[i][j] == ' ' || board[i][j] != board[row][col]) {
break;
}
// Otherwise, increment the counter
count++;
}
return count;
}
// This method returns the number of consecutive pieces in the diagonal
// direction
// (top-right to bottom-left) starting from the specified cell
private static int diagonal2(int row, int col) {
// Initialize the counter to 1 (for the current cell)
int count = 1;
// Check the cells in the top-right direction
for (int i = row - 1, j = col + 1; i >= 0 && j < COLS; i--, j++) {
// If the cell is empty or belongs to the other player, stop counting
if (board[i][j] == ' ' || board[i][j] != board[row][col]) {
break;
}
// Otherwise, increment the counter
count++;
}
// Check the cells in the bottom-left direction
for (int i = row + 1, j = col - 1; i < ROWS && j >= 0; i++, j--) {
// If the cell is empty or belongs to the other player, stop counting
if (board[i][j] == ' ' || board[i][j] != board[row][col]) {
break;
}
// Otherwise, increment the counter
count++;
}
return count;
}
}