- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFour.java
More file actions
Latest commit
147 lines (127 loc) · 4.88 KB
/
Copy pathConnectFour.java
File metadata and controls
147 lines (127 loc) · 4.88 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
publicclassConnectFour {
privateint[][] board;
//board : 0 : empty, 1 : player1, 2 : player2
privateintwin;
privateintplayer;
publicConnectFour(introw, intcol, intwin) {
this.board = newint[row][col];
this.win = win;
this.player = 1;
}
/**
*
* @param col
* @return The current element's position, [-1, -1] if player can not place the move
*/
publicint[] play(intcol) {
int[] pos = newint[2];
introw = 0;
// System.out.println(col);
for (; row < board.length; row++) {
//Find the first empty space
if (board[row][col] == 0) break;
}
if (row < board.length) {
board[row][col] = this.player;
if (player == 1)
player = 2;
else
player = 1;
returnnewint[] {row, col};
}
returnnewint[] {-1, -1};
}
publicvoidprint() {
for (inti = board.length - 1; i >= 0; i--) {
for (intj = 0; j < board[0].length; j++) {
if (board[i][j] == 1) {
System.out.print('O');
} elseif (board[i][j] == 2) {
System.out.print('X');
} else {
System.out.print('.');
}
}
System.out.println();
}
}
/**
* i, j is the position in the board, return 0 if the game is not yet over, 1 if player1 win and 2 if player2 win
* @param i
* @param j
* @return winner (1 or 2), not yet finished: 0, finish but no winner: -1
*/
publicintwinner(inti, intj) {
if (boardFull()) return -1;
//Check row
// left side consecutive + itself + right side consecutive
if (rowConsecutive(i, j, -1) + 1 + rowConsecutive(i, j, 1) >= win) returnboard[i][j];
// Bottom consecutive + itself +
if (colConsecutive(i, j, -1) + 1 + colConsecutive(i, j, 1) >= win) returnboard[i][j];
// Check two way diagonals
if (diagonalConsecutive(i + 1, j + 1, 1, 1) + 1 + diagonalConsecutive(i - 1, j - 1, -1, -1) >= win) returnboard[i][j];
if (diagonalConsecutive(i - 1, j + 1, -1, 1) + 1 + diagonalConsecutive(i + 1, j - 1, 1, -1) >= win) returnboard[i][j];
System.out.print("cons: ");
System.out.println(rowConsecutive(i, j, -1) + 1 + rowConsecutive(i, j, 1));
return0;
}
/**
* Return how many consecutive consecutive elements in the row with given increment
*/
privateintrowConsecutive(introw, intcol, intincrement) {
intplayer = this.board[row][col];
if (player == 0) return0;
intres = 0;
for (; col + increment < board[0].length && col + increment >= 0 && board[row][col + increment] == board[row][col]; col += increment, res += 1);
returnres;
}
/**
* Return how many consecutive consecutive elements in the col with given increment
*/
privateintcolConsecutive(introw, intcol, intincrement) {
intplayer = this.board[row][col];
if (player == 0) return0;
intres = 0;
for (; row + increment < board.length && row + increment >= 0 && board[row + increment][col] == board[row][col]; row += increment, res += 1);
returnres;
}
/**
*
* @param row
* @param col
* @param ver: Indicate vertical direction: +1: up, -1: down
* @param hor: Indicate horizontal direction, +1: top heads to right, -1: top heads to left
* @return
*/
privateintdiagonalConsecutive(introw, intcol, intver, inthor) {
if (row >= board.length || col >= board.length || row < 0 || col < 0 || board[row - ver][col - hor] != board[row][col]) return0;
return1 + diagonalConsecutive(row + ver, col + hor, ver, hor);
}
privatebooleanboardFull() {
for (intcol = 0; col < board[0].length; col++) {
if (board[board.length - 1][col] == 0) returnfalse;
}
returntrue;
}
publicstaticvoidmain(String[] args) {
ConnectFourcf = newConnectFour(9,9,5);
System.out.println("Player1 moves \"O\" and Player2 two moves \"X\"");
while (true) {
int[] pos;
while ((pos = cf.play((int)(Math.random() * 7)))[0] == -1);
System.out.println();
cf.print();
intwinner;
if ((winner = cf.winner(pos[0], pos[1])) != 0) {
if (winner == -1) { //No winner and game is finished
System.out.println("No winner and the game is finished");
break;
} else {
System.out.println("Winner is player" + winner);
System.out.println("Player1 moves \"O\" and Player2 two moves \"X\"");
break;
}
}
}
}
}