First of all, sry my code looks like a mess.
So i know that it might be possible that a word starts on the same letter but i thought once a letter is used in a found word it is used up.
Example:
a b c d ef g hi
1 x x x x x x l x x
2 x x x x o x x x x
3 x x o x x x x x x
4 x c x x x x x x x
5 x x o x x x x x x
6 x x x x o x x x x
7 x x x x x x l x x
there are 2 possible ways to find the word "cool" here but they share a letter.
"cool (c4-g1)"
and
"cool (c4-g7)"
My code would stop if it found one of them.
Would it have to add both instances?
package com.codegym.task.task20.task2027;
import java.io.IOException;
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> list = detectAllWords(wordSearch, "home", "same");
for(Word entry : list){
System.out.println(entry.toString());
}
System.out.println(list.size());
/*
Expected result
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
System.out.println("\n2nd try:\n");
int[][] wordSearch2 = 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
};
List<Word> list2 = detectAllWords(wordSearch2, "qsa");
for(Word entry : list2){
System.out.println(entry.toString());
}
System.out.println(list2.size());
/*
Expected result
qsa - (3, 8) - (5, 8)
*/
}
public static List<Word> detectAllWords(int[][] wordSearch, String... words) {
List<Word> wordlist = new ArrayList<>();
int[][] copy = new int[wordSearch.length][wordSearch[0].length]; // copy the array for good form
for(int i1= 0; i1<copy.length; i1++) {
for (int i2 = 0; i2 < copy[i1].length; i2++){
copy[i1][i2] = wordSearch[i1][i2];
//System.out.print((char) copy[i1][i2]);
}
//System.out.println();
}
for(String word : words) // do this for every word in the list
{
char[] letters = word.toCharArray(); // split word into letters
for (int i1 = 0; i1 < copy.length; i1++) {
for (int i2 = 0; i2 < copy[i1].length; i2++) {
if(letters[0] == copy[i1][i2]){ //search for the starting letter of searched word
try{wordlist.add(checkPossible(word,copy,i1,i2));}
catch (IOException e){
e.printStackTrace();
}
}
}
}
}
// System.out.println(('f' == copy[0][0]));
return wordlist;
}
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);
}
}
public static Word checkPossible(String word, int[][] LArray, int v, int h) throws IOException { // v and h are switched, whatever
Word dummy = new Word(word);
boolean done = false; //condition if last letter is found, set endcoordinates and return if true
dummy.setStartPoint(h, v);
char[] letters = word.toCharArray();
try{ //check for vertical downwards
for(int i = 1; i<letters.length; i++){
if(LArray[v][h+i] != letters[i]){ //break if next letter not found
break;
}else if(i+1 == letters.length){
dummy.setEndPoint(h+i, v);
return dummy;
}
}
}catch (ArrayIndexOutOfBoundsException e){
}
try{ //check for vertical Upwards
for(int i = 1; i<letters.length; i++){
if(LArray[v][h-i] != letters[i]){
break;
}else if(i+1 == letters.length){
dummy.setEndPoint(h-i,v);
return dummy;
}
}
}catch (ArrayIndexOutOfBoundsException e){
}
try{ //check for Horizontal left
for(int i = 1; i<letters.length; i++){
if(LArray[v-i][h] != letters[i]){
break;
}else if(i+1 == letters.length){
dummy.setEndPoint(h,v-i);
return dummy;
}
}
}catch (ArrayIndexOutOfBoundsException e){
}
try{ //check for Horizontal right
for(int i = 1; i<letters.length; i++){
if(LArray[v+i][h] != letters[i]){ //break if next let
break;
}else if(i+1 == letters.length){
dummy.setEndPoint(h,v+i);
return dummy;
}
}
}catch (ArrayIndexOutOfBoundsException e){
}
try{ //check diagonally right up
for(int i = 1; i<letters.length; i++){
if(LArray[v+i][h-i] != letters[i]){
break;
}else if(i+1 == letters.length){
dummy.setEndPoint(h-i,v+i);
return dummy;
}
}
}catch (ArrayIndexOutOfBoundsException e){
}
try{
for(int i = 1; i<letters.length; i++){
if(LArray[v+i][h+i] != letters[i]){
break;
}else if(i+1 == letters.length){
dummy.setEndPoint(h+i, v+i);
return dummy;
}
}
}catch (ArrayIndexOutOfBoundsException e){
}
try{ //check diagonally Left up
for(int i = 1; i<letters.length; i++){
if(LArray[v-i][h-i] != letters[i]){
break;
}else if(i+1 == letters.length){
dummy.setEndPoint(h-i,v-i);
return dummy;
}
}
}catch (ArrayIndexOutOfBoundsException e){
}
try{ //check diagonally left down
for(int i = 1; i<letters.length; i++){
if(LArray[v-i][h+i] != letters[i]){
break;
}else if(i+1 == letters.length){
dummy.setEndPoint(h+i,v-i);
return dummy;
}
}
}catch (ArrayIndexOutOfBoundsException e){
}
throw new IOException();
}
}