Hello!
I'm completely stuck on this one. Usually hints in help section helped me get correct solution, but this time even tho code seems to work correctly, validator still doesn't let me in. I've tried with single word entry, multiple word entries, palindromes, single and multiple, and it still doesnt work for validator :/
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', 'x'},// 0
{'r', 'a', 'c', 'e', 'c', 'a', 'r'},// 1
{'l', 'n', 'g', 'r', 'o', 'v', 'a'},// 2
{'m', 'l', 'p', 'r', 'r', 's', 'w'},// 3
{'p', 'o', 'e', 'e', 'q', 'a', 'c'},// 4
{'u', 's', 'a', 's', 'a', 's', 'd'},// 5
{'h', 'a', 'n', 'n', 'a', 'h', 'a'},// 6
{'m', 'l', 's', 'r', 's', '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", "racecar", "hannah");
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> result = new ArrayList<>();
for (String word : words) {
/* List<Word> output = new ArrayList<>();
output.addAll(searchX(word, wordSearch));
output.addAll(searchY(word, wordSearch));
output.addAll(searchD(word, wordSearch));
result.add(output.get(0)); */
result.addAll(searchX(word, wordSearch));
result.addAll(searchY(word, wordSearch));
result.addAll(searchD(word, wordSearch));
}
return result;
}
public static List<Word> searchX(String word, int[][] wordSearch) {
List<Word> output = new ArrayList<>();
for (int y = 0; y < wordSearch.length; y++) {
for (int x = 0; x <= wordSearch[0].length - word.length(); x++) {
StringBuilder wrd = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
wrd.append((char) wordSearch[y][x+i]);
}
String fwd = wrd.toString();
String bck = wrd.reverse().toString();
if (fwd.equals(word) || bck.equals(word)) {
Word toAdd = new Word(word);
if (fwd.equals(word)) {
toAdd.setStartPoint(x, y);
toAdd.setEndPoint(x + word.length() - 1, y);
}
else {
toAdd.setStartPoint(x + word.length() - 1, y);
toAdd.setEndPoint(x, y);
}
if (fwd.equals(bck)) {
Word add2 = new Word((word));
add2.setStartPoint(x + word.length() - 1, y);
add2.setEndPoint(x, y);
output.add(add2);
}
output.add(toAdd);
}
}
}
return output;
}
public static List<Word> searchY(String word, int[][] wordSearch) {
List<Word> output = new ArrayList<>();
for (int y = 0; y <= wordSearch.length - word.length(); y++) {
for (int x = 0; x < wordSearch[0].length; x++) {
StringBuilder wrd = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
wrd.append((char) wordSearch[y+i][x]);
}
String fwd = wrd.toString();
String bck = wrd.reverse().toString();
if (fwd.equals(word) || bck.equals(word)) {
Word toAdd = new Word(word);
if (fwd.equals(word)) {
toAdd.setStartPoint(x, y);
toAdd.setEndPoint(x, y + word.length() - 1);
}
else {
toAdd.setStartPoint(x, y + word.length() - 1);
toAdd.setEndPoint(x, y);
}
if (fwd.equals(bck)) {
Word add2 = new Word((word));
add2.setStartPoint(x, y + word.length() - 1);
add2.setEndPoint(x, y);
output.add(add2);
}
output.add(toAdd);
}
}
}
return output;
}
public static List<Word> searchD(String word, int[][] wordSearch) {
List<Word> output = new ArrayList<>();
for (int y = 0; y <= wordSearch.length - word.length(); y++) {
for (int x = 0; x <= wordSearch[0].length - word.length(); x++) {
StringBuilder wrd = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
wrd.append((char) wordSearch[y+i][x+i]);
}
String fwd = wrd.toString();
String bck = wrd.reverse().toString();
if (fwd.equals(word) || bck.equals(word)) {
Word toAdd = new Word(word);
if (fwd.equals(word)) {
toAdd.setStartPoint(x, y);
toAdd.setEndPoint(x + word.length() - 1, y + word.length() - 1);
}
else {
toAdd.setStartPoint((x + word.length() - 1), (y + word.length() - 1));
toAdd.setEndPoint(x, y);
}
if (fwd.equals(bck)) {
Word add2 = new Word((word));
add2.setStartPoint(x + word.length() -1, y + word.length() - 1);
add2.setEndPoint(x, y);
output.add(add2);
}
output.add(toAdd);
}
}
}
for (int y = 0; y <= wordSearch.length - word.length(); y++) {
for (int x = wordSearch[0].length - 1; x >= word.length(); x--) {
StringBuilder wrd = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
wrd.append((char) wordSearch[y+i][x-i]);
}
String fwd = wrd.toString();
String bck = wrd.reverse().toString();
if (fwd.equals(word) || bck.equals(word)) {
Word toAdd = new Word(word);
if (fwd.equals(word)) {
toAdd.setStartPoint(x, y);
toAdd.setEndPoint(x - word.length() + 1, y + word.length() - 1);
}
else {
toAdd.setStartPoint((x - word.length() + 1), (y + word.length() - 1));
toAdd.setEndPoint(x, y);
}
if (fwd.equals(bck)) {
Word add2 = new Word((word));
add2.setStartPoint((x - word.length() + 1), (y + word.length() - 1));
add2.setEndPoint(x, y);
output.add(add2);
}
output.add(toAdd);
}
}
}
return output;
}
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);
}
}
}