так уж плохо.
>Ниже представлены различные идеи того, как, по моему мнению, будет работать что-то подобное.
Код: Выделить всё
public static boolean areFiveConnected(String [][] tablero, int x, int y){
return hasFiveInARowVertical(tablero,x,y) || northeastDiagonal(tablero,x,y) || southwestDiagonal(tablero, x, y) || hasFiveInARowHorizontal(tablero, x, y);
}
private static boolean hasFiveInARowVertical(String[][] array, int row, int col) {
// Check if the current element and the next 4 elements in the row are all the same symbol.
for (String [] line: array){
System.out.println(Arrays.toString(line));
for (int i = 0; i < line.length - 4; i++) {
System.out.println(!line[i].equals(".")+line[i]+"here");
if (!line[i].equals(".") || line[i].equals(line[i + 1]) && line[i + 1].equals(line[i + 2]) && line[i + 2].equals(line[i + 3]) &&line[i + 3].equals(line[i + 4])) {
return true;
}
}
}
// If we reach here, there are 5 of the same symbols in a row horizontally.
return false;
}
private static boolean hasFiveInARowHorizontal(String[][] array, int row, int col) {
// Check if the current element and the next 4 elements in the column are all the same symbol.
for (int i = 0; i < 5; i++) {
if (row + i >= array.length || !array[row + i][col].equals(array[row][col]) || array[row][col].equals(".")) {
return false;
}
}
// If we reach here, there are 5 of the same symbols in a row vertically.
return true;
}
private static boolean northeastDiagonal(String[][] tablero, int row, int col) {
// ascendingDiagonalCheck
for (int i = 0; i < 5; i++) {
// Check northeast diagonal.
if (row + i >= tablero.length || col + i >= tablero[row].length || !tablero[row + i][col + i].equals(tablero[row][col]) || tablero[row][col].equals(".")) {
return false;
}
}
return true;
}
private static boolean southwestDiagonal(String[][] tablero, int row, int col) {
// ascendingDiagonalCheck
for (int i = 0; i < 5; i++) {
// Check northeast diagonal.
if (row - i < 0 || col + i >= tablero[row].length || !tablero[row - i][col + i].equals(tablero[row][col]) || tablero[row][col].equals(".")) {
return false;
}
}
return true;
}
Код: Выделить всё
a b c d e f g h i j k l m n o
0 [X] [.] [.] [.] [.] [.] [X] [.] [.] [.] [.] [.] [.] [.] [.]
1 [X] [.] [.] [.] [.] [.] [.] [X] [.] [.] [.] [.] [.] [.] [.]
2 [X] [.] [.] [.] [.] [.] [.] [.] [X] [.] [.] [.] [.] [.] [.]
3 [X] [.] [.] [.] [.] [.] [.] [.] [.] [X] [.] [.] [.] [.] [.]
4 [X] [.] [.] [.] [.] [.] [.] [.] [.] [.] [X] [.] [.] [.] [.]
5 [.] [O] [O] [O] [O] [O] [.] [.] [.] [.] [.] [.] [.] [.] [.]
6 [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.]
7 [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.]
8 [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.]
9 [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.]
10 [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.]
11 [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.]
12 [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.]
13 [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.]
14 [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.] [.]
Я также пытался прочитать следующие 4 части. символ, соседний с фрагментом, который был установлен последним, но который проверяет только края, а не середину строки, что приведет к истинному выводу.
Подробнее здесь: https://stackoverflow.com/questions/772 ... -direction