Вывод мой
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
Чего не так?
package com.javarush.task.task20.task2027;
import java.util.LinkedList;
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> l = detectAllWords(crossword, "home", "same");
for (Word word : l) {
System.out.println(word);
}
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
Word word;
List<Word> lists = new LinkedList<>();
for(int i = 0; i < words.length; i++) {
int a = 0;
int b = 0;
char[] array = words[i].toCharArray();
int len = array.length - 1;
end:
for (int y = 0; y < 5; y++) {
for (int x = 0; x < 6; x++) {
if (crossword[y][x] == array[0]) {
if (x < 5) {if (crossword[y][x+1] == array[1]) {a = 1; b = 0;}}
if (x < 5 && y < 4) {if (crossword[y+1][x+1] == array[1]) {a = 1; b = 1;}}
if (y < 4) {if (crossword[y+1][x] == array[1]) {a = 0; b = 1;}}
if (x > 0) {if (crossword[y][x-1] == array[1]) {a = -1; b = 0;}}
if (y > 0 && x > 0) {if (crossword[y-1][x-1] == array[1]) {a = -1; b = -1;}}
if (y > 0) {if (crossword[y-1][x] == array[1]) {a = 0; b = -1;}}
}
if (a != 0 || b != 0) {
if (x + (a * len) < 6 && x + (a * len) >= 0 && y + (b * len) < 5 && y + (b * len) >= 0) {
word = new Word(words[i]);
word.setStartPoint(x, y);
word.setEndPoint(x + (a * len), y + (b * len));
lists.add(word);
break end;
}
}
}
}
}
return lists;
}
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);
}
}
}