Class Piece

java.lang.Object
  extended by Piece

public class Piece
extends java.lang.Object

Piece represents a rectangular piece in the puzzle. Pieces can move horizontally, vertically, or both, but movement occurrs in only one direction at a time. Piece position and dimensions are immutable. Movement is acheived by generating a list of new pieces in the various possible positions resulting from the "movement" of this piece.

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
static char H_MOVE
           
private  int height
           
private  char movement
           
private  java.lang.String name
           
static char V_MOVE
           
private  int width
           
private  int x
           
private  int y
           
 
Constructor Summary
Piece(int x, int y, int width, int height, char movement)
          Constructor: initializes a piece that is not yet part of any puzzle
Piece(int x, int y, int width, int height, char movement, java.lang.String name)
          Private Constructor: used for cloning, does no error checking
 
Method Summary
 void addedToPuzzle(Puzzle puzzle)
          Informs the piece that it has been added to the specified puzzle.
private  boolean addMove(Puzzle puzzle, java.util.Collection moves, char axis, int delta)
          Checks if the piece fits after moving delta spaces along the specified axis.
 void checkFit(Puzzle puzzle)
          Checks if the piece can fit into the puzzle without overlapping any other piece or hanging off the edge of the board.
 int getHeight()
          Accessor for height
 char getMovement()
          Accessor for axes of movement
 java.lang.String getName()
          Accessor for name
 java.util.Collection getValidMoves(Puzzle puzzle)
          Finds a list of all valid moves that the piece can make.
 int getWidth()
          Accessor for width
 int getX()
          Accessor for x
 int getY()
          Accessor for y
 void setName(java.lang.String newName)
          Mutator for name
 java.lang.String toString()
          Generates a string representation of the piece
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

private java.lang.String name

x

private int x

y

private int y

width

private int width

height

private int height

movement

private char movement

H_MOVE

public static final char H_MOVE
See Also:
Constant Field Values

V_MOVE

public static final char V_MOVE
See Also:
Constant Field Values
Constructor Detail

Piece

public Piece(int x,
             int y,
             int width,
             int height,
             char movement)
      throws PuzzleException
Constructor: initializes a piece that is not yet part of any puzzle

Parameters:
x - the column where the piece lives
y - the row where the piece lives
width - the width of the piece (in columns)
height - the height of the piece (in rows)
movement - bit mask of piece's movement ability: V_MOVE, H_MOVE
Throws:
PuzzleException - if any of the parameters are invalid

Piece

public Piece(int x,
             int y,
             int width,
             int height,
             char movement,
             java.lang.String name)
Private Constructor: used for cloning, does no error checking

Parameters:
x - the column where the piece lives
y - the row where the piece lives
width - the width of the piece (in columns)
height - the height of the piece (in rows)
movement - bit mask of piece's movement ability: V_MOVE, H_MOVE
Method Detail

checkFit

public void checkFit(Puzzle puzzle)
              throws PuzzleException
Checks if the piece can fit into the puzzle without overlapping any other piece or hanging off the edge of the board.

Parameters:
puzzle - the puzzle to check if the piece will fit
Throws:
PuzzleException - if the piece cannot fit into the puzzle

addedToPuzzle

public void addedToPuzzle(Puzzle puzzle)
Informs the piece that it has been added to the specified puzzle. This method registers the locations that the piece occupies in the puzzle board. No error checking is performed so one must call checkFit() before calling this method.

Parameters:
puzzle - the puzzle to which the piece has been added

toString

public java.lang.String toString()
Generates a string representation of the piece

Overrides:
toString in class java.lang.Object
Returns:
human readable string with information about the piece

setName

public void setName(java.lang.String newName)
Mutator for name

Parameters:
newName - the new name of the piece

getName

public java.lang.String getName()
Accessor for name

Returns:
the name of the piece

getX

public int getX()
Accessor for x

Returns:
Returns the column where the piece lives

getY

public int getY()
Accessor for y

Returns:
Returns the row where the piece lives

getWidth

public int getWidth()
Accessor for width

Returns:
Returns the width of the piece (number of columns spanned)

getHeight

public int getHeight()
Accessor for height

Returns:
Returns the height of the piece (number of rows spanned)

getMovement

public char getMovement()
Accessor for axes of movement

Returns:
axes in which this piece can move: V_MOVE, H_MOVE, or both

getValidMoves

public java.util.Collection getValidMoves(Puzzle puzzle)
Finds a list of all valid moves that the piece can make.

Parameters:
puzzle - the puzzle where the piece lives
Returns:
the list of valid moves

addMove

private boolean addMove(Puzzle puzzle,
                        java.util.Collection moves,
                        char axis,
                        int delta)
Checks if the piece fits after moving delta spaces along the specified axis. A copy of the piece in the destination position is then created. A PieceMove object is created and added to the provided list.

Parameters:
puzzle - the puzzle where the piece lives
moves - the list of moves (new move gets added to this)
axis - the axis of movement: H_MOVE or V_MOVE
delta - the distance and direction of travel along the axis
Returns:
true if the move is valid, otherwise false
Throws:
java.lang.IllegalArgumentException - if moves is null, delta is zero, or axis is not equal to H_MOVE and not equal to V_MOVE