I read somewhere, Java send variable containig object by address and thanks to that is possible to modify directly objects
I tried to make this and I don't understand but my returns always return "null" and not the object's address
With different System.out.println I could see I success to have the good final point, and for more details my code works like this :
i begin at the first letter then i use 2 distincts method :
- the first one, line 65, is looking for the second char. I split this method to the rest of my code in case there is multiple letters corresponding to my starting point,like that i can come back and search again. Moreover this method allows me to save the direction to follow for the search of the others chars
- the second one follows the direction and :
* returns null if the char doesn't correspond
* recalls itself if corresponding and if there is others letters
* returns the new Word with the ending points
For example in line 115 I succed to have : same - (0, 0) - (4, 1) and home - (0, 0) - (2, 0), but in line 88 i have null...
Thanks you for the help.
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', 'g', 'r', 'o', 'v'},
{'m', 'l', 'p', 'r', 'r', 'h'},
{'p', 'o', 'e', 'e', 'j', 'j'}
};
List<Word> w = new ArrayList<>();
w = detectAllWords(wordSearch, "home"/*, "same"*/);
System.out.println(w.size());
/*
Expected result
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] wordSearch, String... words) {
List<Word> wordsFound = new ArrayList<>();
for (String word : words) {
int indexLetter = 0;
int firstLetterSearch = word.charAt(0);
//String restLetters = word.substring(1);
repet : for (int i = 0; i < wordSearch.length; i++) {
for (int j = 0; j < wordSearch[i].length; j++) {
int letter = wordSearch[i][j];
if (firstLetterSearch == letter) {
Word w = detectSecondChar(word, indexLetter, j, i, wordSearch);
if (w != null) {
w.setStartPoint(j,i);
wordsFound.add(w);
//System.out.println(w);
break repet;
} else continue;
}
}
}
}
return wordsFound;
}
public static Word detectSecondChar(String word, int indexLetter, int x, int y, int[][] wordSearch) {
//String [] direction = {"HG", "H", "HD", "D", "BD", "B", "BG", "G"};
int index = ++indexLetter;
int secondLetterSearch = word.charAt(index);
for (int i = -1; i <= 1; i++) {
if ((i + y < 0) || (i + y == wordSearch.length)) continue;
for (int j = -1; j <= 1; j++) {
if ((j + x < 0) || (j + x == wordSearch[0].length)) continue;
if (i == 0 && j == 0) continue;
if (secondLetterSearch == wordSearch[i+y][j+x]) {
if (word.length() == 2) {
Word w = new Word(word);
w.setEndPoint(j+x,i+y);
return w;
} else {
int dx = j + x;
int dy = i + y;
System.out.println(dx + " " + dy);
Word w = detectChar(word,index, j, i, dx, dy, wordSearch);
System.out.println(w);
if (w != null) return w;
else continue;
}
}
}
} return null;
}
public static Word detectChar(String word, int id, int x, int y, int dpx, int dpy, int[][] wordSearch) {
int j = x + dpx;
int i = y + dpy;
System.out.println(j + ", " + i);
if (j < 0 || j == wordSearch.length || i < 0 || i == wordSearch.length) return null;
int index = ++id;
int c = word.charAt(index);
if (c == wordSearch[i][j]) {
System.out.println((char) c);
if (index == word.length() - 1) {
System.out.println(index);
Word w = new Word(word);
w.setEndPoint(j,i);
System.out.println(w);
return w;
} else {
Word w = detectChar(word,index, x, y, j, i, wordSearch);
}
}
return null;
}
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);
}
}
}