Все проверки, которые нашёл в обсуждении проверил - всё проходит, всё выдаёт как нужно. Не понимаю что не так... Помогите
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'}
};
List<Word> list = detectAllWords(crossword, "home", "same");
list.forEach(System.out::println);
/*
Ожидаемый результат
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 currentWord : words) {
for (int y = 0; y < crossword.length; y++)
for (int x = 0; x < crossword[y].length; x++) {
if (crossword[y][x] == currentWord.charAt(0)) {
if(currentWord.length() == 1) {
list.add(new Word(currentWord, x, y, x, y));
continue;
}
goLeft(crossword, x, y, x, y, currentWord.substring(1), list, currentWord);
goRight(crossword, x, y, x, y, currentWord.substring(1), list, currentWord);
goUp(crossword, x, y, x, y, currentWord.substring(1), list, currentWord);
goDown(crossword, x, y, x, y, currentWord.substring(1), list, currentWord);
goUL(crossword, x, y, x, y, currentWord.substring(1), list, currentWord);
goUR(crossword, x, y, x, y, currentWord.substring(1), list, currentWord);
goDL(crossword, x, y, x, y, currentWord.substring(1), list, currentWord);
goDR(crossword, x, y, x, y, currentWord.substring(1), list, currentWord);
}
}
}
return list;
}
public static void goLeft(int[][] crossword, int xS, int yS, int xE, int yE, String word, List<Word> list, String current) {
if(xE > 0) if(crossword[yE][xE - 1] == word.charAt(0))
if(word.length() == 1) list.add(new Word(current, xS, yS, xE - 1, yE));
else goLeft(crossword, xS, yS, xE - 1, yE, word.substring(1), list, current);
}
public static void goRight(int[][] crossword, int xS, int yS, int xE, int yE, String word, List<Word> list, String current) {
if(xE < crossword[yE].length - 1) if(crossword[yE][xE + 1] == word.charAt(0))
if(word.length() == 1) list.add(new Word(current, xS, yS, xE + 1, yE));
else goRight(crossword, xS, yS, xE + 1, yE, word.substring(1), list, current);
}
public static void goUp(int[][] crossword, int xS, int yS, int xE, int yE, String word, List<Word> list, String current) {
if(yE > 0) if(crossword[yE - 1][xE] == word.charAt(0))
if(word.length() == 1) list.add(new Word(current, xS, yS, xE, yE - 1));
else goUp(crossword, xS, yS, xE, yE - 1, word.substring(1), list, current);
}
public static void goDown(int[][] crossword, int xS, int yS, int xE, int yE, String word, List<Word> list, String current) {
if(yE < crossword.length - 1) if(crossword[yE + 1][xE] == word.charAt(0))
if(word.length() == 1) list.add(new Word(current, xS, yS, xE, yE + 1));
else goDown(crossword, xS, yS, xE, yE + 1, word.substring(1), list, current);
}
public static void goUL(int[][] crossword, int xS, int yS, int xE, int yE, String word, List<Word> list, String current) {
if(yE > 0 && xE > 0) if(crossword[yE - 1][xE - 1] == word.charAt(0))
if(word.length() == 1) list.add(new Word(current, xS, yS, xE - 1, yE - 1));
else goUL(crossword, xS, yS, xE - 1, yE - 1, word.substring(1), list, current);
}
public static void goUR(int[][] crossword, int xS, int yS, int xE, int yE, String word, List<Word> list, String current) {
if(yE > 0 && xE < crossword[yE].length - 1) if(crossword[yE - 1][xE + 1] == word.charAt(0))
if(word.length() == 1) list.add(new Word(current, xS, yS, xE + 1, yE - 1));
else goUR(crossword, xS, yS, xE + 1, yE - 1, word.substring(1), list, current);
}
public static void goDL(int[][] crossword, int xS, int yS, int xE, int yE, String word, List<Word> list, String current) {
if(yE < crossword.length - 1 && xE > 0) if(crossword[yE + 1][xE - 1] == word.charAt(0))
if(word.length() == 1) list.add(new Word(current, xS, yS, xE - 1, yE + 1));
else goDL(crossword, xS, yS, xE - 1, yE + 1, word.substring(1), list, current);
}
public static void goDR(int[][] crossword, int xS, int yS, int xE, int yE, String word, List<Word> list, String current) {
if(yE < crossword.length - 1 && xE < crossword[yE].length - 1) if(crossword[yE + 1][xE + 1] == word.charAt(0))
if(word.length() == 1) list.add(new Word(current, xS, yS, xE + 1, yE + 1));
else goDR(crossword, xS, yS, xE + 1, yE + 1, word.substring(1), list, current);
}
public static class Word {
private String text;
private int startX = -1;
private int startY = -1;
private int endX = -1;
private int endY = -1;
public Word(String text, int xS, int yS, int xE, int yE) {
this.text = text;
startX = xS;
startY = yS;
endX = xE;
endY = yE;
}
@Override
public String toString() {
return String.format("%s - (%d, %d) - (%d, %d)", text, startX, startY, endX, endY);
}
}
}