Пишет "Ты неправильно определил положение граничных букв(ы) слов(а)."
Не понимаю что не так? Выводит так как нужно но ему не нравится...
package com.javarush.task.task20.task2027;
import java.util.ArrayList;
import java.util.Collections;
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");
for (Word word: list) {
System.out.println(word);
}
/*
Ожидаемый результат
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 s : words) {
list.add(new Word(s));
}
for (int e = 0; e < words.length; e++) { // Слова
char[] word = words[e].toCharArray(); // Разбиваем на символы
char firstS = word[0]; // Первый символ
boolean bContinue = false;
for (int i = 0; i < crossword.length; i++) { // Колонка по высоте
if (bContinue) continue;
for (int j = 0; j < crossword[i].length; j++) { // Колонка по горизонтале.
if (bContinue) {
continue;
}
if (crossword[i][j] == firstS) {
if (checkLeft(crossword, word, j,i)) {
Word w = list.get(e);
w.setStartPoint(j,i);
w.setEndPoint(j-(w.text.length()-1),i);
bContinue = true;
} else if (checkRight(crossword,word,j,i)) {
Word w = list.get(e);
w.setStartPoint(j,i);
w.setEndPoint(j+(w.text.length()-1),i);
bContinue=true;
} else if (checkUp(crossword,word,j,i)) {
Word w = list.get(e);
w.setStartPoint(j,i);
w.setEndPoint(j,i-(w.text.length()-1));
bContinue=true;
} else if (checkDown(crossword,word,j,i)) {
Word w = list.get(e);
w.setStartPoint(j,i);
w.setEndPoint(j,i+(w.text.length()-1));
bContinue=true;
} else if (checkLeftUpDiagonal(crossword,word,j,i)) {
Word w = list.get(e);
w.setStartPoint(j,i);
w.setEndPoint(j-(w.text.length() - 1), i - (w.text.length() - 1));
bContinue=true;
} else if (checkRightUpDiagonal(crossword,word,j,i)) {
Word w = list.get(e);
w.setStartPoint(j,i);
w.setEndPoint(j+(w.text.length() - 1), i - (w.text.length() - 1));
bContinue=true;
} else if (checkLeftDownDiagonal(crossword,word,j,i)) {
Word w = list.get(e);
w.setStartPoint(j,i);
w.setEndPoint(j-(w.text.length()-1),i+(w.text.length()-1));
bContinue=true;
} else if (checkRightDownDiagonal(crossword,word,j,i)) {
Word w = list.get(e);
w.setStartPoint(j,i);
w.setEndPoint(j+(w.text.length()-1),i+(w.text.length()-1));
bContinue=true;
}
}
}
}
}
return list;
}
public static boolean checkLeft(int[][] crossword, char[] word, int x, int y){
//code...
int endX = x - (word.length-1);
int endY = y;
if (endX >= 0 && endY >= 0
&&
endX < x + word.length && endY < y + word.length) {
boolean matchSymbols = true;
for (int i = 0; i < word.length; i++) {
if (matchSymbols) {
if (x >= 0 && x < crossword[i].length && y >= 0 && y < crossword.length) {
if (crossword[y][x] == word[i]) {
x--;
} else {
matchSymbols = false;
}
} else {
matchSymbols = false;
}
} else {
return false;
}
}
return matchSymbols;
}
return false;
}
public static boolean checkRight(int[][] crossword, char[] word, int x, int y) {
int endX = x + (word.length-1);
int endY = y;
if (endX >= 0 && endY >= 0
&&
endX < x + word.length && endY < y + word.length) {
boolean matchSymbols = true;
for (int i = 0; i < word.length; i++) {
if (matchSymbols) {
if (x >= 0 && x < crossword[i].length && y >= 0 && y < crossword.length) {
if (crossword[y][x] == word[i]) {
x++;
} else {
matchSymbols = false;
}
} else {
matchSymbols = false;
}
} else {
return false;
}
}
return matchSymbols;
}
return false;
}
public static boolean checkUp(int[][] crossword, char[] word, int x, int y) {
int endX = x;
int endY = y - (word.length-1);
if (endX >= 0 && endY >= 0
&&
endX < x + word.length && endY < y + word.length) {
boolean matchSymbols = true;
for (int i = 0; i < word.length; i++) {
if (matchSymbols) {
if (x >= 0 && x < crossword[i].length && y >= 0 && y < crossword.length) {
if (crossword[y][x] == word[i]) {
y--;
} else {
matchSymbols = false;
}
} else {
matchSymbols = false;
}
} else {
return false;
}
}
return matchSymbols;
}
return false;
}
public static boolean checkDown(int[][] crossword, char[] word, int x, int y) {
int endX = x;
int endY = y + (word.length-1);
if (endX >= 0 && endY >= 0
&&
endX < x + word.length && endY < y + word.length) {
boolean matchSymbols = true;
for (int i = 0; i < word.length; i++) {
if (matchSymbols) {
if (x >= 0 && x < crossword[i].length && y >= 0 && y < crossword.length) {
if (crossword[y][x] == word[i]) {
y++;
} else {
matchSymbols = false;
}
} else {
matchSymbols = false;
}
} else {
return false;
}
}
return matchSymbols;
}
return false;
}
//Диагональ
public static boolean checkLeftUpDiagonal(int[][] crossword, char[] word, int x, int y) {
int endX = x - (word.length-1);
int endY = y - (word.length-1);
if (endX >= 0 && endY >= 0
&&
endX < x + word.length && endY < y + word.length) {
boolean matchSymbols = true;
for (int i = 0; i < word.length; i++) {
if (matchSymbols) {
if (x >= 0 && x < crossword[i].length && y >= 0 && y < crossword.length) {
if (crossword[y][x] == word[i]) {
x--;
y--;
} else {
matchSymbols = false;
}
} else {
matchSymbols = false;
}
} else {
return false;
}
}
return matchSymbols;
}
return false;
}
public static boolean checkRightUpDiagonal(int[][] crossword, char[] word, int x, int y) {
int endX = x + (word.length-1);
int endY = y - (word.length-1);
if (endX >= 0 && endY >= 0
&&
endX < x + word.length && endY < y + word.length) {
boolean matchSymbols = true;
for (int i = 0; i < word.length; i++) {
if (matchSymbols) {
if (x >= 0 && x < crossword[i].length && y >= 0 && y < crossword.length) {
if (crossword[y][x] == word[i]) {
x++;
y--;
} else {
matchSymbols = false;
}
} else {
matchSymbols = false;
}
} else {
return false;
}
}
return matchSymbols;
}
return false;
}
public static boolean checkLeftDownDiagonal(int[][] crossword, char[] word, int x, int y) {
int endX = x + (word.length-1);
int endY = y + (word.length-1);
if (endX >= 0 && endY >= 0
&&
endX < x + word.length && endY < y + word.length) {
boolean matchSymbols = true;
for (int i = 0; i < word.length; i++) {
if (matchSymbols) {
if (x >= 0 && x < crossword[i].length && y >= 0 && y < crossword.length) {
if (crossword[y][x] == word[i]) {
y++;
x++;
} else {
matchSymbols = false;
}
} else {
matchSymbols = false;
}
} else {
return false;
}
}
return matchSymbols;
}
return false;
}
public static boolean checkRightDownDiagonal(int[][] crossword, char[] word, int x, int y) {
int endX = x - (word.length-1);
int endY = y + (word.length-1);
if (endX >= 0 && endY >= 0
&&
endX < x + word.length && endY < y + word.length) {
boolean matchSymbols = true;
for (int i = 0; i < word.length; i++) {
if (matchSymbols) {
if (x >= 0 && x < crossword[i].length && y >= 0 && y < crossword.length) {
if (crossword[y][x] == word[i]) {
y++;
x--;
} else {
matchSymbols = false;
}
} else {
matchSymbols = false;
}
} else {
return false;
}
}
return matchSymbols;
}
return false;
}
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);
}
}
}