HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

"Word-Target" Puzzle Solver in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
targetjavawordsolverpuzzle

Problem

Have recently been writing a few puzzle solvers in Java, this is my latest attempt. It solves the "Word-Target" puzzle found in some newspapers. You may have seen it before:

Words must be found by using any of the letters in the grid in any order, however each word must contain the centre letter, and must be at least 4 characters long.

The program takes in two text files, one dictionary text file (must be in alphabetical order already) and one puzzle text file:

puzzle.txt:

R O Y
S V N
E U L


I use the dictionary located here: dict2.txt

Program output:

~~~$ java WordTarget dict2.txt puzzle.txt
WORD GRID:
r o y
s V n
e u l

Found 38 results with 4 letters or more
Found 9 results with 4 letters:
- envy
- levy
- love
- oven
- over
- revs
- rove
- very
- vole
Found 12 results with 5 letters:
- envoy
- lover
- loves
- nervy
- novel
- ovens
- overs
- ovule
- roves
- servo
- solve
- voles
Found 12 results with 6 letters:
- envoys
- louver
- lovers
- novels
- overly
- ovules
- sloven
- solver
- survey
- velour
- venous
- voyeur
Found 4 results with 7 letters:
- louvers
- nervous
- velours
- voyeurs
Found 0 results with 8 letters:
Found 1 results with 9 letters:
- nervously
Time taken: 524 milliseconds
Memory used: 11 MB


WordTarget.java:

`/**
* Class Name: WordTarget
*
* @author: Thomas McKeesick
* Creation Date: Monday, February 16 2015, 02:25
* Last Modified: Tuesday, March 03 2015, 11:25
*
* Class Description: A Java class that solves the 9 letter "Word-Target"
* puzzle.
*
* @version 0.2.0
*/

import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Collections;

import java.io.FileReader;
import java.io.BufferedReader;

import java.io.IOException;

public class WordTarget {

private static int MIN_LENGTH = 4;
private static List dict;

public static void main(String[] args) {

if( args.length ");

Solution

One thing should do is specify constant, unchanging variables as final:

private static final int MIN_LENGTH = 4;


Now, you will not be allowed to modify the variable.

You spread your statements over multiple lines too often; many of these would look better on one line, including these:

BufferedReader in = new BufferedReader(new FileReader(filename));

tmp = permute(tmp, str, MIN_LENGTH, String.valueOf(centre));


Also, you have a large main() method. You should delegate most of the work to other methods and just use main() to start the program.

It is excellent that you always use braces, even around one-line if statements and loops; this will help you prevent errors.

Code Snippets

private static final int MIN_LENGTH = 4;
BufferedReader in = new BufferedReader(new FileReader(filename));

tmp = permute(tmp, str, MIN_LENGTH, String.valueOf(centre));

Context

StackExchange Code Review Q#83046, answer score: 4

Revisions (0)

No revisions yet.