Class Puzzle

java.lang.Object
  extended by Puzzle

public class Puzzle
extends java.lang.Object

Puzzle is used to represent a single puzzle state. It keeps a list of Pieces contained in the puzzle. It also uses an array of the same dimensions as the puzzle to keep track of the occupancy status of each location on the board. A list of moves indicates how one can transition from the initial puzzle state to this one. The class is also able to generate a list of new puzzle objects that each represent a state that is reachable by moving only one piece on this puzzle. A maximum of 127 pieces is supported.

Version:
2005-10-30, JDK 1.5.0.5, Eclipse 3.1.0, Windows XP, CS 340, Fall 2005, Instructor: Pat Troy, TA: Nitin Jindal
Author:
Michael Leonhard (mleonhar)

Field Summary
private  Piece[][] board
           
private  int height
           
private  java.util.ArrayList moveList
           
private  java.util.ArrayList pieces
           
private  boolean solved
           
private  int width
           
 
Constructor Summary
  Puzzle(int width, int height)
          Constructor: initializes an empty puzzle
private Puzzle(int width, int height, java.util.ArrayList pieces, Piece[][] board, java.util.ArrayList moveList)
          Private Constructor: used for cloning, no error checking is performed
 
Method Summary
 void add(Piece piece)
          Add the piece to the puzzle.
private  Puzzle clone(PieceMove move)
          Creates a clone of the puzzle where the piece has been moved, as specified in the move object.
static java.lang.String dupString(java.lang.String toDup, int n)
          Constructs a string consisting of n copies of toDup.
 java.util.List getMoveList()
          Accessor for move list
 int getNumPieces()
          Accessor for pieces array length
 java.util.Collection getSingleMovePuzzles()
          Searches for all valid puzzles that can be obtained by making a single move on this puzzle.
 boolean isEmpty(int x, int y, Piece piece)
          Checks if the specified location on the puzzle board is empty or occupied by the specified piece
 boolean isSolved()
          Accessor for solved field
 void occupyLocation(int x, int y, Piece piece)
          Sets the piece as the occupant of the specified location
private static java.lang.String pieceName(int num)
          Returns a name for the piece with the specified number
 java.lang.String prettyString()
          Returns a string version of the puzzle suitable for display to the user
private static void testPieceName()
          Tests pieceName(int) with many different inputs, checking that the correct result is returned or an exception is properly thrown.
private static void testPieceName(int num, java.lang.String expectedResult)
          Tests pieceName() with the supplied number
private static void testPieceNameException(int num)
          Tests pieceName() with the supplied number
 java.lang.String toHtmlString(java.lang.String caption)
          Returns an HTML string version of the puzzle.
 java.lang.String toString()
          Returns a string representation of the puzzle.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

width

private int width

height

private int height

pieces

private java.util.ArrayList pieces

board

private Piece[][] board

moveList

private java.util.ArrayList moveList

solved

private boolean solved
Constructor Detail

Puzzle

public Puzzle(int width,
              int height)
       throws PuzzleException
Constructor: initializes an empty puzzle

Parameters:
width - the number of columns in the puzzle, must be >0
height - the number of rows in the puzzle, must be >0
Throws:
PuzzleException - if the dimensions are invalid

Puzzle

private Puzzle(int width,
               int height,
               java.util.ArrayList pieces,
               Piece[][] board,
               java.util.ArrayList moveList)
Private Constructor: used for cloning, no error checking is performed

Parameters:
width - the number of columns in the puzzle
height - the number of rows in the puzzle
pieces - the ArrayList of pieces
board - the array of references representing the puzzle board
Method Detail

pieceName

private static java.lang.String pieceName(int num)
                                   throws java.lang.IllegalArgumentException
Returns a name for the piece with the specified number

Parameters:
num - the number of the piece, must be in the range -1Returns:
a name for the piece
Throws:
java.lang.IllegalArgumentException - if num is out of range

add

public void add(Piece piece)
         throws PuzzleException
Add the piece to the puzzle.

Parameters:
piece - the piece to be added
Throws:
PuzzleException - if the piece cannot fit in the puzzle, or there are too many pieces

isEmpty

public boolean isEmpty(int x,
                       int y,
                       Piece piece)
Checks if the specified location on the puzzle board is empty or occupied by the specified piece

Parameters:
x - the column of the location to check
y - the row of the location to check
piece - piece that may already occupy the location
Returns:
true if the location is on the board and is occupied by the provided piece or no piece, otherwise false

occupyLocation

public void occupyLocation(int x,
                           int y,
                           Piece piece)
Sets the piece as the occupant of the specified location

Parameters:
x - the column of the location
y - the row of the location
piece - the piece to occupy the location
Throws:
java.lang.IllegalArgumentException - if the location is not on the board
java.lang.RuntimeException - if the location is already occupied

getNumPieces

public int getNumPieces()
Accessor for pieces array length

Returns:
number of pieces in the puzzle

getMoveList

public java.util.List getMoveList()
Accessor for move list

Returns:
list of PieceMove objects represening the moves taken from the initial puzzle state to reach this puzzle

isSolved

public boolean isSolved()
Accessor for solved field

Returns:
true if the puzzle is solved, otherwise false

dupString

public static java.lang.String dupString(java.lang.String toDup,
                                         int n)
Constructs a string consisting of n copies of toDup. Example: dupString("*", 3) returns "***".

Parameters:
toDup - the string to duplicate
n - how many times to duplicate it
Returns:
the constructed string

prettyString

public java.lang.String prettyString()
Returns a string version of the puzzle suitable for display to the user

Returns:
a human readable string version of the puzzle

toString

public java.lang.String toString()
Returns a string representation of the puzzle. Two puzzles with identical pieces in the same locations will always have the same representational string.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the puzzle

toHtmlString

public java.lang.String toHtmlString(java.lang.String caption)
Returns an HTML string version of the puzzle. A table is used to lay out the puzzle and its pieces.

Parameters:
caption - a text title for the table, must not be null
Returns:
HTML string representation of the puzzle

getSingleMovePuzzles

public java.util.Collection getSingleMovePuzzles()
Searches for all valid puzzles that can be obtained by making a single move on this puzzle. If a solved puzzle is found, a partial result will be returned, with the solved puzzle at its head.

Returns:
the list of puzzles (may be empty)

clone

private Puzzle clone(PieceMove move)
Creates a clone of the puzzle where the piece has been moved, as specified in the move object.

Parameters:
move - the move that resulted in this puzzle
Returns:
the new Puzzle object

testPieceName

private static void testPieceName(int num,
                                  java.lang.String expectedResult)
                           throws java.lang.IllegalArgumentException
Tests pieceName() with the supplied number

Parameters:
num - the number to test
expectedResult - the expected result
Throws:
java.lang.IllegalArgumentException - if the result is not as expected

testPieceNameException

private static void testPieceNameException(int num)
                                    throws java.lang.IllegalArgumentException
Tests pieceName() with the supplied number

Parameters:
num - the number to test
Throws:
java.lang.IllegalArgumentException - if pieceName() does not generate an IllegalArgumentException exception

testPieceName

private static void testPieceName()
                           throws java.lang.IllegalArgumentException
Tests pieceName(int) with many different inputs, checking that the correct result is returned or an exception is properly thrown.

Throws:
java.lang.IllegalArgumentException - to indicate the results of the tests