0|_|_|_|_|_|_|_|
1|_|_|_|_|_|_|_|
2|_|_|_|_|_|_|_|
3|_|_|_|_|_|_|_|
4|_|_|_|_|_|_|X|
5|_|X|X|_|X|_|O|
0 1 2 3 4 5 6
You’re going to write some code for a win checker. Given a move and the state of the board, determine if the player wins. For simplicity, only worry about a horizontal win(4 successful X in a row).
public boolean isWin(int dropColumn, char value, char[][] state) {
int level == 0;
for(int i = 0; i < state.length; i++) {
if(state[i][dropColumn] != '0') {
level = i - 1;
state[level][dropColumn] == value;
}
}
int left = dropColumn, right = dropColumn;
while(left >= 0 && state[level][left] == value) left--;
while(right <= state[level].length - 1 && state[level][right] == value) right++;
if(right - left >= 5) return true;
return false;
}