boardGame
Class Board

java.lang.Object
  extended by java.util.Observable
      extended by boardGame.Board
All Implemented Interfaces:
java.util.Observer

public class Board
extends java.util.Observable
implements java.util.Observer

A generic board that, together with Piece, can be used to display board games. This class maintains information about board positions, but does not include any game-specific logic.

Note: The diamond operator, <>, is not used in this class in order to make the class usable to pre-Java 7 programs.

Version:
March 9, 2012
Author:
David Matuszek

Nested Class Summary
static class Board.DragEvent
          Allows drag events to be observed.
 
Field Summary
 Board.DragEvent dragEvent
          To be notified when a user has used the mouse to drag a piece on this board, a program can add an Observer to this field, and override its public void update(Observable o, Object arg) method.
 
Constructor Summary
Board(int rows, int columns)
          Creates a playing board with the given number of rows and columns.
 
Method Summary
 void clear()
          Removes all pieces from this board.
 int columnToX(int columnNumber)
          Returns the X coordinate of the left side of cells in the given column of this board.
 void dump()
          Displays the board contents (for debugging).
 int getColumns()
          Returns the number of columns in this board.
 javax.swing.JPanel getJPanel()
          Returns the JPanel on which this board is displayed.
 Piece getPiece(int row, int column)
          Returns the topmost piece at the given row and column in this board, or null if the given location is empty.
 java.util.Stack<Piece> getPieces(int row, int column)
          Returns a (possibly empty) Stack of all the pieces in the given position.
 int getRows()
          Returns the number of rows in this board.
 int getSelectedColumn()
          Returns the column number (counting from zero) of the currently selected square, or -1 if none is selected.
 Piece getSelectedPiece()
          Returns the currently selected piece, or null if no piece has been selected on this board.
 int getSelectedRow()
          Returns the row number (counting from zero) of the currently selected square, or -1 if none is selected.
 int getSpeed()
          Returns the default speed (in squares per second) of pieces on this board.
 boolean isEmpty(int row, int column)
          Returns true if the given row and column on this board contains no pieces.
 boolean isLegalPosition(int row, int column)
          Determines whether the given row and column denote a legal position on this board.
 boolean isSelectable(int row, int column)
          Returns true to indicate that this square of the board can be selected; override this method to provide different behavior.
 void paint(java.awt.Graphics g)
          Paints this board, not including any pieces that may be on it.
 void place(Piece piece, int row, int column)
          Places the given piece at the given location in this board.
 Piece remove(int row, int column)
          Removes the top piece at the given row and column on this board.
 boolean remove(Piece piece)
          Removes this piece from the board.
 int rowToY(int rowNumber)
          Returns the Y coordinate of the top side of cells in the given column of this board.
 void setSelectedPiece(Piece selectedPiece)
          Mark the given piece as the selected one; unmark any previously selected piece.
 void setSelectedSquare(int row, int column)
          If the given square is selectable, select it, otherwise do nothing.
 void setSpeed(int speed)
          Sets the default speed of movement for pieces on this board, in squares per second.
 void unselectPiece()
          Causes no piece on this board to be selected.
 void unselectSquare()
          Unselects the currently selected square, if any.
 void update(java.util.Observable changedPiece, java.lang.Object rectangle)
          Redraws this board whenever a piece is modified.
 int xToColumn(int x)
          Given an x coordinate, determines which column it is in.
 int yToRow(int y)
          Given a y coordinate, determines which row it is in.
 
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, toString, wait, wait, wait
 

Field Detail

dragEvent

public final Board.DragEvent dragEvent
To be notified when a user has used the mouse to drag a piece on this board, a program can add an Observer to this field, and override its public void update(Observable o, Object arg) method. When the method is invoked, o will hold the Board.DragEvent object, and arg will hold the piece that was dragged.

Constructor Detail

Board

public Board(int rows,
             int columns)
Creates a playing board with the given number of rows and columns. This board contains a Swing JPanel that may be used in a GUI.

Parameters:
rows - Desired number of rows.
columns - Desired number of columns.
Method Detail

getJPanel

public javax.swing.JPanel getJPanel()
Returns the JPanel on which this board is displayed.

Returns:
The JPanel on which this board is displayed.

getRows

public int getRows()
Returns the number of rows in this board.

Returns:
The number of rows.

getColumns

public int getColumns()
Returns the number of columns in this board.

Returns:
The number of columns.

getPiece

public Piece getPiece(int row,
                      int column)
Returns the topmost piece at the given row and column in this board, or null if the given location is empty.

Parameters:
row - The row number.
column - The column number.
Returns:
The piece in the given [row][column], or null if that location is empty. If the board location contains more than one piece, the "topmost" piece is returned.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.

