Валя ругается в чём дело не пойму....
Совет от ментора:
Размер списка возвращаемого методом detectAllWords отличается от правильного!
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[][]{
{'m', 'a', 's', 'a', 'm', 'e'},
{'u', 'a', 'a', 'a', 'e', 'm'},
{'m', 'a', 'm', 'r', 'm', 'o'},
{'m', 'm', 'e', 'm', 'o', 'h'},
{'p', 'e', 'e', 'e', 'j', 'j'}
};
detectAllWords(crossword, "hom", "sam");
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List<Word> wordList = new ArrayList<>();
char firstChar;
char endChar;
int lengthWord;
for (String word: words) {
firstChar = word.charAt(0);
endChar = word.charAt(word.length()-1);
lengthWord = word.length();
for (int i = 0; i < crossword.length; i++) {
for (int j = 0; j < crossword[0].length; j++) {
if (crossword[i][j] == firstChar) {
boolean direction1 = false;
boolean direction2 = false;
boolean direction3 = false;
boolean direction4 = false;
boolean direction5 = false;
boolean direction6 = false;
boolean direction7 = false;
boolean direction8 = false;
if (i >= lengthWord - 1 && j >= lengthWord - 1 && crossword[i].length - j >= lengthWord && crossword.length - i >= lengthWord) {
direction1 = true;
direction2 = true;
direction3 = true;
direction4 = true;
direction5 = true;
direction6 = true;
direction7 = true;
direction8 = true;
} else if (j == 0 && crossword[i].length >= lengthWord && i >= lengthWord - 1 && (crossword.length - i) >= lengthWord) {
direction2 = true;
direction3 = true;
direction4 = true;
direction5 = true;
direction6 = true;
} else if (j == crossword[i].length - 1 && j >= lengthWord && i >= lengthWord - 1 && (crossword.length - i) >= lengthWord) {
direction6 = true;
direction7 = true;
direction8 = true;
direction1 = true;
direction2 = true;
} else if (i == 0 && crossword.length >= lengthWord && j >= lengthWord - 1 && (crossword[i].length - j >= lengthWord)) {
direction4 = true;
direction5 = true;
direction6 = true;
direction7 = true;
direction8 = true;
} else if (i == crossword.length - 1 && i >= lengthWord && j >= lengthWord - 1 && (crossword[i].length - j >= lengthWord)) {
direction1 = true;
direction2 = true;
direction3 = true;
direction4 = true;
direction8 = true;
} else if (i >= lengthWord - 1 && j < lengthWord - 1 && crossword[i].length - j >= lengthWord && crossword.length - i < lengthWord){
direction2 = true;
direction3 = true;
direction4 = true;
}else if (i < lengthWord - 1 && j < lengthWord - 1 && crossword[i].length - j >= lengthWord && crossword.length - i >= lengthWord){
direction4 = true;
direction5 = true;
direction6 = true;
}else if (i < lengthWord - 1 && j >= lengthWord - 1 && crossword[i].length - j < lengthWord && crossword.length - i >= lengthWord){
direction6 = true;
direction7 = true;
direction8 = true;
}else if (i >= lengthWord - 1 && j >= lengthWord - 1 && crossword[i].length - j < lengthWord && crossword.length - i < lengthWord){
direction8 = true;
direction1 = true;
direction2 = true;
}
String finderWord = String.valueOf(firstChar);
if (direction1 && endChar == crossword[i - (lengthWord - 1)][j - (lengthWord - 1)]) {
for (int k = 1; k < lengthWord; k++) {
finderWord += (char) crossword[i - k][j - k];
}
if (finderWord.equals(word)) {
Word tempWord = new Word(finderWord);
tempWord.setStartPoint(j,i);
tempWord.setEndPoint(j - (lengthWord - 1), i - (lengthWord - 1));
wordList.add(tempWord);
}
}
if (direction2 && endChar == crossword[i - (lengthWord - 1)][j]) {
finderWord = String.valueOf(firstChar);
for (int k = 1; k < lengthWord; k++) {
finderWord += (char) crossword[i - k][j];
}
if (finderWord.equals(word)) {
Word tempWord = new Word(finderWord);
tempWord.setStartPoint(j,i);
tempWord.setEndPoint(j, i - (lengthWord - 1));
wordList.add(tempWord);
}
}
if (direction3 && endChar == crossword[i - (lengthWord - 1)][j + (lengthWord - 1)]) {
finderWord = String.valueOf(firstChar);
for (int k = 1; k < lengthWord; k++) {
finderWord += (char) crossword[i - k][j + k];
}
if (finderWord.equals(word)) {
Word tempWord = new Word(finderWord);
tempWord.setStartPoint(j,i);
tempWord.setEndPoint(j + (lengthWord - 1),i - (lengthWord - 1));
wordList.add(tempWord);
}
}
if (direction4 && endChar == crossword[i][j + (lengthWord - 1)]) {
finderWord = String.valueOf(firstChar);
for (int k = 1; k < lengthWord; k++) {
finderWord += (char) crossword[i][j + k];
}
if (finderWord.equals(word)) {
Word tempWord = new Word(finderWord);
tempWord.setStartPoint(j,i);
tempWord.setEndPoint(j + (lengthWord - 1), i);
wordList.add(tempWord);
}
}
if (direction5 && endChar == crossword[i + (lengthWord - 1)][j + (lengthWord - 1)]) {
finderWord = String.valueOf(firstChar);
for (int k = 1; k < lengthWord; k++) {
finderWord += (char) crossword[i + k][j + k];
}
if (finderWord.equals(word)) {
Word tempWord = new Word(finderWord);
tempWord.setStartPoint(j,i);
tempWord.setEndPoint(j + (lengthWord - 1),i + (lengthWord - 1));
wordList.add(tempWord);
}
}
if (direction6 && endChar == crossword[i + (lengthWord - 1)][j]) {
finderWord = String.valueOf(firstChar);
for (int k = 1; k < lengthWord; k++) {
finderWord += (char) crossword[i + k][j];
}
if (finderWord.equals(word)) {
Word tempWord = new Word(finderWord);
tempWord.setStartPoint(j,i);
tempWord.setEndPoint(j, i + (lengthWord - 1));
wordList.add(tempWord);
}
}
if (direction7 && endChar == crossword[i + (lengthWord - 1)][j - (lengthWord - 1)]) {
finderWord = String.valueOf(firstChar);
for (int k = 1; k < lengthWord; k++) {
finderWord += (char) crossword[i + k][j - k];
}
if (finderWord.equals(word)) {
Word tempWord = new Word(finderWord);
tempWord.setStartPoint(j,i);
tempWord.setEndPoint(j - (lengthWord - 1), i + (lengthWord - 1));
wordList.add(tempWord);
}
}
if (direction8 && endChar == crossword[i][j - (lengthWord - 1)]) {
finderWord = String.valueOf(firstChar);
for (int k = 1; k < lengthWord; k++) {
finderWord += (char) crossword[i][j - k];
}
if (finderWord.equals(word)) {
Word tempWord = new Word(finderWord);
tempWord.setStartPoint(j,i);
tempWord.setEndPoint(j - (lengthWord - 1),i);
wordList.add(tempWord);
}
}
}
}
}
}
System.out.println(wordList);
return wordList;
}
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);
}
}
}