I added a few more words to search, so my main method looks like this:
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'}
};
List<Word> words = detectAllWords(wordSearch, "home", "same", "gae", "oprek", "sgrj", "kov", "ore", "emas");
/*
Expected result
home - (5, 3) - (2, 0) searchUpLeft
same - (1, 1) - (4, 1) searchRight
gae - (2, 2) - (2, 0) searchUp
oprek - (1, 4) - (5, 0) searchUpRight
sgrj - (1, 1) - (4, 4) searchDownRight
kov - (5, 0) = (5, 2) searchDown
ore - (4, 2) - (2, 4) searchDownLeft
emas - (4, 1) - (1, 1) searchLeft
*/
for (Word w : words)
System.out.println(w.toString());
}
I've added the expected results of my new 'words'
The output I'm getting is:
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
gae - (2, 2) - (2, 0)
oprek - (0, 0) - (0, 0)
sgrj - (0, 0) - (0, 0)
kov - (5, 0) - (5, 2)
ore - (0, 0) - (0, 0)
emas - (4, 1) - (1, 1)
Clearly, the problem is with the following methods:
searchUpRight
searchDownRight
searchDownLeft
Cannot for the life of me see what it is though - please helppackage 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', 'g', 'r', 'o', 'v'},
{'m', 'l', 'p', 'r', 'r', 'h'},
{'p', 'o', 'e', 'e', 'j', 'j'}
};
List<Word> words = detectAllWords(wordSearch, "home", "same");
/*
Expected result
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
for (Word w : words)
System.out.println(w.toString());
}
public static List<Word> detectAllWords(int[][] wordSearch, String... words) {
List<Word> wordList = new ArrayList<>();
ArrayList<char[]> wordChars = new ArrayList<>();
for (String s : words){
wordChars.add(s.toCharArray());
wordList.add(new Word(s));
}
for (int i = 0; i < wordChars.size(); i++){
char[] word = wordChars.get(i);
for (int j = 0; j < wordSearch.length; j++){
for (int k = 0; k < wordSearch[j].length; k++){
if (word[0] == wordSearch[j][k]){
if (searchUp(wordSearch, word, k, j)){
wordList.get(i).setStartPoint(k, j);
wordList.get(i).setEndPoint(k, j - (word.length -1));
} else if (searchUpRight(wordSearch, word, k, j)){
wordList.get(i).setStartPoint(k ,j);
wordList.get(i).setEndPoint(k + word.length - 1, j - (word.length -1));
} else if (searchRight(wordSearch, word, k ,j)){
wordList.get(i).setStartPoint(k, j);
wordList.get(i).setEndPoint(k + word.length -1, j);
} else if (searchDownRight(wordSearch, word, k, j)){
wordList.get(i).setStartPoint(k, j);
wordList.get(i).setEndPoint(k + word.length -1, j + word.length -1);
} else if (searchDown(wordSearch, word, k, j)){
wordList.get(i).setStartPoint(k, j);
wordList.get(i).setEndPoint(k, j + word.length -1);
} else if (searchDownLeft(wordSearch, word, k, j)){
wordList.get(i).setStartPoint(k, j);
wordList.get(i).setEndPoint(k - (word.length -1), j + word.length -1);
} else if (searchLeft(wordSearch, word, k, j)){
wordList.get(i).setStartPoint(k, j);
wordList.get(i).setEndPoint(k - (word.length -1), j);
} else if (searchUpLeft(wordSearch, word, k, j)){
wordList.get(i).setStartPoint(k, j);
wordList.get(i).setEndPoint(k - (word.length -1), j - (word.length -1));
}
}
}
}
}
return wordList;
}
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);
}
}
public static boolean searchUp(int[][] wordSearch, char[] word, int startX, int startY){
if (startY < word.length - 1) return false;
int letterIndex = 0;
int endY = startY - word.length;
for (int i = startY; i > endY; i--){
if (wordSearch[i][startX] != word[letterIndex]) return false;
letterIndex++;
}
return true;}
public static boolean searchUpRight(int[][] wordSearch, char[] word, int startX, int startY){
if (startY < word.length - 1 || startX + word.length >= wordSearch[startY].length) return false;
int letterIndex = 0;
int xIndex = startX;
int endY = startY - word.length;
for (int i = startY; i > endY; i--){
if (wordSearch[i][xIndex] != word[letterIndex]) return false;
letterIndex++;
xIndex++;
}
return true;}
public static boolean searchRight(int[][] wordSearch, char[] word, int startX, int startY){
if (startX + word.length >= wordSearch[startY].length) return false;
int letterIndex = 0;
int endX = startX + word.length;
for (int i = startX; i < endX; i++){
if (wordSearch[startY][i] != word[letterIndex]) return false;
letterIndex++;
}
return true;}
public static boolean searchDownRight(int[][] wordSearch, char[] word, int startX, int startY){
if (startX + word.length >= wordSearch[startY].length || startY + word.length >= wordSearch.length) return false;
int letterIndex = 0;
int xIndex = startX;
int endY = startY + word.length;
for (int i = startY; i < endY; i++){
if (wordSearch[i][xIndex] != word[letterIndex]) return false;
letterIndex++;
xIndex++;
}
return true;}
public static boolean searchDown(int[][] wordSearch, char[] word, int startX, int startY){
if (startY + word.length >= wordSearch.length) return false;
int letterIndex = 0;
int endY = startY + word.length;
for (int i = startY; i < endY; i++){
if (wordSearch[i][startX] != word[letterIndex]) return false;
letterIndex++;
}
return true;}
public static boolean searchDownLeft(int[][] wordSearch, char[] word, int startX, int startY){
if (startY + word.length >= wordSearch.length || startX < word.length - 1) return false;
int letterIndex = 0;
int xIndex = startX;
int endY = startY + word.length;
for (int i = startY; i < endY; i++){
if (wordSearch[i][xIndex] != word[letterIndex]) return false;
xIndex--;
letterIndex++;
}
return true;}
public static boolean searchLeft(int[][] wordSearch, char[] word, int startX, int startY){
if (startX < word.length -1) return false;
int letterIndex = 0;
int endX = startX - word.length;
for (int i = startX; i > endX; i--){
if (wordSearch[startY][i] != word[letterIndex]) return false;
letterIndex++;
}
return true;}
public static boolean searchUpLeft(int[][] wordSearch, char[] word, int startX, int startY){
if (startY < word.length - 1 || startX < word.length - 1) return false;
int letterIndex = 0;
int xIndex = startX;
int endY = startY - word.length;
for (int i = startY; i > endY; i--){
if (wordSearch[i][xIndex] != word[letterIndex]) return false;
xIndex--;
letterIndex++;
}
return true;}
}