-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFour.java
More file actions
169 lines (145 loc) · 4.18 KB
/
ConnectFour.java
File metadata and controls
169 lines (145 loc) · 4.18 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
import java.util.Stack;
public class ConnectFour extends ConnectFourI {
private Stack<Integer> stackCol = new Stack<Integer>();
private Stack<Integer> stackRow = new Stack<Integer>();
public ConnectFour(){
x = new Player("x");
o = new Player("o");
toMove = x;
}
@Override
public int[] getMoves() {
int k = 0;
int[] tempBoard = new int[BOARD_WIDTH];
for(int i = 0; i < BOARD_WIDTH; i++){
for(int j = 0; j < BOARD_HEIGHT; j++){
if(board[j][i] == null){
//Add the column
tempBoard[k] = i;
k++;
break;
}
}
}
int[] subBoard = new int[k];
for(int x = 0; x < tempBoard.length; x++){
if(tempBoard[x] != 0 || x == 0){
subBoard[x] = tempBoard[x];
}
}
return subBoard;
}
@Override
public boolean isGameOver() {
if(this.getWinner() == null){
return false;
}
return (this.getMoves() == null || this.getWinner().getName().equals("x") || this.getWinner().getName().equals("o"));
}
@Override
public Player getWinner() {
if(stackCol.isEmpty() && stackRow.isEmpty()){
return null;
}
int lastCol = stackCol.peek();
int lastRow = stackRow.peek();
Player who = getSquare(lastRow, lastCol);
if(checkNbd(who, lastRow, lastCol)){
return who;
} else {
return null;
}
}
//Checks a neighborhood of the last played position to determine if there is a winner
private boolean checkNbd(Player who, int lastRow, int lastCol){
int horzCnt = 0;
int vertCnt = 0;
int diagCnt1 = 0;
int diagCnt2 = 0;
for(int i = 0; i < 4; i++){
//Horizontals (Across columns), Verticals (up and down rows), Diagonals(positive and negative "slopes")
for(int j = 0; j < 4; j++){
horzCnt = checkDir(who, horzCnt, lastRow, lastCol - i + j);
vertCnt = checkDir(who, vertCnt, lastRow - i + j, lastCol);
diagCnt1 = checkDir(who, diagCnt1, lastRow + i - j, lastCol - i + j);
diagCnt2 = checkDir(who, diagCnt2, lastRow + i - j, lastCol + i - j);
}
if(horzCnt >= 4 || vertCnt >= 4 || diagCnt1 >= 4|| diagCnt2 >= 4){
return true;
} else {
horzCnt = 0;
vertCnt = 0;
diagCnt1 = 0;
diagCnt2 = 0;
}
}
return false;
}
private int checkDir(Player who, int cnt, int row, int col){
if(checkPlayer(who, row, col)){
cnt++;
}
return cnt;
}
//Checks if the player at (row, col) is "who"
private boolean checkPlayer(Player who, int row, int col){
if(getSquare(row, col) == null){
return false;
}
return who.getName() == getSquare(row, col).getName();
}
@Override
public boolean makeMove(int col) {
if((col > BOARD_WIDTH - 1 || col < 0) || this.isGameOver()){
return false;
}
//Decreasing, since row-major order is the opposite order of insertion
for(int i = BOARD_HEIGHT - 1; i >= 0; i--){
if(board[i][col] == null){
if(toMove == x){
board[i][col] = new Player("x");
toMove = o;
} else if (toMove == o){
board[i][col] = new Player("o");
toMove = x;
}
stackCol.push(col);
stackRow.push(i);
return true;
}
}
return false;
}
@Override
public void undoMove() {
if(!stackRow.isEmpty() && !stackCol.isEmpty()){
int row = stackRow.pop();
int col = stackCol.pop();
//Resetting the movecounters
if(this.getSquare(row, col).getName().equals("x")){
toMove = x;
} else if(this.getSquare(row, col).getName().equals("o")){
toMove = o;
}
board[row][col] = null;
}
}
@Override
public Player getSquare(int row, int col) {
if(row > BOARD_HEIGHT - 1 || row < 0 || col > BOARD_WIDTH - 1 || col < 0){
return null;
}
return board[row][col];
}
@Override
public Player getToMove() {
if(this.isGameOver()){
return null;
}
if(toMove == x){
return x;
} else {
return o;
}
}
}