-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectFourLogic.java
246 lines (226 loc) · 7.99 KB
/
ConnectFourLogic.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
//This class inherits from TicTacToe base class
//Most implentation can be found there
public class ConnectFourLogic extends TicTacToe {
//Constructor
public ConnectFourLogic() {
super(6);
}
//Current player makes move if possible
public boolean makeMove(int y) {
int lastEmptyRow = -1;
for(int [] row : board) {
if(row[y] == 0)
lastEmptyRow++;
}
if(lastEmptyRow == -1) {
return false;
}
board[lastEmptyRow][y] = turn;
if (turn == 1) {
turn = 2;
} else {
turn = 1;
}
return true;
}
//Returns the index of the last empty row in a column, -1 if column is full
public int emptyRow(int y) {
int lastEmptyRow = -1;
for(int [] row : board) {
if(row[y] == 0)
lastEmptyRow++;
}
return lastEmptyRow;
}
//Returns -1 if the current game state is a Tie, 1 if player 1 won, 2 if player 2 won
public int gameStatus() {
boolean [] playerWinStatus = new boolean[] {false, false, false};
//Check if any row is identical
for( int[] row : board) {
int count1 = 1;
int count2 = 1;
for(int p = 1; p < 3; p++) {
for(int index = 0; index < 6; index++) {
if( (p == 1) && (row[index] == 1) && (index != 0) && (row[index -1] == 1) ) {
count1++;
}
else if ( (p== 2) && (row[index] == 2) && (index != 0) && (row[index -1] == 2)){
count2++;
}
}
}
if(count1 >= 4)
playerWinStatus[1] = true;
if(count2 >= 4)
playerWinStatus[2] = true;
}
//Check if any column is identical
for(int column = 0; column < boardSize; column++) {
int count1 = 1;
int count2 = 1;
for (int row = 0; row < boardSize - 1; row++) {
if ((board[row][column] != 0) && (board[row][column] == board[row + 1][column])) {
if (board[row][column] == 1) {
count1++;
} else {
count2++;
}
}
}
if (count1 >= 4)
playerWinStatus[1] = true;
if (count2 >= 4)
playerWinStatus[2] = true;
}
//Check if an identical diagonal exists slanting right -> \
int column = 2;
int count = 1;
for (int row = 0; row < 3; row ++) {
if(( board[row][column] != 0) && (board[row][column] == board[row+1][column+1]))
count++;
column++;
}
if( (count == 4) && (board[0][2] == 1) )
playerWinStatus[1] = true;
if( (count == 4) && (board[0][2] == 2) )
playerWinStatus[2] = true;
int count1 = 1;
int count2 = 1;
for( int p = 1; p < 3; p++) {
column = 1;
for (int row = 0; row < 4; row ++) {
if ((p == 1) && (board[row][column] == 1) && (board[row + 1][column + 1] == 1))
count1++;
if ((p == 2) && (board[row][column] == 2) && (board[row + 1][column + 1] == 2))
count2++;
column++;
}
}
if( count1 >= 4)
playerWinStatus[1] = true;
if( count2>=4 )
playerWinStatus[2] = true;
count1 = 1;
count2 = 1;
for( int p = 1; p < 3; p++) {
column = 0;
for (int row = 0; row < 5; row ++) {
if ((p == 1) && (board[row][column] == 1) && (board[row + 1][column + 1] == 1))
count1++;
if ((p == 2) && (board[row][column] == 2) && (board[row + 1][column + 1] == 2))
count2++;
column++;
}
}
if( count1 >= 4)
playerWinStatus[1] = true;
if( count2>=4 )
playerWinStatus[2] = true;
count1 = 1;
count2 = 1;
for( int p = 1; p < 3; p++) {
column = 0;
for (int row = 1; row < 5; row ++) {
if ((p == 1) && (board[row][column] == 1) && (board[row + 1][column + 1] == 1))
count1++;
if ((p == 2) && (board[row][column] == 2) && (board[row + 1][column + 1] == 2))
count2++;
column++;
}
}
if( count1 >= 4)
playerWinStatus[1] = true;
if( count2>=4 )
playerWinStatus[2] = true;
column = 0;
count = 1;
for (int row = 2; row < 5; row ++) {
if(( board[row][column] != 0) && (board[row][column] == board[row+1][column+1]))
count++;
column++;
}
if( (count >= 4) && (board[2][0] == 1) )
playerWinStatus[1] = true;
if( (count >= 4) && (board[2][0] == 2) )
playerWinStatus[2] = true;
//Check if an identical diagonal exists slanting left -> /
int columnR = 5;
int countR = 1;
for (int row = 2; row < 5; row ++) {
if( ( board[row][columnR] != 0) && (board[row][columnR] == board[row+1][columnR-1]) )
countR++;
columnR--;
}
if( (countR >= 4) && (board[2][5] == 1) )
playerWinStatus[1] = true;
if( (countR >= 4) && (board[2][5] == 2) )
playerWinStatus[2] = true;
int count1R = 1;
int count2R = 1;
for(int p = 1; p < 3; p++) {
columnR = 5;
for (int row = 1; row < 5; row ++) {
if ((p == 1) && (board[row][columnR] == 1) && (board[row + 1][columnR - 1] == 1))
count1R++;
if ((p == 2) && (board[row][columnR] == 2) && (board[row + 1][columnR - 1] == 2))
count2R++;
columnR--;
}
}
if( count1R >= 4)
playerWinStatus[1] = true;
if( count2R >= 4 )
playerWinStatus[2] = true;
count1R = 1;
count2R = 1;
for(int p = 1; p < 3; p++) {
columnR = 5;
for (int row = 0; row < 5; row++) {
if ((p == 1) && (board[row][columnR] == 1) && (board[row + 1][columnR - 1] == 1))
count1R++;
if ((p == 2) && (board[row][columnR] == 2) && (board[row + 1][columnR - 1] == 2))
count2R++;
columnR--;
}
}
if( count1R >= 4)
playerWinStatus[1] = true;
if( count2R >= 4 )
playerWinStatus[2] = true;
count1R = 1;
count2R = 1;
for(int p = 1; p < 3; p++) {
columnR = 4;
for (int row = 0; row < 4; row ++) {
if ((p == 1) && (board[row][columnR] == 1) && (board[row + 1][columnR - 1] == 1))
count1R++;
if ((p == 2) && (board[row][columnR] == 2) && (board[row + 1][columnR - 1] == 2))
count2R++;
columnR--;
}
}
if( count1R >= 4)
playerWinStatus[1] = true;
if( count2R >= 4 )
playerWinStatus[2] = true;
columnR = 3;
countR = 1;
for (int row = 0; row < 3; row ++) {
if( ( board[row][columnR] != 0) && (board[row][columnR] == board[row+1][columnR-1]) )
countR++;
columnR--;
}
if( (countR >= 4) && (board[0][3] == 1) )
playerWinStatus[1] = true;
if( (countR >= 4) && (board[0][3] == 2) )
playerWinStatus[2] = true;
if(playerWinStatus[1] && playerWinStatus[2])
gameStatus = -1;
else if (playerWinStatus[1])
gameStatus = 1;
else if (playerWinStatus[2])
gameStatus = 2;
else { gameStatus = 0; }
return gameStatus;
}
}