So I need to write a program that will find all of the words

So, I need to write a program that will find all of the words in a crossword, including reversed and both horizontal or vertical. (no diagonal words). I have to store the puzzle in a two-dimensional char array, and then can only process one line at a time and one column at a time. And finally, a function that determines if the string of letters is a word or not. I have an file with over 400 words in the dictionary ( 4 letters only).

heres the file name

dictionary_four_letter_words

Here is the word puzzle.

pxabackegs
esorelated
vmoonkcent
zrllablead
msrkradmcf
floortjukv
strxmicedc
qutamktsud
rxquitmmuy
datasetlas

Solution

import java.util.HashSet;

import java.util.LinkedHashSet;

import java.util.Set;

public class WordPuzzle {

    public Set<String> findWords(char[][] puzzle, Set<String> words) {

        Set<String> foundWords = new HashSet<String>();

        int minimumWordLength = findMinimumWordLength(words);

        Set<String> possibleWords = findPossibleWords(puzzle, minimumWordLength);

        for(String word : words) {

            for(String possibleWord : possibleWords) {

                if(possibleWord.contains(word) || possibleWord.contains(new StringBuffer(word).reverse())) {

                    foundWords.add(word);

                    break;

                }

            }

        }      

        return foundWords;

    }

    private int findMinimumWordLength(Set<String> words) {

        int minimumLength = Integer.MAX_VALUE;

        for(String word : words) {

            if(word.length() < minimumLength)

                minimumLength = word.length();

        }

        return minimumLength;

    }

    private Set<String> findPossibleWords(char[][] puzzle, int minimumWordLength) {

        Set<String> possibleWords = new LinkedHashSet<String>();

        int dimension = puzzle.length; //Assuming puzzle is square

        if(dimension >= minimumWordLength) {

            /* Every row in the puzzle is added as a possible word holder */

            for(int i = 0; i < dimension; i++) {

                if(puzzle[i].length >= minimumWordLength) {

                    possibleWords.add(new String(puzzle[i]));

                }

            }

            /* Every column in the puzzle is added as a possible word holder */

            for(int i = 0; i < dimension; i++) {

                StringBuffer temp = new StringBuffer();

                for(int j = 0; j < dimension; j++) {

                    temp = temp.append(puzzle[j][i]);

                }

                possibleWords.add(new String(temp));

            }

            /* Adding principle diagonal word holders */

            StringBuffer temp1 = new StringBuffer();

            StringBuffer temp2 = new StringBuffer();

            for(int i = 0; i < dimension; i++) {

                temp1 = temp1.append(puzzle[i][i]);

                temp2 = temp2.append(puzzle[i][dimension - i - 1]);

            }

            possibleWords.add(new String(temp1));

            possibleWords.add(new String(temp2));

            /* Adding non-principle diagonal word holders */

            for(int i = 1; i < dimension - minimumWordLength; i++) {

                temp1 = new StringBuffer();

                temp2 = new StringBuffer();

                StringBuffer temp3 = new StringBuffer();

                StringBuffer temp4 = new StringBuffer();

                for(int j = i, k = 0; j < dimension && k < dimension; j++, k++) {

                    temp1 = temp1.append(puzzle[j][k]);

                    temp2 = temp2.append(puzzle[k][j]);

                    temp3 = temp3.append(puzzle[dimension - j - 1][k]);

                    temp4 = temp4.append(puzzle[dimension - k - 1][j]);

                }

                possibleWords.add(new String(temp1));

                possibleWords.add(new String(temp2));

                possibleWords.add(new String(temp3));

                possibleWords.add(new String(temp4));

            }

        }

        return possibleWords;

    }

    public static void main(String args[]) {

        WordPuzzle program = new WordPuzzle();

        char[][] puzzle = {

                            {‘p\',’x’,’a’,’b’,’a’,’c’,’k’,’e’,’g’,’s’},

                            {‘e’,’s’,’o’,’r’,’e’,’l’,’a’,’t’,’e’,’d’},

                            {‘v’,’m’,’o’,’o’,’n’,’k’,’c’,’e’,’n’,’t’},

                            {‘z’,’r’,’l’,’l’,’a’,’b’,’l’,’e’,’a’,’d’},

                            {‘m’,’s’,’r’,’k’,’r’,’a’,’d’,’m’,’c’,’f’},

                            {‘f’,’l’,’o’,’o’,’r’,’t’,’j’,’u’,’k’,’v’},

                            {‘d’,’a’,’t’,’a’,’s’,’e’,’t’,’l’,’a’,’s’ }

                          };

        Set<String> words = new HashSet<String>();

        words.add(\"packages\");

        words.add(\"sorted\");

        words.add(\"mooncent\");

        words.add(\"blead\");

        words.add(\"floor\");

        words.add(\"setdata\");

        Set<String> wordsFound = program.findWords(puzzle, words);

        for(String word : wordsFound) {

            System.out.println(word);

        }

    }

}

So, I need to write a program that will find all of the words in a crossword, including reversed and both horizontal or vertical. (no diagonal words). I have to
So, I need to write a program that will find all of the words in a crossword, including reversed and both horizontal or vertical. (no diagonal words). I have to
So, I need to write a program that will find all of the words in a crossword, including reversed and both horizontal or vertical. (no diagonal words). I have to
So, I need to write a program that will find all of the words in a crossword, including reversed and both horizontal or vertical. (no diagonal words). I have to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site