Валидатор пишет, что не верная длина списка!!
Уже не знаю как проверить. Вроде все работает
package com.javarush.task.task20.task2027;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
Кроссворд
*/
public class Solution {
public static void main(String[] args) {
int[][] crossword = new int[][]{
//{'o', 'p', 'e', 'r', 'l', 'k'},
//{'h', 's', 'a', 'm', 'e', 'o'},
//{'l', 'n', 's', 'r', 'o', 'v'},
//{'m', 'l', 'p', 'h', 'r', 'h'},
//{'p', 'r', 'e', 'm', 'o', 'h'}
{'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'},
{'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'}
};
//List<Word> result = detectAllWords(crossword, "home", "same", "emas");
List<Word> result = detectAllWords(crossword, "enone");
for (int i=0; i<result.size(); i++){
System.out.println(result.get(i));
}
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
//List<String> listWords = Arrays.asList(words);
List<Word> list = new ArrayList<Word>();
List<Word> listTemp = new ArrayList<Word>();
List<int[]> listOneLeter = new ArrayList<int[]>();
String s = "";
for (int k=0; k<words.length; k++){
Word word = new Word(words[k]);
s = word.text;
if (s.length()>1) {
listTemp = FinishList(crossword, s);
for (int i = 0; i < listTemp.size(); i++) {
list.add(listTemp.get(i));
}
} else {
// Нужно добавить однобуквенные слова
listOneLeter=FirstLetter(crossword,s);
//int[] coordinat = new int[2];
for (int i=0; i<listOneLeter.size(); i++){
int[] coordinat = listOneLeter.get(i);
Word word2 = new Word(s);
word2.setStartPoint(coordinat[1],coordinat[0]);
word2.setEndPoint(coordinat[1],coordinat[0]);
list.add(word2);
}
}
}
return list;
}
/** Создаем список слов совпадающих с заданым словом*/
private static List<Word> FinishList(int[][] crossword, String word){
List<Word> sortList = SortList(crossword, word);
int x1, y1, x2, y2;
for (int i=0; i<sortList.size(); i++) {
x1 = sortList.get(i).startX;
y1 = sortList.get(i).startY;
x2 = sortList.get(i).endX;
y2 = sortList.get(i).endY;
String s = sortList.get(i).text;
if (x1 == x2 && y2 > y1) {
/** Здесь проход циклом вниз */
int count = 2;
while (count < word.length()) {
y2++;
s = s + String.valueOf((char) crossword[y2][x2]);
sortList.get(i).setEndPoint(x2,y2);
count++;
}
Word wordAll = new Word(s);
wordAll.setStartPoint(x1, y1);
wordAll.setEndPoint(x2,y2);
sortList.set(i,wordAll);
} else if (x1 == x2 && y2 < y1) {
/** Здесь проход циклом вверх */
int count = 2;
while (count < word.length()) {
y2--;
s = s + String.valueOf((char) crossword[y2][x2]);
sortList.get(i).setEndPoint(x2,y2);
count++;
}
Word wordAll = new Word(s);
wordAll.setStartPoint(x1, y1);
wordAll.setEndPoint(x2,y2);
sortList.set(i,wordAll);
} else
if (x2 > x1 && y2 == y1) {
/** Здесь проход циклом вправо */
int count = 2;
while (count < word.length()) {
x2++;
s = s + String.valueOf((char) crossword[y2][x2]);
sortList.get(i).setEndPoint(x2,y2);
count++;
}
Word wordAll = new Word(s);
wordAll.setStartPoint(x1, y1);
wordAll.setEndPoint(x2,y2);
sortList.set(i,wordAll);
} else if (x2 > x1 && y2 < y1) {
/** Здесь проход циклом вправо и вверх*/
int count = 2;
while (count < word.length()) {
x2++;
y2--;
s = s + String.valueOf((char) crossword[y2][x2]);
sortList.get(i).setEndPoint(x2,y2);
count++;
}
Word wordAll = new Word(s);
wordAll.setStartPoint(x1, y1);
wordAll.setEndPoint(x2,y2);
sortList.set(i,wordAll);
} else if ( x2 > x1 && y2 > y1) {
/** Здесь проход циклом вправо и вниз*/
int count = 2;
while (count < word.length()) {
x2++;
y2++;
s = s + String.valueOf((char) crossword[y2][x2]);
sortList.get(i).setEndPoint(x2,y2);
count++;
}
Word wordAll = new Word(s);
wordAll.setStartPoint(x1, y1);
wordAll.setEndPoint(x2,y2);
sortList.set(i,wordAll);
} else
if (x2 < x1 && y2 == y1) {
/** Здесь проход циклом влево */
int count = 2;
while (count < word.length()) {
x2--;
s = s + String.valueOf((char) crossword[y2][x2]);
sortList.get(i).setEndPoint(x2,y2);
count++;
}
Word wordAll = new Word(s);
wordAll.setStartPoint(x1, y1);
wordAll.setEndPoint(x2,y2);
sortList.set(i,wordAll);
} else if (x2 < x1 && y2 < y1) {
/** Здесь проход циклом влево и вверх*/
int count = 2;
while (count < word.length()) {
x2--;
y2--;
s = s + String.valueOf((char) crossword[y2][x2]);
sortList.get(i).setEndPoint(x2,y2);
count++;
}
Word wordAll = new Word(s);
wordAll.setStartPoint(x1, y1);
wordAll.setEndPoint(x2,y2);
sortList.set(i,wordAll);
} else if (x2 < x1 && y2 > y1) {
/** Здесь проход циклом влево и вниз*/
int count = 2;
while (count < word.length()) {
x2--;
y2++;
s = s + String.valueOf((char) crossword[y2][x2]);
sortList.get(i).setEndPoint(x2,y2);
count++;
}
Word wordAll = new Word(s);
wordAll.setStartPoint(x1, y1);
wordAll.setEndPoint(x2,y2);
sortList.set(i,wordAll);
}
}
int j=0;
while (j<sortList.size()){
if (!sortList.get(j).text.equals(word)){
sortList.remove(j);
}else j++;
}
return sortList;
}
/** Отсеиваем из списка все пары первых и вторых букв продолжение которых не влезет в поле*/
private static List<Word> SortList(int[][] crossword, String word){
List<Word> sortList = TempList(crossword, word);
int x1, y1, x2, y2;
int i=0;
while (i<sortList.size()) {
x1 = sortList.get(i).startX;
y1 = sortList.get(i).startY;
x2 = sortList.get(i).endX;
y2 = sortList.get(i).endY;
if (x1 == x2 && y2 > y1) {
/** Здесь проход циклом вниз */
int count = 2;
boolean flag = true;
while (count < word.length() && flag) {
y2++;
count++;
if (y2 >= crossword.length-1) {
flag = false;
}
}
if (count < word.length()) {
sortList.remove(i);
} else i++;
} else if (x1 == x2 && y2 < y1) {
/** Здесь проход циклом вверх */
int count = 2;
boolean flag = true;
while (count < word.length() && flag) {
y2--;
count++;
if (y2 <= 0) {
flag = false;
}
}
if (count < word.length()) {
sortList.remove(i);
} else i++;
} else
if (x2 > x1 && y2 == y1) {
/** Здесь проход циклом вправо */
int count = 2;
boolean flag = true;
while (count < word.length() && flag) {
x2++;
count++;
if (x2 >= crossword[0].length-1) {
flag = false;
}
}
if (count < word.length()) {
sortList.remove(i);
} else i++;
} else if (x2 > x1 && y2 < y1) {
/** Здесь проход циклом вправо и вверх*/
int count = 2;
boolean flag = true;
while (count < word.length() && flag) {
x2++;
y2--;
count++;
if (x2 >= crossword[0].length-1 || y2 <= 0) {
flag = false;
}
}
if (count < word.length()) {
sortList.remove(i);
} else i++;
} else if ( x2 > x1 && y2 > y1) {
/** Здесь проход циклом вправо и вниз*/
int count = 2;
boolean flag = true;
while (count < word.length() && flag) {
x2++;
y2++;
count++;
if (x2 >= crossword[0].length-1 || y2 >= crossword.length-1) {
flag = false;
}
}
if (count < word.length()) {
sortList.remove(i);
} else i++;
} else
if (x2 < x1 && y2 == y1) {
/** Здесь проход циклом влево */
int count = 2;
boolean flag = true;
while (count < word.length() && flag) {
x2--;
count++;
if (x2 <= 0) {
flag = false;
}
}
if (count < word.length()) {
sortList.remove(i);
} else i++;
} else if (x2 < x1 && y2 < y1) {
/** Здесь проход циклом влево и вверх*/
int count = 2;
boolean flag = true;
while (count < word.length() && flag) {
x2--;
y2--;
count++;
if (x2 <= 0 || y2 <= 0) {
flag = false;
}
}
if (count < word.length()) {
sortList.remove(i);
} else i++;
} else if (x2 < x1 && y2 > y1) {
/** Здесь проход циклом влево и вниз*/
int count = 2;
boolean flag = true;
while (count < word.length() && flag) {
x2--;
y2++;
count++;
if (x2<= 0 || y2>= crossword.length-1) {
flag = false;
}
}
if (count < word.length()) {
sortList.remove(i);
} else i++;
}
}
return sortList;
}
/** Создаем список из всех пар первых и вторых букв*/
private static List<Word> TempList(int[][] crossword, String word){
List<Word> tempWord = new ArrayList<Word>();
List<int[]> first = FirstLetter(crossword, word);
for (int i=0; i<first.size(); i++){
int[] coord1 = first.get(i);
int x = coord1[1];
int y = coord1[0];
List<int[]> second = SecondLetter(crossword, word, first.get(i));
for (int j=0; j<second.size(); j++){
int[] coord2 = second.get(j);
int x2 = coord2[0];
int y2 = coord2[1];
String s = String.valueOf((char) crossword[y][x]) + String.valueOf((char) crossword[y2][x2]);
Word word1 = new Word(s);
word1.setStartPoint(x,y);
word1.setEndPoint(x2,y2);
tempWord.add(word1);
}
}
return tempWord;
}
/** Создаем список из возможных первых букв*/
private static List<int[]> FirstLetter(int[][] crossword, String word){
List<int[]> firstLetterList = new ArrayList<int[]>();
char[] wordChar = word.toCharArray();
for (int i=0; i<crossword.length; i++) {
for (int j = 0; j < crossword[i].length; j++) {
if ((char) crossword[i][j] == wordChar[0]) {
int[] coordinat = new int[2];
coordinat[0] = i;
coordinat[1] = j;
firstLetterList.add(coordinat);
}
}
}
return firstLetterList;
}
/** Создаем список из возможных вторых букв*/
private static List<int[]> SecondLetter(int[][] crossword, String word, int[] firstLetter){
List<int[]> secondLetterList = new ArrayList<int[]>();
char[] wordChar = word.toCharArray();
int i = firstLetter[0];
int j = firstLetter[1];
if (word.length()>1) {
for (int y = i - 1; y < i + 2; y++) {
for (int x = j - 1; x < j + 2; x++) {
if (!((j == x) && (i == y))) {
if ((x >= 0 && x < crossword[i].length) && (y >= 0 && y < crossword.length)) {
if ((char) crossword[y][x] == wordChar[1]) {
int[] coordinat = new int[2];
coordinat[0] = x;
coordinat[1] = y;
secondLetterList.add(coordinat);
}
}
}
}
}
}
return secondLetterList;
}
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);
}
}
}