Another solution is for each cell to check if its two neighbours are full and it is empty:
public class StackOverflow{
static char[][] board = {{'x', 'x', ' '}, {'x', ' ', ' '}, {' ', ' ', ' '}};
public static int[] checkRow(){
for (int i = 0; i<3; i++){
for (int j = 0; j<3; j++){
if (board[i][(j+1)%3]=='x' && board[i][(j+1)%3]=='x' && board[i][j]==' '){
return new int[]{i, j};
}
}
}
return null;
}
public static void main(String[] args){
System.out.println(checkRow()[0] + " "+checkRow()[1]);
}
}
This will iterate through every cell, and check the ones 1 and 2 to the left, mod 3 so that you never check out of bounds. It returns null on a failed attempt. (which will throw because of the print statement, not because of the function itself)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…