1

I am struggling to implement a solution to finding all the words in a random 5x5 board of letters. Currently it is returning a few words but not nearly the full list. I am pretty sure that my problem exists within the findWords method with my for loops, but I can't figure out what to make the if statement to continue traversing in all 8 directions.

import java.io.File;
import java.util.*;
public class RandomWordGame {

    private static char[][] board = new char[5][5];
    private static Random r = new Random();
    private static ArrayList<String> dictionary = new ArrayList<String>();

    private static char[][] createBoard()
    {
        for (int i=0; i<board.length; i++)
        {
            for (int j=0; j<board.length; j++)
            {
                board[i][j] = (char) (r.nextInt(26) + 'a');
                System.out.print(board[i][j]);
            }
            System.out.println("");
        }
        System.out.println();
        return board;
    }
    public static ArrayList<String> solver(char[][] board)
    {
        if(board == null)
            System.out.println("Board cannot be empty");
        ArrayList<String> words = new ArrayList<String>();
        for(int i=0; i<board.length; i++)
        {
            for(int j=0; j<board[0].length; j++)
            {
                findWords(i, j, board[i][j] + "");
            }
        }
        return words;
    }
    public static void findWords(int i, int j, String currWord)
    {
        try
        {
            Scanner inputStream = new Scanner(new File("./dictionary.txt"));
            while(inputStream.hasNext())
            {
                dictionary.add(inputStream.nextLine());
            }
            inputStream.close();
        }catch(Exception e){
            e.printStackTrace();
        }

        for(i=0; i>=0 && i<board.length; i++)
        {
            for(j=0; j>=0; j++)
            {
                currWord += board[i][j];
                if(currWord.length()>5)
                    return;
                if(dictionary.contains(currWord))
                    System.out.println(currWord);
            }
        }
    }   
    public static void main(String[] args)
    {
        board = createBoard();
        ArrayList<String> validWords = RandomWordGame.solver(board);
        for(String word : validWords)
            System.out.println(word);
    }
}
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
JakeL
  • 11
  • 4
  • What does your dictionary file look like? – SME_Dev Jan 10 '17 at 14:19
  • _Currently it is returning a few words but not nearly the full list..._ Since the object `ArrayList words = new ArrayList();` is never populated in the posted code snippet, I highly doubt that it can contain any items. – SME_Dev Jan 10 '17 at 14:30
  • You need to not reinitialize i and j in your for loop. That won't fix everything though. looping through a 2D array in a linear fashion will miss most of the permutations you are looking for. Also, you should build the dictionary ONCE. You could do this in the main method. Right now your program remakes the exact same dictionary 25 times. – Sam Hazleton Jan 10 '17 at 14:55
  • I think if you run this code, but print currWord every time, rather than only when a match is found (so you can see what is being guessed), it will be highly illuminating. – Sam Hazleton Jan 10 '17 at 15:02

2 Answers2

0

There are a few things funny with this code. For one, you are always returning an empty ArrayList from your solver method, but that isn't what's killing you because you are printing each result from findWords.

The problem is that the findWords method just continuously adds letters starting from the top left of the puzzle.

//i=0 and j=0 means it will always start at the top left tile
for(i=0; i>=0 && i<board.length; i++)
{
    for(j=0; j>=0; j++)
    {
        //currWord is never reset, so it just keeps getting longer
        currWord += board[i][j];
        if(currWord.length()>5)
            return;
        if(dictionary.contains(currWord))
            System.out.println(currWord);
    }
}

Right now you are only finding words that start with your chosen tile, and the rest of the letters are picked in the same order they were added to the puzzle starting from the top left.

I would recommend spending some quality time with a pencil and paper, and gaining an intimate understanding of the relationship between two dimensional array indexes and their position in a grid.

Sam Hazleton
  • 470
  • 1
  • 5
  • 21
0

Java implementation using DFS approach

    import java.util.Arrays;

    public class WordBoggle {

    static int[] dirx = { -1, 0, 0, 1 };
    static int[] diry = { 0, -1, 1, 0 };

    public static void main(String[] args) {
        char[][] board = { { 'A', 'B', 'C', 'E' }, { 'S', 'F', 'C', 'S' }, { 'A', 'D', 'E', 'E' } };

        String word = "ABFSADEESCCEA";
        System.out.println(exist(board, word));
    }

    static boolean exist(char[][] board, String word) {
        if (board == null || board.length == 0 || word == null || word.isEmpty())
            return false;
        boolean[][] visited = new boolean[board.length][board[0].length];
        for (int i = 0; i < board.length; i++) {
            resetVisited(visited);
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == word.charAt(i)) {
                    return DFS(board, word, i, j, 1, visited);
                }
            }
        }
        return false;
    }

    static void resetVisited(boolean[][] visited) {
        for (int l = 0; l < visited.length; l++) {
            Arrays.fill(visited[l], false);
        }
    }

    static boolean DFS(char[][] board, String word, int i, int j, int k, boolean[][] visited) {
        visited[i][j] = true;
        if (k >= word.length())
            return true;
        for (int z = 0; z < 4; z++) {
            if (isValid(board, i + dirx[z], j + diry[z], visited)) {
                if (word.charAt(k) == board[i + dirx[z]][j + diry[z]]) {

                    return DFS(board, word, i + dirx[z], j + diry[z], k + 1, visited);
                }

            }
        }
        return false;
    }

    static boolean isValid(char[][] board, int i, int j, boolean[][] visited) {
        return (i >= 0 && i < board.length && j >= 0 && j < board[0].length && !visited[i][j]);
    }
}
Joe
  • 326
  • 3
  • 11