Вроде бы все верно, но не могу найти баг, помогите пожалуйста...
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").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> wordList = new ArrayList<>();
for (String word:words) {
int lengthWord = word.length()-1;
char symbolStart = word.charAt(0);
char symbolEnd = word.charAt(lengthWord);
ArrayList<Coordinate> starts = choiceCoordSymbol(crossword, symbolStart);
ArrayList<Coordinate> ends = choiceCoordSymbol(crossword, symbolEnd);
for (Coordinate start:starts){
for (Coordinate end:ends){
if (start.checkWord(end, lengthWord)){
wordList.add(new Word(word, start, end));
}
}
}
}
return wordList;
}
public static class Coordinate {
int x;
int y;
public Coordinate(int x, int y){
this.x = x;
this.y = y;
}
public boolean checkWord(Coordinate endOrStart, int lengthWord) {
boolean flag = false;
if ((this.x+lengthWord==endOrStart.x)&&((this.y==endOrStart.y)||
(this.y+lengthWord==endOrStart.y)||(this.y-lengthWord==endOrStart.y))) flag = true;
if ((this.x==endOrStart.x)&&((this.y+lengthWord==endOrStart.y)||
this.y-lengthWord==endOrStart.y)) flag = true;
if ((this.x-lengthWord==endOrStart.x)&&((this.y==endOrStart.y)||
(this.y+lengthWord==endOrStart.y)||(this.y-lengthWord==endOrStart.y))) flag = true;
return flag;
}
}
public static ArrayList<Coordinate> choiceCoordSymbol(int[][] crosword, char symbol){
ArrayList<Coordinate> list = new ArrayList<>();
int symb = (int) symbol;
for (int i = 0; i < crosword.length; i++) {
for (int j = 0; j < crosword[i].length; j++) {
if (symb==crosword[i][j]) list.add(new Coordinate(j, i));
}
}
return list;
}
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 Word(String text, Coordinate start, Coordinate end) {
this.text = text;
this.startX = start.x;
this.startY = start.y;
this.endX = end.x;
this.endY = end.y;
}
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);
}
}
}