As I said, I was proud as punch. Just wrote down the code and after a good half hour tested if a usable result came out and bang.... perfect. Everything works right away. Only the validation does not want again. The list has not the right size. What? Here is the version in which I abort after a found word to have no double occurrences (label NexIt). No success either.
Does anyone have any ideas... I'm going to cry otherwise 😥 😭. Hopefully no one wants to be responsible for that. 😨
Pleeeeease...
package de.codegym.task.task20.task2027;
import java.util.ArrayList;
import java.util.List;
/*
Wortsuche
*/
public class Solution {
public static void main(String[] args) {
int[][] wordSearch = 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'}
};
System.out.println(detectAllWords(wordSearch, "home", "same"));
/*
Erwartetes Ergebnis
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] wordSearch, String... words) {
List<Word> result = new ArrayList<>();
// we search for each word in the words var args separately in a loop
for (String search : words) {
// 0. works only with > 2 char words
// 1. now iterate over the matrix and search for the first letter of the search word.
// 2. if found, look in each direction for the second letter.
// 3. if found, look further into that direction
// 4. if a whole word is found, add it to the result list
char[] word = search.toCharArray();
NextIt:
for (int y = 0; y < wordSearch.length; y++) {
for (int x = 0; x < wordSearch[y].length; x++) {
if (wordSearch[y][x] == word[0]) { // 1. first letter found
// 2. check all directions
for (int dirY = - 1; dirY <= 1; dirY++) {
for (int dirX = -1; dirX <= 1; dirX++) {
if (dirY == 0 && dirX == 0)
continue;
if (inMatrix(wordSearch, x + dirX, y + dirY)) { // step in the direction inside array bounds
if (wordSearch[y + dirY][x + dirX] == word[1]) { // 3. second char match, now we know a direction and check the rest
for (int step = 2; step < word.length; step++) { // we start to compare from the 3rd char to the end of the word
if (inMatrix(wordSearch, x + dirX * step, y + dirY * step)) {
if (wordSearch[y + dirY * step][x + dirX * step] == word[step]) {
if (word.length == step + 1) {// 4. end reached, add the word
Word current = new Word(search); // create the word
current.setStartPoint(x, y); // set the start position of the found word
current.setEndPoint(x + dirX * step, y + dirY * step); // set the start position of the found word
result.add(current);
break NextIt;
}
} else break; // chars don't match: break to for (dirX
} else break; // step not in matrix: break to for (dirX
}
}
}
}
}
}
}
}
}
return result;
}
private static boolean inMatrix(int[][] matrix, int x, int y) {
return (y >= 0 && y < matrix.length && x > 0 && x < matrix[y].length);
}
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);
}
}
}