|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectPuzzle
public class Puzzle
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.
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 |
---|
private int width
private int height
private java.util.ArrayList pieces
private Piece[][] board
private java.util.ArrayList moveList
private boolean solved
Constructor Detail |
---|
public Puzzle(int width, int height) throws PuzzleException
width
- the number of columns in the puzzle, must be >0height
- the number of rows in the puzzle, must be >0
PuzzleException
- if the dimensions are invalidprivate Puzzle(int width, int height, java.util.ArrayList pieces, Piece[][] board, java.util.ArrayList moveList)
width
- the number of columns in the puzzleheight
- the number of rows in the puzzlepieces
- the ArrayList of piecesboard
- the array of references representing the puzzle boardMethod Detail |
---|
private static java.lang.String pieceName(int num) throws java.lang.IllegalArgumentException
num
- the number of the piece, must be in the range -1java.lang.IllegalArgumentException
- if num is out of rangepublic void add(Piece piece) throws PuzzleException
piece
- the piece to be added
PuzzleException
- if the piece cannot fit in the puzzle, or there
are too many piecespublic boolean isEmpty(int x, int y, Piece piece)
x
- the column of the location to checky
- the row of the location to checkpiece
- piece that may already occupy the location
public void occupyLocation(int x, int y, Piece piece)
x
- the column of the locationy
- the row of the locationpiece
- the piece to occupy the location
java.lang.IllegalArgumentException
- if the location is not on the board
java.lang.RuntimeException
- if the location is already occupiedpublic int getNumPieces()
public java.util.List getMoveList()
public boolean isSolved()
public static java.lang.String dupString(java.lang.String toDup, int n)
toDup
- the string to duplicaten
- how many times to duplicate it
public java.lang.String prettyString()
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toHtmlString(java.lang.String caption)
caption
- a text title for the table, must not be null
public java.util.Collection getSingleMovePuzzles()
private Puzzle clone(PieceMove move)
move
- the move that resulted in this puzzle
private static void testPieceName(int num, java.lang.String expectedResult) throws java.lang.IllegalArgumentException
num
- the number to testexpectedResult
- the expected result
java.lang.IllegalArgumentException
- if the result is not as expectedprivate static void testPieceNameException(int num) throws java.lang.IllegalArgumentException
num
- the number to test
java.lang.IllegalArgumentException
- if pieceName() does not generate an
IllegalArgumentException exceptionprivate static void testPieceName() throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- to indicate the results of the tests
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |