-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectFour.java
62 lines (59 loc) · 2.18 KB
/
ConnectFour.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
public class ConnectFour {
private final int rows = 6;
private final int cols = 7;
private final int[][] board = new int[rows][cols];
public boolean dropPiece(int col, int player) {
if (col < 0 || col >= cols) {
return false;
}
for (int row = rows - 1; row >= 0; row--) {
if (board[row][col] == 0) {
board[row][col] = player;
return true;
}
}
return false;
}
public int checkWinner() {
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int player = board[row][col];
if (player != 0) {
if (col + 3 < cols &&
player == board[row][col + 1] &&
player == board[row][col + 2] &&
player == board[row][col + 3]) {
return player;
}
if (row + 3 < rows &&
player == board[row + 1][col] &&
player == board[row + 2][col] &&
player == board[row + 3][col]) {
return player;
}
if (row + 3 < rows && col + 3 < cols &&
player == board[row + 1][col + 1] &&
player == board[row + 2][col + 2] &&
player == board[row + 3][col + 3]) {
return player;
}
if (row - 3 >= 0 && col + 3 < cols &&
player == board[row - 1][col + 1] &&
player == board[row - 2][col + 2] &&
player == board[row - 3][col + 3]) {
return player;
}
}
}
}
for (int col = 0; col < cols; col++) {
if (board[0][col] == 0) {
return 0;
}
}
return -1;
}
public int[][] getBoard() {
return board;
}
}