Валидатор не принимает решение. В чем моя ошибка?
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'}
};
int[][] crosswordModified = 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', 'r', 'e', 'j', 'j'}
};
int[][] crossword2 = new int[][]{
{'f', 'e', 'e', 'e', 'l', 'e'},
{'u', 's', 'n', 'n', 'n', 'o'},
{'l', 'e', 'n', 'o', 'n', 'e'},
{'m', 'm', 'n', 'n', 'n', 'h'},
{'p', 'e', 'e', 'e', 'j', 'e'},
};
detectAllWords(crossword, "jr", "rr", "oe", "mm", "vo", "jrgs", "vorg", "aaa");
//detectAllWords(crossword, "leo", "home", "sun", "same", "god", "red", "mo");
//detectAllWords(crosswordModified, "rr");
/*Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List<Word> result = new ArrayList<>();
for (String s : words) {
char [] sDisassembled = s.toCharArray();
char firstChar = sDisassembled[0];
ArrayList<Integer[]> possibleStarts = new ArrayList<>();
for (int y = 0; y < crossword.length; y++) {
for (int x = 0; x < crossword[y].length; x++) {
if (crossword[y][x] == firstChar)
possibleStarts.add(new Integer[]{y, x});
}
}
//
for (Integer[] startCoords : possibleStarts) {
int[] neighbors = getNeighbors(crossword, startCoords[0], startCoords[1]);
for (int i = 0; i < 8; i++) {
if (neighbors[i] == sDisassembled[1]) {
CheckingString checkingString = getWordByDirection(crossword, i, startCoords[0], startCoords[1], s.length() - 1);
Word currentWord = new Word(s);
if (s.equals(checkingString.getValue())) {
currentWord.setStartPoint(startCoords[1], startCoords[0]);
currentWord.setEndPoint(checkingString.getxPos(), checkingString.getyPos());
}
result.add(currentWord);
}
}
}
}
//home - (5, 3) - (2, 0)
for (Word currentWord : result) {
System.out.println(currentWord.text + " - (" + currentWord.startX + ", " + currentWord.startY +
") - (" + currentWord.endX + ", " + currentWord.endY + ")");
}
System.out.println(result.size());
return result;
}
public static CheckingString getWordByDirection(int[][] crossword, int direction, int yPos, int xPos, int charCount) {
String result = "" + (char) crossword[yPos][xPos];
switch (direction) {
case 0 : {
if (charCount <= yPos) {
while (charCount > 0) {
yPos--;
result += (char) crossword[yPos][xPos];
charCount--;
}
}
break;
}
case 1 : {
if ((charCount <= yPos) && (charCount < crossword[yPos].length - xPos)) {
while (charCount > 0) {
yPos--;
xPos++;
result += (char) crossword[yPos][xPos];
charCount--;
}
}
break;
}
case 2 : {
if (charCount < crossword[yPos].length - xPos) {
while (charCount > 0) {
xPos++;
result += (char) crossword[yPos][xPos];
charCount--;
}
}
break;
}
case 3 : {
if ((charCount < crossword[yPos].length - xPos) && (charCount < crossword.length - yPos)) {
while (charCount > 0) {
yPos++;
xPos++;
result += (char) crossword[yPos][xPos];
charCount--;
}
}
break;
}
case 4 : {
if (charCount < crossword.length - yPos) {
while (charCount > 0) {
yPos++;
result += (char) crossword[yPos][xPos];
charCount--;
}
}
break;
}
case 5 : {
if ((charCount < crossword.length - yPos) && (charCount < xPos)) {
while (charCount > 0) {
yPos++;
xPos--;
result += (char) crossword[yPos][xPos];
charCount--;
}
}
break;
}
case 6 : {
if (charCount <= xPos) {
while (charCount > 0) {
xPos--;
result += (char) crossword[yPos][xPos];
charCount--;
}
}
break;
}
case 7 : {
if ((charCount <= yPos) && (charCount <= xPos)) {
while (charCount > 0) {
yPos--;
xPos--;
result += (char) crossword[yPos][xPos];
charCount--;
}
}
break;
}
}
return new CheckingString(yPos, xPos, result);
}
public static int[] getNeighbors(int[][] crossword, int yPos, int xPos) {
int [] result = new int[] {'.', '.', '.', '.', '.', '.', '.', '.'};
// 0 = up, 1 = upRight, 2 = right, 3 = downRight, 4 = down, 5 = downLeft, 6 = left, 7 = upLeft
if (yPos > 0) {
result[0] = crossword[yPos - 1][xPos]; // up
if (xPos < crossword[yPos].length - 1)
result[1] = crossword[yPos - 1][xPos + 1]; // upRight
if (xPos > 0) result[7] = crossword[yPos - 1][xPos - 1]; // upLeft
}
//
if (xPos < crossword[yPos].length - 1) {
result[2] = crossword[yPos][xPos + 1]; // right
if (yPos < crossword.length - 1)
result[3] = crossword[yPos + 1][xPos + 1]; //downRight
}
//
if (yPos < crossword.length - 1) {
result[4] = crossword[yPos + 1][xPos]; // down
if (xPos > 0) result[5] = crossword[yPos + 1][xPos - 1]; // downLeft
}
//
if (xPos > 0) result[6] = crossword[yPos][xPos - 1]; // left
//
return result;
}
public static class CheckingString {
private int xPos;
private int yPos;
private String value;
public CheckingString(int yPosArg, int xPosArg, String valueArg) {
this.value = valueArg;
this.xPos = xPosArg;
this.yPos = yPosArg;
}
public int getxPos() {
return xPos;
}
public int getyPos() {
return yPos;
}
public String getValue() {
return value;
}
}
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);
}
}
}