Any clues as to why it's not verifying?
Also I can't think of a way to avoid all the repeated code. Thoughts?
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[][] wordSearch = 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
};
List<Word> words = detectAllWords(wordSearch, "qsa");
if (words != null) {
for (Word word : words) {
System.out.println(word);
}
}
/*
Expected result
qsa - (3, 8) - (5, 8)
*/
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'}
};
words = detectAllWords(wordSearch, "home", "same");
if (words != null) {
for (Word word : words) {
System.out.println(word);
}
}
/*
Expected result
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] wordSearch, String... words) {
List<Word> foundWords = new ArrayList<>();
for (String text : words) {
Word word = new Word(text);
int lastChar = text.length() - 1; // remaining characters in word (already at the first character)
for (int j = 0; j < wordSearch.length; j++) {
for (int k = 0; k < wordSearch[j].length; k++) {
if (wordSearch[j][k] == text.charAt(0)) {
word.setStartPoint(k, j);
boolean isWord;
// searches to the right across
if (k + lastChar < wordSearch[j].length) {
isWord = true;
for (int i = 0; i < text.length(); i++) {
if (wordSearch[j][k + i] != text.charAt(i)) {
isWord = false;
break;
}
}
if (isWord) {
word.setEndPoint(k + lastChar, j);
foundWords.add(word);
}
}
// searches right and down diagonally
if ((k + lastChar < wordSearch[j].length) && (j + lastChar < wordSearch.length)) {
isWord = true;
for (int i = 0; i < text.length(); i++) {
if (wordSearch[j + i][k + i] != text.charAt(i)) {
isWord = false;
break;
}
}
if (isWord) {
word.setEndPoint(k + lastChar, j + lastChar);
foundWords.add(word);
}
}
// searches down
if (j + lastChar < wordSearch.length) {
isWord = true;
for (int i = 0; i < text.length(); i++) {
if (wordSearch[j + i][k] != text.charAt(i)) {
isWord = false;
break;
}
}
if (isWord) {
word.setEndPoint(k, j + lastChar);
foundWords.add(word);
}
}
// searches down and left diagonally
if ((j + lastChar < wordSearch.length) && (k - lastChar >= 0)) {
isWord = true;
for (int i = 0; i < text.length(); i++) {
if (wordSearch[j + i][k - i] != text.charAt(i)) {
isWord = false;
break;
}
}
if (isWord) {
word.setEndPoint(k - lastChar, j + lastChar);
foundWords.add(word);
}
}
// searches left across
if (k - lastChar >= 0) {
isWord = true;
for (int i = 0; i < text.length(); i++) {
if (wordSearch[j][k - i] != text.charAt(i)) {
isWord = false;
break;
}
}
if(isWord) {
word.setEndPoint(k - lastChar, j);
foundWords.add(word);
}
}
// searches left and up diagonally
if ((k - lastChar >= 0) && (j - lastChar >= 0)) {
isWord = true;
for (int i = 0; i < text.length(); i++) {
if (wordSearch[j - i][k - i] != text.charAt(i)) {
isWord = false;
break;
}
}
if(isWord) {
word.setEndPoint(k - lastChar, j - lastChar);
foundWords.add(word);
}
}
// searches up
if (j - lastChar >= 0) {
isWord = true;
for (int i = 0; i < text.length(); i++) {
if (wordSearch[j - i][k] != text.charAt(i)) {
isWord = false;
break;
}
}
if(isWord) {
word.setEndPoint(k, j - lastChar);
foundWords.add(word);
}
}
// searches up and right diagonally
if ((j - lastChar >= 0) && (k + lastChar < wordSearch[j].length)) {
isWord = true;
for (int i = 0; i < text.length(); i++) {
if (wordSearch[j - i][k + i] != text.charAt(i)) {
isWord = false;
break;
}
}
if(isWord) {
word.setEndPoint(k + lastChar, j - lastChar);
foundWords.add(word);
}
}
}
}
}
}
return foundWords;
}
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);
}
}
}