boardGame
Class Piece

java.lang.Object
  extended by java.util.Observable
      extended by boardGame.Piece
Direct Known Subclasses:
RoundPiece

public abstract class Piece
extends java.util.Observable

An abstract class that represents a playing piece. This class, together with Board, can be used to display board games. This class maintains information about board positions, but does not include any game-specific logic.

Version:
March 9, 2012
Author:
David Matuszek

Constructor Summary
Piece()
          Creates a piece.
Piece(java.lang.String name)
          Creates a piece with the given name.
 
Method Summary
 boolean canMove(int deltaRow, int deltaColumn)
          Determines whether this piece can be moved to the specified location.
 boolean canMoveTo(int newRow, int newColumn)
          Determines whether this piece can be moved to the specified location.
 void dump()
          Debugging method to print out the status of this piece.
 Board getBoard()
          Returns the board on which this piece has been placed.
 int getColumn()
          Returns the column that this piece is in, or -1 if this piece is not currently on some board.
 java.lang.String getName()
          Returns the name of this piece; possibly useful for debugging.
 int getRow()
          Returns the row that this piece is in, or -1 if this piece is not currently on some board.
 int getSpeed()
          Get the speed (in squares/second) at which this piece will move.
 boolean isDraggable()
          Returns true if the piece can be dragged by the mouse.
 boolean isOnBoard()
          Tests whether this piece is on some (any) board.
 boolean isOnBoard(Board board)
          Tests whether this piece is on the specified board.
 boolean isSelectable()
          Returns true if the piece can be selected by the mouse.
 boolean move(int deltaRow, int deltaColumn)
          Moves this piece the given number of rows and columns.
 boolean moveTo(int newRow, int newColumn)
          Moves this piece to a new position on the board.
 void moveToTop()
          Ensures that this piece will be drawn on top of any other pieces in the same location on the board.
abstract  void paint(java.awt.Graphics g, java.awt.Rectangle r)
          Paints this piece on the board within the given rectangle; must be implemented by a subclass.
 void place(Board board, int row, int column)
          Places this piece at the given row and column on the board.
 void redraw()
          Causes this piece to be redrawn.
 void redraw(java.awt.Rectangle rect)
          Causes the given rectangle to be redrawn.
 void remove()
          Removes this piece from whatever board it may be on, but does not delete the piece itself.
 void setDraggable(boolean draggable)
          Determines whether the piece can be dragged by the mouse.
 void setSelectable(boolean selectable)
          Sets whether this piece can be selected by the mouse.
 void setSpeed(int speed)
          Sets the speed of movement (in squares/second) for this piece.
 java.lang.String toString()
          Returns a printable String representing this piece.
 
Methods inherited from class java.util.Observable
addObserver, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Piece

public Piece()
Creates a piece. The piece is not placed on a board; for that, see the instance methods board.place(piece, row, column) and piece.place(board, row, column).


Piece

public Piece(java.lang.String name)
Creates a piece with the given name. The piece is not placed on a board; for that, see the instance methods board.place(piece, row, column) and piece.place(board, row, column).

Parameters:
name - The user-defined name of this piece.
Method Detail

getName

public java.lang.String getName()
Returns the name of this piece; possibly useful for debugging.

Returns:
The name of this piece.

getBoard

public Board getBoard()
Returns the board on which this piece has been placed.

Returns:
The board (if any) containing this piece.

toString

public java.lang.String toString()
Returns a printable String representing this piece.

Overrides:
toString in class java.lang.Object
Returns:
A printable representation of this piece.

setDraggable

public void setDraggable(boolean draggable)
Determines whether the piece can be dragged by the mouse.

Parameters:
draggable - Tell whether the piece can be dragged by user mouse movement.

isDraggable

public boolean isDraggable()
Returns true if the piece can be dragged by the mouse.

Returns:
true if the piece is draggable.

setSelectable

public void setSelectable(boolean selectable)
Sets whether this piece can be selected by the mouse.

Parameters:
selectable - A value of true means that the piece can be selected with the mouse.

isSelectable

public boolean isSelectable()
Returns true if the piece can be selected by the mouse.

Returns:
true if the piece is selectable.

getRow

public int getRow()
Returns the row that this piece is in, or -1 if this piece is not currently on some board.

Returns:
The row number.

getColumn

public int getColumn()
Returns the column that this piece is in, or -1 if this piece is not currently on some board.

Returns:
The column number.

place

public void place(Board board,
                  int row,
                  int column)
Places this piece at the given row and column on the board.

Parameters:
board - The board on which to place this piece.
row - The row in which to place this piece.
column - The column in which to place this piece.

remove

public void remove()
Removes this piece from whatever board it may be on, but does not delete the piece itself.


isOnBoard

public boolean isOnBoard()
Tests whether this piece is on some (any) board.

Returns:
true if this piece is currently on a board.

isOnBoard

public boolean isOnBoard(Board board)
Tests whether this piece is on the specified board.

Returns:
true if this piece is currently on the given board.

setSpeed

public void setSpeed(int speed)
Sets the speed of movement (in squares/second) for this piece. Values of 1 to 10 are reasonable; very large values will cause this piece to "teleport" to the new location; a zero or negative value will cause the default speed (set by the board) to be used.

Parameters:
speed - The desired speed of movement, in pixels/redraw.

getSpeed

public int getSpeed()
Get the speed (in squares/second) at which this piece will move.

Returns:
This piece's speed of movement, in pixels/redraw.

paint

public abstract void paint(java.awt.Graphics g,
                           java.awt.Rectangle r)
Paints this piece on the board within the given rectangle; must be implemented by a subclass.

Parameters:
g - The Graphics object on which painting should be done.
r - The rectangle in which to paint this piece.

moveToTop

public void moveToTop()
Ensures that this piece will be drawn on top of any other pieces in the same location on the board.


move

public boolean move(int deltaRow,
                    int deltaColumn)
Moves this piece the given number of rows and columns.

Parameters:
deltaRow - The number of rows down to move this piece; a negative number will move the piece up.
deltaColumn - The number of columns to move this piece to the right; a negative number will move the piece left.
Returns:
False if the move would take the piece outside the boundaries of the board.

moveTo

public boolean moveTo(int newRow,
                      int newColumn)
Moves this piece to a new position on the board.

Parameters:
newRow - The destination row.
newColumn - The destination column.
Returns:
false if the destination is not a legal board position, or if the piece is already moving.

canMove

public final boolean canMove(int deltaRow,
                             int deltaColumn)
Determines whether this piece can be moved to the specified location. The default behavior is to return true if this piece is on some board, and the specified location exists on that board.

This method delegates the decision to the canMoveTo method. To ensure consistency, this method may not be overridden; but overriding canMoveTo with more specific tests will alter the behavior of both methods.

Parameters:
deltaRow - The number of squares to move down.
deltaColumn - The number of squares to move to the right.
Returns:
true if the move can be made.

canMoveTo

public boolean canMoveTo(int newRow,
                         int newColumn)
Determines whether this piece can be moved to the specified location. This method returns true if this piece is on a board and the location is legal for that board. This method can be overridden with more specific tests.

Parameters:
newRow - The desired row.
newColumn - The desired column.
Returns:
true if the move can be made.

redraw

public void redraw()
Causes this piece to be redrawn.


redraw

public void redraw(java.awt.Rectangle rect)
Causes the given rectangle to be redrawn.

Parameters:
rect - The area to be redrawn.

dump

public void dump()
Debugging method to print out the status of this piece.