Понимаю что много копи-говнокода.) Но почему не проходит валидацию, тестил с разными словами с 1-й буквой, проверял на повторимость слов..
Хоть убейте не пойму что не так.. Тест с "one" с "rr" все ок.. мля
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'}
};
List<Word> S = detectAllWords(crossword, "home", "same");
for (Word i: S){
System.out.println(i.toString());
}
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List<Word> list = new ArrayList<>();
String[] Words = words;
for (int c = 0; c < Words.length; c++) {
char[] ch = Words[c].toCharArray();
Word obj = null;
Boolean nextWord = true;
for (int i = 0; i < crossword.length; i++) {
for (int j = 0; j < crossword[0].length && nextWord; j++) {
if (crossword[i][j] == ch[0]) { //found first word
if (ch.length == 1) {
list.add(obj = new Word(Words[c]));
obj.setStartPoint(i, j);
obj.setEndPoint(i, j);
nextWord = false;
}
try {
if (crossword[i - 1][j] == ch[1]) { //UP| **********************
for (int q = 1; q < ch.length && (i - q) >= 0; q++) {
if (crossword[i - q][j] == ch[q]) {
if (q == ch.length -1) {
list.add(obj = new Word(Words[c]));
obj.setStartPoint(j, i);
obj.setEndPoint(j, i - q);
nextWord = false;
}
}
}
}
} catch (Exception e) {
}
try {
if (crossword[i - 1][j + 1] == ch[1]) { // UPRight/ **********************
for (int q = 1; q < ch.length; q++) {
if (crossword[i - q][j + q] == ch[q]) {
if (q == ch.length -1) {
list.add(obj = new Word(Words[c]));
obj.setStartPoint(j, i);
obj.setEndPoint(j + q, i - q);
nextWord = false;
}
}
}
}
} catch (Exception e) {
}
try {
if (crossword[i][j + 1] == ch[1]) { // Right-- **********************
for (int q = 1; q < ch.length; q++) {
if (crossword[i][j + q] == ch[q]) {
if (q == ch.length -1) {
list.add(obj = new Word(Words[c]));
obj.setStartPoint(j, i);
obj.setEndPoint(j + q, i);
nextWord = false;
}
}
}
}
} catch (Exception e) {
}
try {
if (crossword[i + 1][j + 1] == ch[1]) { // RightDown \ **********************
for (int q = 1; q < ch.length; q++) {
if (crossword[i + q][j + q] == ch[q]) {
if (q == ch.length -1) {
list.add(obj = new Word(Words[c]));
obj.setStartPoint(j, i);
obj.setEndPoint(j + q, i + q);
nextWord = false;
}
}
}
}
} catch (Exception e) {
}
try {
if (crossword[i + 1][j] == ch[1]) { // Down | **********************
for (int q = 1; q < ch.length; q++) {
if (crossword[i + q][j] == ch[q]) {
if (q == ch.length -1) {
list.add(obj = new Word(Words[c]));
obj.setStartPoint(j, i);
obj.setEndPoint(j, i + q);
nextWord = false;
}
}
}
}
} catch (Exception e) {
}
try {
if (crossword[i + 1][j - 1] == ch[1]) { // DownLeft / **********************
for (int q = 1; q < ch.length; q++) {
if (crossword[i + q][j - q] == ch[q]) {
if (q == ch.length -1) {
list.add(obj = new Word(Words[c]));
obj.setStartPoint(j, i);
obj.setEndPoint(j - q, i + q);
nextWord = false;
}
}
}
}
} catch (Exception e) {
}
try {
if (crossword[i][j - 1] == ch[1]) { // Right - **********************
for (int q = 1; q < ch.length; q++) {
if (crossword[i][j - q] == ch[q]) {
if (q == ch.length -1) {
list.add(obj = new Word(Words[c]));
obj.setStartPoint(j, i);
obj.setEndPoint(j - q, i);
nextWord = false;
}
}
}
}
} catch (Exception e) {
}
try {
if (crossword[i - 1][j - 1] == ch[1]) { // UpLeft \ **********************
for (int q = 1; q < ch.length; q++) {
if (crossword[i - q][j - q] == ch[q]) {
if (q == ch.length -1) {
list.add(obj = new Word(Words[c]));
obj.setStartPoint(j, i);
obj.setEndPoint(j - q, i - q);
nextWord = false;
}
}
}
}
} catch (Exception e) {
}
}
}
}
String s = list.get(list.size() - 1).text;
if (!s.equals(Words[c])) {
list.add(obj = new Word(Words[c]));
obj.setStartPoint(0, 0);
obj.setEndPoint(0, 0);
}
}
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);
}
}
}