I have gone through and tried all the different matrix options that other people have suggested. Heck, I've even tried with an unbalanced matrix and that seems to work when I run it, but I still can't seem to pass the final requirement. Any ideas on what is wrong?
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[][]{
// {'f', 'd', 'e', 'r', 'l', 'k'},
// {'u', 's', 'a', 'm', 'e', 'o'},
// {'l', 'n', 'b', 'r', 'o', 'v'},
// {'m', 'l', 'e', 'r', 'r', 'h'},
// {'p', 'r', 'e', 'e', 'd', 'j'},
// {'p', 'r', 'r', 'e'},
// {'q', 'q', 'e', 'm'},
// {'q', 'r'}
// };
// detectAllWords(wordSearch, "home", "same", "deer", "beer");
/*
Expected result
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
// int[][] wordSearch = new int[][]{
// {'a', 'b'},
// {'c', 'b'}
// };
// detectAllWords(wordSearch, "ac");
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', 'k', 'g', 'r', 'o', 'v', 'z'},// 2
{'m', 'l', 'p', 'r', 'r', 'h', 'w'},// 3
{'p', 'o', 'e', 'e', 'a', 'a', 'c'},// 4
{'u', 't', 'a', 'm', 'a', 'a', 'd'},// 5
{'l', 'k', 'g', 'r', 'n', '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
};
System.out.println(detectAllWords(wordSearch, "qsa", "usa", "nasa", "poeeaa"));
// detectAllWords(wordSearch, "nasa");
/*Expected result
[usa - (0, 1) - (2, 1), poeeaa - (0, 4) - (5, 4), nasa - (4, 6) - (4, 9), qsa - (3, 8) - (5, 8)]
*/
}
public static List<Word> detectAllWords(int[][] wordSearch, String... words) {
List<Word> wordsFound = new ArrayList<>();
boolean found = false;
int[] rowAdjust = {-1, 0, 1};
int[] colAdjust = {-1, 0, 1};
int letterCount = 0;
int rowArrayIndex;
int rowLoop;
int colArrayIndex;
int colLoop;
// boolean inBounds;
for (String word : words) {
// System.out.println(word);
// break the current word to be searched into a char array.
char[] wordChar = word.toCharArray();
// start looping through the matrix starting at 0,0
for (int row = 0; row < wordSearch.length; row++) {
for (int col = 0; col < wordSearch[row].length; col++) {
// find first letter
char currentLetter = wordChar[letterCount];
if (wordSearch[row][col] == currentLetter) {
// System.out.println("first letter of " + word + " found. It is " + currentLetter + ". at matrix " + row + ", " + col);
letterCount = 1;
// inBounds = true;
found = true;
// see if the word will fit in the space regardless of direction by looping the directions
for (rowLoop = 0; rowLoop < rowAdjust.length; rowLoop++) {
// while (found) {
for (colLoop = 0; colLoop < colAdjust.length; colLoop++) {
// while (found) {
// start checking in each direction to see if we will be out of bounds
// get the length of the word.
int rowWordLength = word.length();
int colWordLength = word.length();
// if we are adjusting using a negative, make the word length negative.
rowArrayIndex = rowAdjust[rowLoop];
rowWordLength = rowWordLength * rowArrayIndex;
colArrayIndex = colAdjust[colLoop];
colWordLength = colWordLength * colArrayIndex;
found = true;
// check to see if both row and col lengths are 0. This would indicate we won't be doing anything so skip it.
if (rowWordLength == 0 && colWordLength == 0) {
// inBounds = false;
// letterCount = 0;
found = false;
rowArrayIndex = 0;
colArrayIndex = 0;
}
while (found) {
// System.out.println("The index " + letterCount + " letter of " + word + " found. It is " + wordChar[letterCount]);
// System.out.println("lettercount is " + letterCount + ". Word Length minus 1 is " + (word.length() - 1));
// System.out.println("index are, Row: " + rowArrayIndex + " --- col: " + colArrayIndex);
// match the current cell with the word character
try {
if (wordSearch[row + rowArrayIndex][col + colArrayIndex] == wordChar[letterCount]) {
// System.out.println("is try working?");
if (letterCount == word.length() - 1) {
// System.out.println("word " + word + " found. Saving location");
Word foundWord = new Word(word);
foundWord.setStartPoint(col, row);
foundWord.setEndPoint(col + colArrayIndex, row + rowArrayIndex);
// foundWord.text = word;
// (word, row, col, row + rowArrayIndex, col + colArrayIndex);
// System.out.println(foundWord.toString());
found = false;
// inBounds = false;
letterCount = 0;
wordsFound.add(foundWord);
}
if (colArrayIndex > 0) {
colArrayIndex++;
// System.out.println("ColArrayIndex is now " + colArrayIndex);
} else if (colArrayIndex < 0) {
colArrayIndex--;
// System.out.println("ColArrayIndex is now " + colArrayIndex);
}
if (rowArrayIndex > 0) {
rowArrayIndex++;
// System.out.println("RowArrayIndex is now " + rowArrayIndex);
} else if (rowArrayIndex < 0) {
rowArrayIndex--;
// System.out.println("RowArrayIndex is now " + rowArrayIndex);
}
letterCount++;
// System.out.println("LetterCoutn is now " + letterCount);
} else {
found = false;
// inBounds = false;
letterCount = 1;
}
} catch (ArrayIndexOutOfBoundsException e) {
// System.out.println("out of bounds");
found = false;
}
}
}
}
letterCount = 0;
}
}
}
}
return wordsFound;
}
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);
}
}
}