Помогите, пожалуйста, очень надо)
package com.javarush.task.task20.task2027;
import java.util.*;
import java.io.*;
import java.text.*;
/*
Кроссворд
*/
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'}
};
detectAllWords(crossword, "home", "emoh", "same","emas","fulm","mluf","oprek","kerpo");
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List<Word> result = new ArrayList<>();
char[][] charword = new char[crossword.length][crossword[0].length];
for (int i = 0; i < crossword.length; i++) {
for (int j = 0; j < crossword[0].length; j++) {
charword[i][j] = (char) crossword[i][j];
}
}
for (String str : words) {
// Word word = new Word(str);
for (int string = 0; string < crossword.length; string++) {
for (int row = 0; row < crossword[string].length; row++) {
if (charword[string][row] == str.charAt(0)) {
//south
int southSlovoLength = 0;
// String southSlovo = "";
for (int shift = 0; shift < str.length(); shift++) {
if ((string + str.length() - 1) >= 0 && (string + str.length() - 1) < 5 && charword[string + shift][row] == str.charAt(southSlovoLength)) { //SouthBorderNotCrossed
southSlovoLength++;
// southSlovo = southSlovo + str.charAt(string+shift);
continue;
} else {
break;
}
}
if (southSlovoLength == str.length()) { // &&southSlovo.equals(str)) {
Word word = new Word(str);
word.setStartPoint(row, string);
word.setEndPoint(row , string+ str.length() - 1);
result.add(word);
}
//north
int northSlovoLength = 0;
for (int shift = 0; shift < str.length(); shift++) {
if ((string - str.length() + 1) >= 0 && (string - str.length() + 1) < 5 && charword[string - shift][row] == str.charAt(northSlovoLength)) { //NorthBorderNotCrossed
northSlovoLength++;
continue;
} else {
break;
}
}
if (northSlovoLength == str.length()) {
Word word = new Word(str);
word.setStartPoint(row, string);
word.setEndPoint(row , string- str.length() + 1);
result.add(word);
}
//west
int westSlovoLength = 0;
for (int shift = 0; shift < str.length(); shift++) {
if ((row - str.length() + 1) >= 0 && (row - str.length() + 1) < 6 && charword[string][row - shift] == str.charAt(westSlovoLength)) { //WestBorderNotCrossed
westSlovoLength++;
continue;
} else {
break;
}
}
if (westSlovoLength == str.length()) {
Word word = new Word(str);
word.setStartPoint(row, string);
word.setEndPoint(row- str.length() + 1, string );
result.add(word);
}
//east
int eastSlovoLength = 0;
for (int shift = 0; shift < str.length(); shift++) {
if ((row + str.length() - 1 >= 0) && (row + str.length() - 1) < 6 && charword[string][row + shift] == str.charAt(eastSlovoLength)) { //EastBorderNotCrossed
eastSlovoLength++;
continue;
} else {
break;
}
}
if (eastSlovoLength == str.length()) {
Word word = new Word(str);
word.setStartPoint(row, string);
word.setEndPoint(row + str.length() - 1, string);
result.add(word);
}
//north-west
int nwSlovoLength = 0;
for (int shift = 0; shift < str.length(); shift++) {
if ((string - str.length() + 1) >= 0 && (string - str.length() + 1) < 5 && (row - str.length() + 1) >= 0 && (row - str.length() + 1) < 6 && charword[string - shift][row - shift] == str.charAt(nwSlovoLength)) { //NorthWestBorderNotCrossed
nwSlovoLength++;
continue;
} else {
break;
}
}
if (nwSlovoLength == str.length()) {
Word word = new Word(str);
word.setStartPoint(row, string);
word.setEndPoint(row - str.length() + 1, string - str.length() + 1);
result.add(word);
}
//north-east
int neSlovoLength = 0;
for (int shift = 0; shift < str.length(); shift++) {
if ((string - str.length() + 1) >= 0 && (string - str.length() + 1) < 5 && (row + str.length() - 1 >= 0) && (row + str.length() - 1) < 6 && charword[string - shift][row + shift] == str.charAt(neSlovoLength)) { //NorthEastBorderNotCrossed
neSlovoLength++;
continue;
} else {
break;
}
}
if (neSlovoLength == str.length()) {
Word word = new Word(str);
word.setStartPoint(row, string);
word.setEndPoint(row + str.length() - 1, string - str.length() + 1);
result.add(word);
}
//south-east
int seSlovoLength = 0;
for (int shift = 0; shift < str.length(); shift++) {
if ((string + str.length() - 1) >= 0 && (string + str.length() - 1) < 5 && (row + str.length() - 1 >= 0) && (row + str.length() - 1) < 6 && charword[string + shift][row + shift] == str.charAt(seSlovoLength)) { //SouthEastBorderNotCrossed
seSlovoLength++;
continue;
} else {
break;
}
}
if (seSlovoLength == str.length()) {
Word word = new Word(str);
word.setStartPoint(row, string);
word.setEndPoint(row + str.length() - 1, string + str.length() - 1);
result.add(word);
}
//south-west
int swSlovoLength = 0;
for (int shift = 0; shift < str.length(); shift++) {
if ((string + str.length() - 1) >= 0 && (string + str.length() - 1) < 5 && (row - str.length() + 1) >= 0 && (row - str.length() + 1) < 6 && charword[string + shift][row - shift] == str.charAt(swSlovoLength)) { //SouthWestBorderNotCrossed
swSlovoLength++;
continue;
} else {
break;
}
}
if (swSlovoLength == str.length()) {
Word word = new Word(str);
word.setStartPoint(row, string);
word.setEndPoint(row - str.length() + 1, string + str.length() - 1);
result.add(word);
}
}
}
}
}
return result;
}
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);
}
}
}