Hi there,
I'm not able to get this task validated. According to it "The list returned by detectAllWords is not the right size!" however when I test it, the size is the right one.
Some help please?
package com.codegym.task.task20.task2027;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/*
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<>();
// x and y size of the wordSearch array
int xLength = wordSearch.length - 1;
int yLength = wordSearch[0].length - 1 ;
// array to load the words
char[] wordSearched;
// First xPos of the wordSearch
int xPos;
// Last yPos of wordSearch
int yPos;
// Last position of x coord in wordSearch
int lastXPos;
// Last position of y coord in wordSearch
int lastYPos;
// To get control on movement on diagonal movements
int initXPos;
int initYPos;
// var to check if we have found a word
boolean found;
// 1.- Load the word in a char array
// 2.- Search the char array in every direction into wordSearch
// 2.1 From left to right
// 2.2 From right to left
// 2.3 From top to bottom
// 2.4 From bottom to top
// 2.5 From left-top to right-bottom
// 2.6 From left-bottom to right-top
// 2.7 From right-top to left-bottom
// 2.8 From right-bottom to left-top
// 3.- if it's found, set a new Word object
// 4.- Repeat the procedure as many times as words are in the arguments.
for(String word : words) {
wordSearched = word.toCharArray();
found = false;
char[] buffer = new char[wordSearched.length];
/*
********************************
*1.- Search from left to right
********************************
*/
xPos = 0;
yPos = 0;
do {
do {
lastXPos = xPos;
lastYPos = yPos;
for (int index = 0; index < buffer.length; index++) {
buffer[index] = (char) wordSearch[lastXPos][lastYPos];
lastYPos++;
}
lastYPos--;
if (Objects.deepEquals(buffer, wordSearched)) {
result.add(newWord(xPos, yPos, lastXPos, lastYPos, word));
found = true;
}
yPos++;
} while (lastYPos < yLength && !found);
xPos++;
yPos = 0;
} while ((xPos <= xLength) && !found);
/*
********************************
*2.-Search from right to left
********************************
*/
if (!found) {
xPos = 0;
yPos = yLength;
do {
do {
lastXPos = xPos;
lastYPos = yPos;
for (int index = 0; index < buffer.length; index++) {
buffer[index] = (char) wordSearch[lastXPos][lastYPos];
lastYPos--;
}
lastYPos++;
if (Objects.deepEquals(buffer, wordSearched)) {
result.add(newWord(xPos, yPos, lastXPos, lastYPos, word));
found = true;
}
yPos--;
} while (lastYPos > 0 && !found);
xPos++;
yPos = yLength;
} while (xPos <= xLength && !found);
}
/*
********************************
*3.-Search from top to bottom
********************************
*/
if(!found) {
xPos = 0;
yPos = 0;
do {
do {
lastXPos = xPos;
lastYPos = yPos;
for (int index = 0; index < buffer.length; index++) {
buffer[index] = (char) wordSearch[lastXPos][lastYPos];
lastXPos++;
}
lastXPos--;
if (Objects.deepEquals(buffer, wordSearched)) {
result.add(newWord(xPos, yPos, lastXPos, lastYPos, word));
found = true;
}
xPos++;
} while (lastXPos < xLength && !found);
yPos++;
xPos = 0;
} while (yPos <= yLength && !found);
}
/*
********************************
*4.-Search from bottom to top
********************************
*/
if(!found) {
xPos = xLength;
yPos = 0;
do {
do {
lastXPos = xPos;
lastYPos = yPos;
for (int index = 0; index < buffer.length; index++) {
buffer[index] = (char) wordSearch[lastXPos][lastYPos];
lastXPos--;
}
lastXPos++;
if (Objects.deepEquals(buffer, wordSearched)) {
result.add(newWord(xPos, yPos, lastXPos, lastYPos, word));
found = true;
}
xPos--;
} while (lastXPos > 0 && !found);
yPos++;
xPos = xLength;
} while (yPos <= yLength && !found);
}
/*
********************************
*5.-Search from left-top to right-bottom
********************************
*/
if(!found) {
xPos = 0;
yPos = 0;
initXPos = xPos;
initYPos = yPos;
do {
do {
do {
lastXPos = xPos;
lastYPos = yPos;
//
if ((xPos + buffer.length - 1) <= xLength &&
(yPos + buffer.length - 1) <= yLength) {
for (int index = 0; index < buffer.length; index++) {
buffer[index] = (char) wordSearch[lastXPos][lastYPos];
lastXPos++;
lastYPos++;
}
lastXPos--;
lastYPos--;
} else {
break;
}
if (Objects.deepEquals(buffer, wordSearched)) {
result.add(newWord(xPos, yPos, lastXPos, lastYPos, word));
found = true;
}
xPos++;
yPos++;
} while (lastYPos < yLength && lastXPos < xLength && !found);
if (initXPos == 0) {
initYPos++;
} else {
initXPos++;
initYPos = 0;
}
xPos = initXPos;
yPos = initYPos;
} while (xPos < xLength && yPos < yLength && !found);
initXPos++;
xPos = initXPos;
initYPos = 0;
yPos = initYPos;
} while (xPos <= xLength && !found);
}
/*
********************************
*6.-Search from right-bottom to left-top
********************************
*/
if(!found) {
xPos = xLength;
yPos = 0;
initXPos = xPos;
initYPos = yPos;
do {
do {
do {
lastXPos = xPos;
lastYPos = yPos;
//
if ((xPos - buffer.length + 1) >= 0 &&
(yPos + buffer.length - 1) <= yLength) {
for (int index = 0; index < buffer.length; index++) {
buffer[index] = (char) wordSearch[lastXPos][lastYPos];
lastXPos--;
lastYPos++;
}
lastXPos++;
lastYPos--;
} else {
break;
}
if (Objects.deepEquals(buffer, wordSearched)) {
result.add(newWord(xPos, yPos, lastXPos, lastYPos, word));
found = true;
}
xPos--;
yPos++;
} while (lastXPos >= 0 && lastYPos < yLength && !found);
if (initYPos == 0) {
initXPos--;
} else {
initXPos = xLength;
initYPos++;
}
xPos = initXPos;
yPos = initYPos;
} while (xPos >= 0 && yPos < yLength && !found);
initXPos = xLength;
xPos = initXPos;
initYPos++;
yPos = initYPos;
} while (yPos <= yLength && !found);
}
/*
********************************
*7.-Search from right-top to left-bottom
********************************
*/
if(!found) {
xPos = 0;
yPos = yLength;
initXPos = xPos;
initYPos = yPos;
do {
do {
do {
lastXPos = xPos;
lastYPos = yPos;
//
if ((xPos + buffer.length - 1) <= xLength &&
(yPos - buffer.length + 1) >= 0) {
for (int index = 0; index < buffer.length; index++) {
buffer[index] = (char) wordSearch[lastXPos][lastYPos];
lastXPos++;
lastYPos--;
}
lastXPos--;
lastYPos++;
} else {
break;
}
if (Objects.deepEquals(buffer, wordSearched)) {
result.add(newWord(xPos, yPos, lastXPos, lastYPos, word));
found = true;
}
xPos++;
yPos--;
} while (lastXPos < xLength && lastYPos < yLength && !found);
if (initXPos == 0 && initYPos >= 0) {
initYPos--;
} else {
initXPos++;
initYPos = yLength;
}
xPos = initXPos;
yPos = initYPos;
} while (xPos < xLength && !found);
initXPos++;
xPos = initXPos;
initYPos = yLength;
yPos = initYPos;
} while (yPos < yLength && !found);
}
/*
********************************
8.-Search from right-bottom to left-top
********************************
*/
if(!found) {
xPos = xLength;
yPos = yLength;
initXPos = xPos;
initYPos = yPos;
do {
do {
do {
lastXPos = xPos;
lastYPos = yPos;
//
if ((xPos - buffer.length + 1) >= 0 &&
(yPos - buffer.length + 1) >= 0) {
for (int index = 0; index < buffer.length; index++) {
buffer[index] = (char) wordSearch[lastXPos][lastYPos];
lastXPos--;
lastYPos--;
}
lastXPos++;
lastYPos++;
} else {
break;
}
if (Objects.deepEquals(buffer, wordSearched)) {
result.add(newWord(xPos, yPos, lastXPos, lastYPos, word));
found = true;
}
xPos--;
yPos--;
} while (lastXPos >= 0 && lastYPos >= 0 && !found);
if (initXPos == xLength && initYPos > 0) {
initYPos--;
} else {
initXPos--;
initYPos = yLength;
}
xPos = initXPos;
yPos = initYPos;
} while (yPos >= 0 && !found);
initXPos--;
xPos = initXPos;
initYPos = yLength;
yPos = initYPos;
} while (yPos >= 0 && !found);
}
}
return result;
}
public static Word newWord(int initXPoint, int initYPoint, int lastXPoint, int lastYPoint, String description){
Word word = new Word(description);
word.setStartPoint(initYPoint, initXPoint);
word.setEndPoint(lastYPoint, lastXPoint);
return word;
}
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);
}
}
}