Ребята, Please, помогите найти ошибку или ошибки в коде. Меня особенно смущает моя строка :
if (chars[y + 1] == crossword[a - 1][b - 1] || chars[y + 1] == crossword[a][b - 1] || chars[y + 1] == crossword[a + 1][b - 1] || chars[y + 1] == crossword[a + 1][b] || chars[y + 1] == crossword[a + 1][b + 1] || chars[y + 1] == crossword[a][b + 1] || chars[y + 1] == crossword[a-1][b+1]||chars[y+1]==crossword[a-1][b]) {
так как по логике вещей будет выход за пределы массива на каком-то моменте, так и выдает:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
не могу понять, как тогда задать параметры поиска буквы вокруг другой буквы, чтобы не было выхода за пределы массива.
Из-за этой ошибки, соответственно не могу посмотреть какой получается вывод.
package com.javarush.task.task20.task2027;
import java.util.ArrayList;
import java.util.List;
/*
Кроссворд
*/
public class Solution {
public static void main(String[] args) {
int[][] crossword = new int[][]{
{'f', 'd', 'e', 'r', 'l', 'k'},
{'u', 's', 'a', 'm', 'e', 'o'},
{'l', 'n', 'g', 'r', 'o', 'v'},
{'m', 'l', 'p', 'r', 'r', 'h'},
{'p', 'o', 'e', 'e', 'j', 'j'}
};
detectAllWords(crossword, "home", "same");
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
String [] arrayWords = words.toString().split(" ");
List<Word> word = new ArrayList<>();
for (int i = 0; i < arrayWords.length; i++) {
char [] chars = arrayWords[i].toCharArray();
for (int y = 0; y < chars.length; y++) {
int beginA = 0;
int beginB = 0;
int finishA = 0;
int finishB = 0;
for (int x = 0; x < crossword.length; x++) {
for (int a = 0; a < 5; a++)
for (int b = 0; b < crossword[a].length; b++) {
if (crossword [a][b] == chars[y]) {
int begin = crossword[a][b];
beginA = a;
beginB = b;
if (chars[y + 1] == crossword[a - 1][b - 1] || chars[y + 1] == crossword[a][b - 1] || chars[y + 1] == crossword[a + 1][b - 1] || chars[y + 1] == crossword[a + 1][b] || chars[y + 1] == crossword[a + 1][b + 1] || chars[y + 1] == crossword[a][b + 1] || chars[y + 1] == crossword[a-1][b+1]||chars[y+1]==crossword[a-1][b]) {
if (chars[y + 1] != chars[chars.length]) {
continue;
} else if (chars[y + 1] == chars[chars.length]) {
if (chars[y+1]==crossword[a - 1][b - 1]) {
int finish = crossword[a - 1][b - 1];
finishA = a - 1;
finishB = b -1;
}
else if (chars[y + 1] == crossword[a][b - 1]) {
int finish = crossword[a][b - 1];
finishA = a;
finishB = b -1;
}
else if (chars[y + 1] == crossword[a + 1][b - 1]){
int finish = crossword[a + 1][b - 1];
finishA = a + 1;
finishB = b - 1;
}
else if (chars[y + 1] == crossword[a + 1][b]) {
int finish = crossword[a + 1][b];
finishA = a + 1;
finishB = b;
}
else if (chars[y + 1] == crossword[a + 1][b + 1]) {
int finish = crossword[a + 1][b + 1];
finishA = a + 1;
finishB = b + 1;
}
else if (chars[y + 1] == crossword[a][b + 1]) {
int finish = crossword[a][b + 1];
finishA = a;
finishB = b + 1;
}
else if(chars[y + 1] == crossword[a-1][b+1]) {
int finish = crossword[a-1][b+1];
finishA = a - 1;
finishB = b + 1;
}
else if(chars[y+1]==crossword[a-1][b]) {
int finish = crossword[a-1][b];
finishA = a - 1;
finishB = b;
}
}
}
else {
break;
}
}
else continue;
}
}
word.add(new Word(words[y] + " - (" + beginA + ", " + beginB + ") - (" + finishA + ", " + finishB + ")"));
}
}
for(int i = 0; i < word.size(); i++) {
System.out.println(word.get(i));
}
return word;
}
public static class Word {
private String text;
private int startX;
private int startY;
private int endX;
private int endY;
public Word(String text) {
this.text = text;
}
public void setStartPoint(int i, int j) {
startX = i;
startY = j;
}
public void setEndPoint(int i, int j) {
endX = i;
endY = j;
}
@Override
public String toString() {
return String.format("%s - (%d, %d) - (%d, %d)", text, startX, startY, endX, endY);
}
}
}