...что мне теперь делать? Это проблема моего кода или временные затруднения приложения? В Idea на предложенных примерах все решало (хотя за общие случаи, пожалуй, не ручаюсь)
package com.javarush.task.task20.task2027;
import java.util.*;
import javafx.util.*;
/*
Кроссворд
*/
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) {
boolean is_found = true;
Pair<Integer, Integer> start = new Pair<>(-1, 0);
Pair<Integer, Integer> next;
Pair<Integer, Integer> step;
Pair<Integer, Integer> end = new Pair<>(-1, 0);
List<Word> answer = new ArrayList<Word>();
for (String s : words) {
start = new Pair<>(-1, 0);
while (true) {
is_found = true;
start = findLetter(s.charAt(0), crossword, start.getKey(), start.getValue());
next = findNeighbor(s.charAt(1), start.getValue(), start.getKey(), crossword);
step = new Pair<>(next.getKey() - start.getKey(), next.getValue() - start.getValue());
for (int i = 0; i < s.length() - 1; i++) {
end = new Pair<>(next.getKey() + i * step.getKey(), next.getValue() + i * step.getValue());
if (crossword[end.getKey()][end.getValue()] != s.charAt(i + 1)) {
is_found = false;
}
}
if (is_found) break;
}
Word w = new Word(s);
w.setStartPoint(start.getValue(), start.getKey());
w.setEndPoint(end.getValue(), end.getKey());
answer.add(w);
}
return answer;
}
public static Pair<Integer, Integer> findLetter (char letter, int[][] array, int X, int Y) {
for (int i = Y; i < array.length; i++) {
for (int j = X + 1; j < array[i].length; j++) {
if (array[i][j] == letter) return new Pair<>(i, j);
}
}
return null;
}
public static Pair<Integer, Integer> findNeighbor (char letter, int X, int Y, int[][] array) {
if (array[Y - 1][X - 1] == letter) {
return new Pair<>(Y - 1, X - 1);
}
else if (array[Y][X - 1] == letter) {
return new Pair<>(Y, X - 1);
}
else if (array[Y + 1][X - 1] == letter) {
return new Pair<>(Y + 1, X - 1);
}
else if (array[Y - 1][X] == letter) {
return new Pair<>(Y - 1, X);
}
else if (array[Y - 1][X + 1] == letter) {
return new Pair<>(Y, X - 1);
}
else if (array[Y + 1][X] == letter) {
return new Pair<>(Y + 1, X);
}
else if (array[Y][X + 1] == letter) {
return new Pair<>(Y, X + 1);
}
else if (array[Y + 1][X + 1] == letter) {
return new Pair<>(Y + 1, X + 1);
}
else return null;
}
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);
}
}
}