getPieces

public java.util.Stack<Piece> getPieces(int row,
                                        int column)
Returns a (possibly empty) Stack of all the pieces in the given position. The top element of the stack is the topmost element in that board location.

Parameters:
row - A row number on this board.
column - A column number on this board.
Returns:
The pieces in this board location.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.

isEmpty

public boolean isEmpty(int row,
                       int column)
Returns true if the given row and column on this board contains no pieces.

Parameters:
row - The row to examine.
column - The column to examine.
Returns:
true if this location is empty.

xToColumn

public int xToColumn(int x)
Given an x coordinate, determines which column it is in.

Parameters:
x - A local x coordinate.
Returns:
The number of the column containing the given x coordinate.

yToRow

public int yToRow(int y)
Given a y coordinate, determines which row it is in.

Parameters:
y - A local y coordinate.
Returns:
The number of the row containing the given y coordinate.

columnToX

public int columnToX(int columnNumber)
Returns the X coordinate of the left side of cells in the given column of this board.

Parameters:
columnNumber - A column number.
Returns:
The X coordinate of the left side of that column.

rowToY

public int rowToY(int rowNumber)
Returns the Y coordinate of the top side of cells in the given column of this board.

Parameters:
rowNumber - A row number.
Returns:
The Y coordinate of the top side of that row.

place

public void place(Piece piece,
                  int row,
                  int column)
Places the given piece at the given location in this board. It is possible to place more than one piece in a given board location, in which case later pieces go "on top of" earlier pieces.

Parameters:
piece - The piece to be placed on this board.
row - The row in which to place the piece.
column - The column in which to place the piece.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.

clear

public void clear()
Removes all pieces from this board.


remove

public Piece remove(int row,
                    int column)
Removes the top piece at the given row and column on this board.

Parameters:
row - The row of the piece to be removed.
column - The column of the piece to be removed.
Returns:
The top piece at the given location, or null if the given location is empty.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.

remove

public boolean remove(Piece piece)
Removes this piece from the board. Does nothing if the piece is not, in fact, on the board.

Parameters:
piece - The piece to remove.
Returns:
true if anything has been changed.

setSpeed

public void setSpeed(int speed)
Sets the default speed of movement for pieces on this board, in squares per second. This value is used only for pieces that do not specify their own speed.

Parameters:
speed - The default speed for pieces on this board.

getSpeed

public int getSpeed()
Returns the default speed (in squares per second) of pieces on this board.

Returns:
The default speed for pieces on this board.

isLegalPosition

public boolean isLegalPosition(int row,
                               int column)
Determines whether the given row and column denote a legal position on this board.

Parameters:
row - The given row number.
column - The given column number.
Returns:
true if the given row and column number represent a valid location on this board

update

public final void update(java.util.Observable changedPiece,
                         java.lang.Object rectangle)
Redraws this board whenever a piece is modified.

Specified by:
update in interface java.util.Observer
Parameters:
changedPiece - The piece that needs to be redrawn.
rectangle - The area in which to redraw the piece.

paint

public void paint(java.awt.Graphics g)
Paints this board, not including any pieces that may be on it. (Pieces are requested to paint themselves by the paint method that is defined in the DisplayPanel inner class.)

Parameters:
g - The Graphics context on which this board is painted.

dump

public void dump()
Displays the board contents (for debugging).


setSelectedPiece

public void setSelectedPiece(Piece selectedPiece)
Mark the given piece as the selected one; unmark any previously selected piece.

Parameters:
selectedPiece - The piece to be "selected."

getSelectedPiece

public Piece getSelectedPiece()
Returns the currently selected piece, or null if no piece has been selected on this board.

Returns:
The currently selected piece.

unselectPiece

public void unselectPiece()
Causes no piece on this board to be selected.


isSelectable

public boolean isSelectable(int row,
                            int column)
Returns true to indicate that this square of the board can be selected; override this method to provide different behavior.

Parameters:
row - The row index of the square to consider.
column - The column index of the square to consider.
Returns:
true.

getSelectedRow

public int getSelectedRow()
Returns the row number (counting from zero) of the currently selected square, or -1 if none is selected.

Returns:
The selected row number.

getSelectedColumn

public int getSelectedColumn()
Returns the column number (counting from zero) of the currently selected square, or -1 if none is selected.

Returns:
The selected column number.

setSelectedSquare

public void setSelectedSquare(int row,
                              int column)
If the given square is selectable, select it, otherwise do nothing.

Parameters:
row - The row number of the square being selected.
column - The column number of the square being selected.

unselectSquare

public void unselectSquare()
Unselects the currently selected square, if any.