I test my code different words and arrays and get correct output, but the last condition is still unreached
package com.codegym.task.task20.task2027;
import java.util.List;
import java.util.ArrayList;
/*
Word search
*/
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'}
};
detectAllWords(wordSearch, "home", "same");
/*
Expected result
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] wordSearch, String... words) {
List<Word> list = new ArrayList<>();
int count = 0;
for (String word : words) {
list.add(new Word(word));
char[] chars = word.toCharArray();
for (int i = 0; i < wordSearch.length; i++) {
for (int j = 0; j < wordSearch[0].length; j++) {
int index = 0;
int y;
int x;
if (wordSearch[i][j] == (int)chars[index]) {
list.get(count).setStartPoint(j, i);
y = i;
x = j;
while (index < chars.length - 1) {
if (x + 1 < wordSearch[0].length) {
x++;
index++;
if (wordSearch[y][x] != (int)chars[index]) {
x = j;
index = 0;
break;
}
} else {
x = j;
index = 0;
break;
}
list.get(count).setEndPoint(x, y);
}
while (index < chars.length - 1) {
if (x - 1 >= 0) {
x--;
index++;
if (wordSearch[y][x] != (int)chars[index]) {
x = j;
index = 0;
break;
}
} else {
x = j;
index = 0;
break;
}
list.get(count).setEndPoint(x, y);
}
while (index < chars.length - 1) {
if (y - 1 >= 0) {
y--;
index++;
if (wordSearch[y][x] != (int)chars[index]) {
y = i;
index = 0;
break;
}
} else {
y = i;
index = 0;
break;
}
list.get(count).setEndPoint(x, y);
}
while (index < chars.length - 1) {
if (y + 1 < wordSearch.length) {
y++;
index++;
if (wordSearch[y][x] != (int)chars[index]) {
y = i;
index = 0;
break;
}
} else {
y = i;
index = 0;
break;
}
list.get(count).setEndPoint(x, y);
}
while (index < chars.length - 1) {
if (x + 1 < wordSearch[0].length && y + 1 < wordSearch.length) {
y++;
x++;
index++;
if (wordSearch[y][x] != (int)chars[index]) {
y = i;
x = j;
index = 0;
break;
}
} else {
y = i;
x = j;
index = 0;
break;
}
list.get(count).setEndPoint(x, y);
}
while (index < chars.length - 1) {
if (x - 1 >= 0 && y + 1 < wordSearch.length) {
y++;
x--;
index++;
if (wordSearch[y][x] != (int)chars[index]) {
y = i;
x = j;
index = 0;
break;
}
} else {
y = i;
x = j;
index = 0;
break;
}
list.get(count).setEndPoint(x, y);
}
while (index < chars.length - 1) {
if (x - 1 >= 0 && y - 1 >= 0) {
y--;
x--;
index++;
if (wordSearch[y][x] != (int)chars[index]) {
y = i;
x = j;
index = 0;
break;
}
} else {
y = i;
x = j;
index = 0;
break;
}
list.get(count).setEndPoint(x, y);
}
while (index < chars.length - 1) {
if (x + 1 < wordSearch[0].length && y - 1 >= 0) {
y--;
x++;
index++;
if (wordSearch[y][x] != (int)chars[index]) {
y = i;
x = j;
index = 0;
break;
}
} else {
y = i;
x = j;
index = 0;
break;
}
list.get(count).setEndPoint(x, y);
}
}
}
}
count++;
}
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);
}
}
}