Help pls
package com.codegym.task.task20.task2027;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/*
Word search
*/
public class Solution {
public static void main(String[] args) {
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', 'n', 'g', 'r', 'o', 'v', 'z'},// 2
{'m', 'l', 'p', 'r', 'r', 'h', 'w'},// 3
{'p', 'o', 'e', 'e', 'a', 'a', 'c'},// 4
{'u', 's', 'a', 'm', 'a', 'a', 'd'},// 5
{'l', 'n', 'g', 'r', 'a', '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
};
detectAllWords(wordSearch, "qsa");
/*
Expected result
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] wordSearch, String... words) {
int maxX = wordSearch[0].length, maxY = wordSearch.length;
LinkedHashMap<int[],Integer> wordPlacement;
List<Word> confirmedValues = new LinkedList<>();
for (String word :words){
wordPlacement = new LinkedHashMap<>();
int firstChar = word.charAt(0), lastChar = word.charAt(word.length()-1), wordLength = word.length();
boolean found = false;
for(int y = 0;y < maxY;y++){
if(found)break;
for(int x = 0; x < maxX; x++){
if(found) break;
if(wordSearch[y][x] == firstChar || wordSearch[y][x] == lastChar){
//check diag back
if(y + wordLength <= maxY && x + 1 - wordLength >= 0){
wordPlacement.clear();
wordPlacement.put(new int[]{x,y},wordSearch[y][x]);
int count = 1;
int y1= y+1;
for(int x1 = x - 1; x1 >= x+1-wordLength;x1--){
if(wordSearch[y1][x1] == word.charAt(count) ||wordSearch[y1][x1] == word.charAt(word.length()-1-count)){
count++;
wordPlacement.put(new int[]{x1,y1},wordSearch[y1][x1]);
y1++;
found = true;
} else{
found = false;
break;
}
}
if(found) {
confirmedValues.add(addWordToClass(wordPlacement,word));
break;
}
}
// check down
if(y + wordLength <= maxY){
wordPlacement.clear();
wordPlacement.put(new int[]{x,y},wordSearch[y][x]);
int count = 1;
for(int y1 = y + 1; y1 < y+wordLength;y1++){
if(wordSearch[y1][x] == word.charAt(count) ||wordSearch[y1][x] == word.charAt(word.length()-1-count)){
count++;
wordPlacement.put(new int[]{x,y1},wordSearch[y1][x]);
found = true;
} else{
found = false;
break;
}
}
if(found) {
confirmedValues.add(addWordToClass(wordPlacement,word));
break;
}
}
// check diag forward
if(y + wordLength <= maxY && x + wordLength <= maxX) {
wordPlacement.clear();
wordPlacement.put(new int[]{x,y},wordSearch[y][x]);
int count = 1;
int y1= y+1;
for(int x1 = x + 1; x1 < x+wordLength;x1++){
if(wordSearch[y1][x1] == word.charAt(count) ||wordSearch[y1][x1] == word.charAt(word.length()-1-count)){
count++;
wordPlacement.put(new int[]{x1,y1},wordSearch[y1][x1]);
y1++;
found = true;
} else{
wordPlacement.clear();
found = false;
break;
}
}
if(found) {
confirmedValues.add(addWordToClass(wordPlacement,word));
break;
}
}
// check forward
if(x + wordLength <= maxX){
wordPlacement.clear();
wordPlacement.put(new int[]{x,y},wordSearch[y][x]);
int count = 1;
for(int x1 = x + 1; x1 < x+wordLength;x1++){
if(wordSearch[y][x1] == word.charAt(count) ||wordSearch[y][x1] == word.charAt(word.length()-1-count)){
count++;
wordPlacement.put(new int[]{x1,y},wordSearch[y][x1]);
found = true;
} else{
found = false;
break;
}
}
if(found) {
confirmedValues.add(addWordToClass(wordPlacement,word));
break;
}
}
}
}
}
}
return confirmedValues;
}
private static Word addWordToClass(LinkedHashMap<int[], Integer> wordPlacement, String word) {
Word tempReturn = new Word(word);
boolean forwards = true;
int[] firstPlots = new int[2], lastplots = new int[2];
for(Map.Entry<int[],Integer> pair : wordPlacement.entrySet()){
firstPlots[0] = pair.getKey()[0];
firstPlots[1] = pair.getKey()[1];
break;
}
int count = 0;
for(Map.Entry<int[],Integer> pair : wordPlacement.entrySet()){
lastplots[0] = pair.getKey()[0];
lastplots[1] = pair.getKey()[1];
if(forwards)
if(pair.getValue() != word.charAt(count++))
forwards = false;
}
if(forwards){
tempReturn.setStartPoint(firstPlots[0],firstPlots[1]);
tempReturn.setEndPoint(lastplots[0],lastplots[1]);
}else
{
tempReturn.setEndPoint(firstPlots[0],firstPlots[1]);
tempReturn.setStartPoint(lastplots[0],lastplots[1]);
}
return tempReturn;
}
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);
}
}
}