Валидатор говорит, что размер списка неверен, как исправить?
Размер списка возвращаемого методом 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[][]{
{'x', 'y', 'r', 'd', 'e', 'h'},
{'x', 'y', 'm', 'm', 'o', 'e'},
{'x', 'y', 'r', 'm', 'f', 's'},
{'x', 'y', 'e', 'd', 'h', 'h'},// 1; 3, 4; 0
{'h', 'o', 'e', 'h', 'e', 'j'}
};
detectAllWords(crossword, "home", "same");
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
ArrayList<String> startpoints = new ArrayList<>();
ArrayList<String> endpoints = new ArrayList<>();
List<Word> list = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
Word word;
char[] chars = words[i].toCharArray();
if(chars.length == 1){
for (int j = 0; j < crossword.length; j++) {
for (int k = 0; k < crossword[0].length; k++) {
if(crossword[j] [k] == (int) chars[0]){
word = new Word(words[i]);
word.setStartPoint(k, j);
word.setEndPoint(k, j);
list.add(word);
}
}
}
}
else{
for (int j = 0; j < crossword.length; j++) {
for (int k = 0; k < crossword[0].length; k++) {
if(crossword[j] [k] == (int) chars[0]){
startpoints.add(k + " " + j);
}
if(crossword[j][k] == (int) chars[chars.length - 1]){
endpoints.add(k + " " + j);
}
}
}
for (int j = 0; j < startpoints.size(); j++) {
int x1 = Integer.parseInt(startpoints.get(j).split(" ")[0]);
int y1 = Integer.parseInt(startpoints.get(j).split(" ")[1]);
for (int k = 0; k < endpoints.size(); k++) {
int x2 = Integer.parseInt(endpoints.get(k).split(" ")[0]);
int y2 = Integer.parseInt(endpoints.get(k).split(" ")[1]);
if((x2 - x1 == chars.length - 1 || x1 - x2 == chars.length - 1 || x1 == x2) && (y2 - y1 == chars.length - 1 || y1 - y2 == chars.length - 1 || y1 == y2) && (x1 != x2 || y1 != y2)){
if(x1 == x2){
if(y1 > y2){
int y11 = y1;
int count = 0;
int count1 = 0;
for (int l = 1; l < chars.length - 1; l++) {
y11--;
count++;
if(crossword[y11][x1] == (int) chars[l]){
count1++;
}
}
if(count == count1){
word = new Word(words[i]);
word.setStartPoint(x1, y1);
word.setEndPoint(x2, y2);
list.add(word);
}
}
else{
int y11 = y1;
int count = 0;
int count1 = 0;
for (int l = 1; l < chars.length - 1; l++) {
y11++;
count++;
if(crossword[y11][x1] == (int) chars[l]){
count1++;
}
}
if(count == count1){
word = new Word(words[i]);
word.setStartPoint(x1, y1);
word.setEndPoint(x2, y2);
list.add(word);
}
}
}
if(y1 == y2){
if(x1 > x2){
int x11 = x1;
int count = 0;
int count1 = 0;
for (int l = 1; l < chars.length - 1; l++) {
x11--;
count++;
if(crossword[y1][x11] == (int) chars[l]){
count1++;
}
}
if(count == count1){
word = new Word(words[i]);
word.setStartPoint(x1, y1);
word.setEndPoint(x2, y2);
list.add(word);
}
}
else{
int x11 = x1;
int count = 0;
int count1 = 0;
for (int l = 1; l < chars.length - 1; l++) {
x11++;
count++;
if(crossword[y1][x11] == (int) chars[l]){
count1++;
}
}
if(count == count1){
word = new Word(words[i]);
word.setStartPoint(x1, y1);
word.setEndPoint(x2, y2);
list.add(word);
}
}
}
if(x1 - x2 == chars.length - 1 && y1 - y2 == chars.length - 1){
int x11 = x1;
int y11 = y1;
int count = 0;
int count1 = 0;
for (int l = 1; l < chars.length - 1; l++) {
x11--;
y11--;
count++;
if(crossword[y11][x11] == (int) chars[l]){
count1++;
}
}
if(count == count1){
word = new Word(words[i]);
word.setStartPoint(x1, y1);
word.setEndPoint(x2, y2);
list.add(word);
}
}
if(x2 - x1 == chars.length - 1 && y2 - y1 == chars.length - 1){
int x11 = x1;
int y11 = y1;
int count = 0;
int count1 = 0;
for (int l = 1; l < chars.length - 1; l++) {
x11++;
y11++;
count++;
if(crossword[y11][x11] == (int) chars[l]){
count1++;
}
}
if(count == count1){
word = new Word(words[i]);
word.setStartPoint(x1, y1);
word.setEndPoint(x2, y2);
list.add(word);
}
}
if(x2 - x1 == chars.length - 1 && y1 - y2 == chars.length - 1){
int x11 = x1;
int y11 = y1;
int count = 0;
int count1 = 0;
for (int l = 1; l < chars.length - 1; l++) {
x11++;
y11--;
count++;
if(crossword[y11][x11] == (int) chars[l]){
count1++;
}
}
if(count == count1){
word = new Word(words[i]);
word.setStartPoint(x1, y1);
word.setEndPoint(x2, y2);
list.add(word);
}
}
if(x1 - x2 == chars.length - 1 && y2 - y1 == chars.length - 1){
int x11 = x1;
int y11 = y1;
int count = 0;
int count1 = 0;
for (int l = 1; l < chars.length - 1; l++) {
x11--;
y11++;
count++;
if(crossword[y11][x11] == (int) chars[l]){
count1++;
}
}
if(count == count1){
word = new Word(words[i]);
word.setStartPoint(x1, y1);
word.setEndPoint(x2, y2);
list.add(word);
}
}
}
}
}
}
}
for (int i = 0; i < list.size(); i++) {
for (int j = i + 1; j < list.size(); j++) {
if(list.get(j).startX == list.get(i).startX && list.get(j).text == list.get(i).text && list.get(j).startY == list.get(i).startY && list.get(j).endX == list.get(i).endX && list.get(j).endY == list.get(i).endY){
list.remove(j);
}
}
}
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 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);
}
}
}