Tested with this.(.Words that are diagonal and backwards are being added incorrectly. I tested with an Array such as:)This is fixed..
{'f', 'd', 'e', 'h', 'l', 'k'},
{'u', 's', 'o', 'g', 'e', 'o'},
{'l', 'm', 'g', 'r', 'o', 'v'},
{'e', 'l', 'p', 'r', 'r', 'h'},
{'p', 'o', 'e', 'e', 'j', 'j'}
But still not verifying..Can you help.
package com.codegym.task.task20.task2027;
import java.util.ArrayList;
import java.util.List;
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'}
};
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> result = new ArrayList<>();
for (String word : words) {
result.addAll(verticalSearch(wordSearch, word));
result.addAll(horizontalSearch(wordSearch, word));
result.addAll(diagonalSearch(wordSearch, word));
}
return result;
}
private static List<Word> diagonalSearch(int[][] wordSearch, String word)
{
List<Word> result = new ArrayList<>();
for (int x = 0; x <= wordSearch.length - word.length(); x++)
{
for (int y = 0; y <= wordSearch[0].length - word.length(); y++)
{
StringBuilder attempt1 = new StringBuilder();
StringBuilder attempt2 = new StringBuilder();
for (int i = 0; i < word.length(); i++)
{
attempt1.append((char)wordSearch[x + i][y + i]);
attempt2.append((char)wordSearch[x + word.length() - i - 1][y + i]);
}
String a1 = attempt1.toString();
String a1r = attempt1.reverse().toString();
String a2 = attempt2.toString();
String a2r = attempt2.reverse().toString();
if (a1.equals(word) || a1r.equals(word) || a2.equals(word) || a2r.equals(word))
{
Word foundWord = new Word(word);
if (a1.equals(word))
{
foundWord.setStartPoint(y, x);
foundWord.setEndPoint(y + word.length() - 1,x + word.length() - 1);
}
else if (a1r.equals(word))
{
foundWord.setStartPoint(y + word.length() - 1,x + word.length() - 1);
foundWord.setEndPoint(y, x);
// foundWord.setStartPoint(y,x);
// foundWord.setEndPoint(y + word.length() - 1,x + word.length() - 1);
} else if (a2.equals(word))
{
foundWord.setStartPoint(y + word.length() - 1, x);
foundWord.setEndPoint(y,x + word.length() - 1);
} else if (a2r.equals(word))
{
// foundWord.setStartPoint(y,x+ word.length() - 1);
// foundWord.setEndPoint(y + word.length() - 1, x);
foundWord.setStartPoint(y + word.length() - 1, x);
foundWord.setEndPoint(y,x+ word.length() - 1);
}
result.add(foundWord);
}
}
}
for(int i=0;i<result.size();i++){
System.out.println(result.get(i));
}
return result;
}
//Vertical search
private static List<Word> verticalSearch(int[][] wordSearch, String word) {
List<Word> result = new ArrayList<>();
for (int x = 0; x <= wordSearch.length - word.length(); x++) {
for (int y = 0; y < wordSearch[0].length; y++) {
StringBuilder attempt = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
attempt.append((char)wordSearch[x + i][y]);
}
String a = attempt.toString();
String ar = attempt.reverse().toString();
if (a.equals(word) || ar.equals(word)) {
Word foundWord = new Word(word);
if (a.equals(word)) {
foundWord.setStartPoint(y, x);
foundWord.setEndPoint( y+ word.length() - 1, x);
} else if (ar.equals(word)) {
foundWord.setStartPoint(y+ word.length() - 1, x);
foundWord.setEndPoint(y, x);
}
result.add(foundWord);
}
}
}
return result;
}
//horizontal search
private static List<Word> horizontalSearch(int[][] wordSearch, String word) {
List<Word> result = new ArrayList<>();
for (int x = 0; x < wordSearch.length; x++) {
for (int y = 0; y <= wordSearch[0].length - word.length(); y++) {
StringBuilder attempt = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
attempt.append((char)wordSearch[x][y + i]);
}
String a = attempt.toString();
String ar = attempt.reverse().toString();
if (a.equals(word) || ar.equals(word)) {
Word foundWord = new Word(word);
if (a.equals(word)) {
foundWord.setStartPoint(y, x);
foundWord.setEndPoint( x+ word.length() - 1,y);
} else if (ar.equals(word)) {
foundWord.setStartPoint( x + word.length() - 1,y);
foundWord.setEndPoint(y, x);
}
result.add(foundWord);
}
}
}
for(int i=0;i<result.size();i++){
System.out.println(result.get(i));
}
return result;
}
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);
}
}
}