при проверке выдает ошибку выхода за границы массива
соответственно если отрезать по 2 последних,то жрет, но теряет первое слово
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> result = new ArrayList<>();
int flag = 0;
char[] wordInArrFormat;
for (int i = 0; i < words.length; i++)
{
flag = 0;
wordInArrFormat = words[i].toCharArray();
for (int j = 0; j < crossword[j].length; j++)
{
for (int k = 0; k < crossword[k].length; k++)
{
if(crossword[j][k] == wordInArrFormat[0])
{
if(crossword[j-1][k-1] == wordInArrFormat[1])
{
for (int l = 2; l < wordInArrFormat.length; l++)
{
if(crossword[j-1][k-1] != wordInArrFormat[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
Word eee = new Word(new String(wordInArrFormat));
eee.setStartPoint(j,k);
eee.setEndPoint(j - wordInArrFormat.length, k - wordInArrFormat.length);
result.add(eee);
}
}
else if(crossword[j-1][k] == wordInArrFormat[1])
{
for (int l = 2; l < wordInArrFormat.length; l++)
{
if(crossword[j-1][k] != wordInArrFormat[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
Word eee = new Word(new String(wordInArrFormat));
eee.setStartPoint(j,k);
eee.setEndPoint(j - wordInArrFormat.length, k);
result.add(eee);
}
}
else if(crossword[j-1][k+1] == wordInArrFormat[1])
{
for (int l = 2; l < wordInArrFormat.length; l++)
{
if(crossword[j-1][k+1] != wordInArrFormat[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
Word eee = new Word(new String(wordInArrFormat));
eee.setStartPoint(j,k);
eee.setEndPoint(j - wordInArrFormat.length, k + wordInArrFormat.length);
result.add(eee);
}
}
else if(crossword[j+1][k-1] == wordInArrFormat[1])
{
for (int l = 2; l < wordInArrFormat.length; l++)
{
if(crossword[j+1][k-1] != wordInArrFormat[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
Word eee = new Word(new String(wordInArrFormat));
eee.setStartPoint(j,k);
eee.setEndPoint(j + wordInArrFormat.length, k - wordInArrFormat.length);
result.add(eee);
}
}
else if(crossword[j+1][k] == wordInArrFormat[1])
{
for (int l = 2; l < wordInArrFormat.length; l++)
{
if(crossword[j+1][k] != wordInArrFormat[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
Word eee = new Word(new String(wordInArrFormat));
eee.setStartPoint(j,k);
eee.setEndPoint(j + wordInArrFormat.length, k);
result.add(eee);
}
}
else if(crossword[j][k-1] == wordInArrFormat[1])
{
for (int l = 2; l < wordInArrFormat.length; l++)
{
if(crossword[j][k-1] != wordInArrFormat[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
Word eee = new Word(new String(wordInArrFormat));
eee.setStartPoint(j,k);
eee.setEndPoint(j, k - wordInArrFormat.length);
result.add(eee);
}
}
else if(crossword[j][k+1] == wordInArrFormat[1])
{
for (int l = 2; l < wordInArrFormat.length; l++)
{
if(crossword[j][k + 1] != wordInArrFormat[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
Word eee = new Word(new String(wordInArrFormat));
eee.setStartPoint(j,k);
eee.setEndPoint(j, k + wordInArrFormat.length);
result.add(eee);
}
}
else if(crossword[j+1][k+1] == wordInArrFormat[1])
{
for (int l = 2; l < wordInArrFormat.length; l++)
{
if(crossword[j+1][k+1] != wordInArrFormat[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
Word eee = new Word(new String(wordInArrFormat));
eee.setStartPoint(j,k);
eee.setEndPoint(j + wordInArrFormat.length, k + wordInArrFormat.length);
result.add(eee);
}
}
}
}
}
}
return result;
}
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);
}
}
}