Now I have method that return one word when I search for it , e.g
System.out.println(detectAllWords(wordSearch, "same"));
I get back only one directories, but I have three words "same" in table now.
It's the problem? Have I good understanding, that I need return every word from crossword?
If you see other problems in my code give me a hint :)
{'f', 'd', 'e', 'r', 'l', 'k'},
{'s', 's', 'a', 'm', 'e', 'o'},
{'a', 'n', 'a', 'r', 'o', 'v'},
{'m', 's', 'a', 'm', 'e', 'h'},
{'e', 'o', 'e', 'e', 'e', 'j'}
package com.codegym.task.task20.task2027;
import java.util.ArrayList;
import java.util.List;
/*
Word search
*/
public class Solution {
public static void main(String[] args) {
int[][] wordSearch2 = new int[][]{
//0 1 2 3 4 5 6
{'f', 'd', 'e', 'r', 'l', 'k', 'x'},// 0
{'u', 's', 'a', 'm', 'e', 'o', 'd'},// 1
{'l', 'n', 'g', 'r', 'o', 'v', 'z'},// 2
{'m', 'l', 'p', 'r', 'r', 'h', 'w'},// 3
{'p', 'o', 'e', 'e', 'a', 'a', 'c'},// 4
{'u', 's', 'a', 'm', 'a', 'a', 'd'},// 5
{'l', 'n', 'g', 'r', 'a', 'a', 'z'},// 6
{'m', 'l', 'p', 'r', 'a', 'a', 'w'},// 7
{'p', 'o', 'e', 'q', 's', 'a', 'c'},// 8
{'f', 'd', 'e', 'r', 'a', 'a', 'x'},// 9
};
/*
Expected result
qsa - (3, 8) - (5, 8)
{'f', 'd', 'e', 'r', 'l', 'k'},
{'u', 's', 'a', 'm', 'e', 'o'}, // (j = 1, i = 1) (y = 1, x = 2) --> x only one direction
{'l', 'n', 'a', 'r', 'o', 'v'},
{'m', 'l', 'p', 'm', 'r', 'h'},
{'p', 'o', 'e', 'e', 'e', 'j'}
*/
int[][] wordSearch = new int[][]{
{'f', 'd', 'e', 'r', 'l', 'k'},
{'u', 's', 'a', 'm', 'e', 'o'}, // (j = 1, i = 1) (y = 1, x = 2) --> x only one direction
{'l', 'n', 'a', 'r', 'o', 'v'},
{'m', 'l', 'p', 'm', 'r', 'h'},
{'p', 'o', 'e', 'e', 'e', 'j'}
};
System.out.println(detectAllWords(wordSearch, "poe", "ok", "uff"));
// System.out.println(detectAllWords(wordSearch2, "qsa"));
/*
Expected result
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
System.out.println("test");
}
public static List<Word> detectAllWords(int[][] wordSearch, String... words) {
// search for each word separately
List<Word> listOfWords = new ArrayList<>();
for (String word : words) {
Word word1 = new Word(word);
word1.text = word;
char[] ch = word.toCharArray();
int lengthThisWord = word.length();
// firstly going through matrix
for (int j = 0; j < wordSearch.length; j++) {
for (int i = 0; i < wordSearch[j].length; i++) {
int f = 0;
// when found first letter from this word
if (ch[f] == wordSearch[j][i]) {
// check if maybe word have only one letter (btw it's word at all in that situation?)
if (f == lengthThisWord - 1) {
// break;
word1.setStartPoint(i, j);
word1.setEndPoint(i, j);
listOfWords.add(word1);
} else {
// until I got whole word!
// now going through neighbors looking for another good characters
Coordinates coordinates;
int y = j;
int x = i;
// going to next character until method searchNeighbor() return null
f++;
if (searchNeighbor(wordSearch, new Coordinates(y, x), ch[f]) != null) {
coordinates = searchNeighbor(wordSearch, new Coordinates(y, x), ch[f]);
if (f == lengthThisWord - 1) {
word1.setStartPoint(i, j);
word1.setEndPoint(coordinates.x, coordinates.y);
listOfWords.add(word1);
} else {
f++;
// now we have 2 found characters, and we'll must see for another,
// but we know direction
if (y == coordinates.y && x == coordinates.x - 1) {
// searchRight
y = coordinates.y;
x = coordinates.x + 1;
while (f < lengthThisWord && x < wordSearch[0].length && wordSearch[y][x] == ch[f]) {
if (f == lengthThisWord - 1) {
word1.setStartPoint(i, j);
word1.setEndPoint(x, y);
listOfWords.add(word1);
}
f++;
x++;
}
}
if (y == coordinates.y && x == coordinates.x + 1) {
// searchLeft
y = coordinates.y;
x = coordinates.x - 1;
while (f < lengthThisWord && x >= 0 && wordSearch[y][x] == ch[f]) {
if (f == lengthThisWord - 1) {
word1.setStartPoint(i, j);
word1.setEndPoint(x, y);
listOfWords.add(word1);
}
f++;
x--;
}
}
if (y == coordinates.y - 1 && x == coordinates.x) {
// searchDown
y = coordinates.y + 1;
x = coordinates.x;
while (f < lengthThisWord && y < wordSearch.length && wordSearch[y][x] == ch[f]) {
if (f == lengthThisWord - 1) {
word1.setStartPoint(i, j);
word1.setEndPoint(x, y);
listOfWords.add(word1);
}
f++;
y++;
}
}
if (y == coordinates.y + 1 && x == coordinates.x) {
// searchUp
y = coordinates.y - 1;
x = coordinates.x;
while (f < lengthThisWord && y >= 0 && wordSearch[y][x] == ch[f]) {
if (f == lengthThisWord - 1) {
word1.setStartPoint(i, j);
word1.setEndPoint(x, y);
listOfWords.add(word1);
}
f++;
y--;
}
}
if (y == coordinates.y + 1 && x == coordinates.x + 1) {
// searchUpLeft
y = coordinates.y - 1;
x = coordinates.x - 1;
while (f < lengthThisWord && x >= 0 && y >= 0 && wordSearch[y][x] == ch[f]) {
if (f == lengthThisWord - 1) {
word1.setStartPoint(i, j);
word1.setEndPoint(x, y);
listOfWords.add(word1);
}
f++;
y--;
x--;
}
}
if (y == coordinates.y + 1 && x == coordinates.x - 1) {
// searchUpRight
y = coordinates.y - 1;
x = coordinates.x + 1;
while (f < lengthThisWord && y >= 0 && x < wordSearch[0].length && wordSearch[y][x] == ch[f]) {
if (f == lengthThisWord - 1) {
word1.setStartPoint(i, j);
word1.setEndPoint(x, y);
listOfWords.add(word1);
}
f++;
y--;
x++;
}
}
if (y == coordinates.y - 1 && x == coordinates.x + 1) {
// searchDownLeft
y = coordinates.y + 1;
x = coordinates.x - 1;
while (f < lengthThisWord && y < wordSearch.length && x >= 0 && wordSearch[y][x] == ch[f]) {
if (f == lengthThisWord - 1) {
word1.setStartPoint(i, j);
word1.setEndPoint(x, y);
listOfWords.add(word1);
}
f++;
y++;
x--;
}
}
if (y == coordinates.y - 1 && x == coordinates.x - 1) {
// searchDownRight
y = coordinates.y + 1;
x = coordinates.x + 1;
while (f < lengthThisWord && x < wordSearch[0].length && y < wordSearch.length && wordSearch[y][x] == ch[f]) {
if (f == lengthThisWord - 1) {
word1.setStartPoint(i, j);
word1.setEndPoint(x, y);
listOfWords.add(word1);
}
f++;
y++;
x++;
}
}
}
}
}
}
}
}
}
return listOfWords;
}
// method searching for accurate neighbor
public static Coordinates searchNeighbor(int[][] wordSearch, Coordinates co, char ch) {
for (int y = co.y - 1; y <= co.y + 1; y++) {
for (int x = co.x - 1; x <= co.x + 1; x++) {
if (y < 0 || y >= wordSearch.length)
continue;
if (x < 0 || x >= wordSearch[0].length)
continue;
if (wordSearch[y][x] == ch) {
return new Coordinates(y, x);
}
}
}
return null;
}
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);
}
}
}