Выходов вне размеров массива нет , что то не так в коде, подскажите пожалуйста что.
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) {
List<Word> list = new ArrayList<>();
for(String word:words){
for(int i =0;i<crossword.length;i++){
for(int j =0;j<crossword[i].length;j++){
if(crossword[i][j]==word.charAt(0)){
ArrayList<int[]> point = new ArrayList<>();
point=detectPoint(crossword,i,j,word);
for(int[] points : point){
Word a = new Word(word);
a.setStartPoint(points[0],points[1]);
a.setEndPoint(points[2],points[3]);
list.add(a);
}
}
}
}
}
return list;
}
public static ArrayList<int[]> detectPoint(int[][] cros, int fir,int sec,String word){
ArrayList<int[]> detect = new ArrayList<>();
detect.add(detectLeft(cros,fir,sec,word));
detect.add(detectRight(cros,fir,sec,word));
detect.add(detectUp(cros,fir,sec,word));
detect.add(detectDown(cros,fir,sec,word));
detect.add(detectLeftUp(cros,fir,sec,word));
detect.add(detectLeftDown(cros,fir,sec,word));
detect.add(detectRightUp(cros,fir,sec,word));
detect.add(detectRightDown(cros,fir,sec,word));
return detect;
}
public static int[] detectLeft(int[][] crossword,int fir,int sec,String word){
int[] array = new int[4];
int i = fir;
int j = sec;
for(int n = 0;n<word.length()-1;n++){
if((i>=0 && i<=crossword.length - 1) && (j>=0 && j<=crossword[i].length - 1)){
if(crossword[i][j]==word.charAt(n)){
j--;
if(n==word.length()-1){
array[0] = fir;
array[1] = sec;
array[2] = i ;
array[3] = j ;
}
}
}
}
return array;
}
public static int[] detectRight(int[][] crossword,int fir,int sec,String word){
int[] array = new int[4];
int i = fir;
int j = sec;
for(int n = 0;n<word.length()-1;n++){
if((i>=0 && i<=crossword.length - 1) && (j>=0 && j<=crossword[i].length - 1)){
if(crossword[i][j]==word.charAt(n)){
j++;
if(n==word.length()-1){
array[0] = fir;
array[1] = sec;
array[2] = i ;
array[3] = j ;
}
}
}
}
return array;
}
public static int[] detectUp(int[][] crossword,int fir,int sec,String word){
int[] array = new int[4];
int i = fir;
int j = sec;
for(int n = 0;n<word.length()-1;n++){
if((i>=0 && i<=crossword.length - 1) && (j>=0 && j<=crossword[i].length - 1)){
if(crossword[i][j]==word.charAt(n)){
i++;
if(n==word.length()-1){
array[0] = fir;
array[1] = sec;
array[2] = i ;
array[3] = j ;
}
}
}
}
return array;
}
public static int[] detectDown(int[][] crossword,int fir,int sec,String word){
int[] array = new int[4];
int i = fir;
int j = sec;
for(int n = 0;n<word.length()-1;n++){
if((i>=0 && i<=crossword.length - 1) && (j>=0 && j<=crossword[i].length - 1)){
if(crossword[i][j]==word.charAt(n)){
i--;
if(n==word.length()-1){
array[0] = fir;
array[1] = sec;
array[2] = i ;
array[3] = j ;
}
}
}
}
return array;
}
public static int[] detectLeftUp(int[][] crossword,int fir,int sec,String word){
int[] array = new int[4];
int i = fir;
int j = sec;
for(int n = 0;n<word.length()-1;n++){
if((i>=0 && i<=crossword.length - 1) && (j>=0 && j<=crossword[i].length - 1)){
if(crossword[i][j]==word.charAt(n)){
i++;
j--;
if(n==word.length()-1){
array[0] = fir;
array[1] = sec;
array[2] = i ;
array[3] = j ;
}
}
}
}
return array;
}
public static int[] detectLeftDown(int[][] crossword,int fir,int sec,String word){
int[] array = new int[4];
int i = fir;
int j = sec;
for(int n = 0;n<word.length()-1;n++){
if((i>=0 && i<=crossword.length - 1) && (j>=0 && j<=crossword[i].length - 1)){
if(crossword[i][j]==word.charAt(n)){
i--;
j--;
if(n==word.length()-1){
array[0] = fir;
array[1] = sec;
array[2] = i ;
array[3] = j ;
}
}
}
}
return array;
}
public static int[] detectRightUp(int[][] crossword,int fir,int sec,String word){
int[] array = new int[4];
int i = fir;
int j = sec;
for(int n = 0;n<word.length()-1;n++){
if((i>=0 && i<=crossword.length - 1) && (j>=0 && j<=crossword[i].length - 1)){
if(crossword[i][j]==word.charAt(n)){
i++;
j++;
if(n==word.length()-1){
array[0] = fir;
array[1] = sec;
array[2] = i ;
array[3] = j ;
}
}
}
}
return array;
}
public static int[] detectRightDown(int[][] crossword,int fir,int sec,String word){
int[] array = new int[4];
int i = fir;
int j = sec;
for(int n = 0;n<word.length()-1;n++){
if((i>=0 && i<=crossword.length - 1) && (j>=0 && j<=crossword[i].length - 1)){
if(crossword[i][j]==word.charAt(n)){
i--;
j++;
if(n==word.length()-1){
array[0] = fir;
array[1] = sec;
array[2] = i ;
array[3] = j ;
}
}
}
}
return array;
}
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);
}
}
}