game
Class Board

java.lang.Object
  extended bygame.Board

public class Board
extends Object

Board represents the state of a checkerboard. A checkerboard is a square array of squares. Although checkerboards are normally 8x8, Board can represent any size checkerboard.

Each square of the checkboard may contain at most one Piece, or it may be empty. Each square is identified by its row and column, numbered from 0 to size-1.

Boards are mutable: pieces can be added, removed, and moved. The board informs a set of listeners about these mutations. The size of a board is immutable, however.


Constructor Summary
Board(int n)
          Make an empty n x n checkerboard.
 
Method Summary
 void add(Piece piece, int row, int col)
          Add a new piece to this board.
 void addBoardListener(BoardListener listener)
          Add a listener to this board.
 void clear()
          Remove all pieces from board.
 PieceLocation find(Piece piece)
          Get a piece's location on this board.
 Piece getPieceAt(int row, int col)
          Get the piece found at a given row and column.
 int getSize()
          Get the size of the checkerboard.
 boolean isEmpty(int row, int col)
          Test if a square contains a piece.
 void move(Piece piece, int toRow, int toCol)
          Move a piece from its current square on this board to a new square.
 Iterator pieceLocations()
          Get all pieces on this board, with their locations.
 Iterator pieces()
          Get all pieces on this board.
 void remove(Piece piece)
          Remove a piece from this board.
 void removeBoardListener(BoardListener listener)
          Remove a listener from this board.
 String toString()
          Converts board to a string representation.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Board

public Board(int n)
Make an empty n x n checkerboard.

Parameters:
n - number of squares on each side of the checkerboard
Requires:
n >= 0
Method Detail

getSize

public int getSize()
Get the size of the checkerboard.

Returns:
number of squares on each side of the checkerboard

getPieceAt

public Piece getPieceAt(int row,
                        int col)
Get the piece found at a given row and column.

Parameters:
row - row number in range [0,size-1]
col - column number in range [0,size-1]
Returns:
piece found on the square at [row,column], or null if the square is empty

isEmpty

public boolean isEmpty(int row,
                       int col)
Test if a square contains a piece.

Parameters:
row - row number in range [0,size-1]
col - column number in range [0,size-1]
Returns:
true if and only if square at [row,column] is empty

find

public PieceLocation find(Piece piece)
Get a piece's location on this board.

Parameters:
piece - Piece to find
Returns:
location of piece (row and column) if it's found on this board, or null if not found.

pieces

public Iterator pieces()
Get all pieces on this board.

Returns:
iterator of Pieces on this board, in no particular order.
Requires:
board is not mutated while iterator is being used

pieceLocations

public Iterator pieceLocations()
Get all pieces on this board, with their locations.

Returns:
iterator of PieceLocations, one for each Piece on this board, in no particular order.
Requires:
board is not mutated while iterator is being used

add

public void add(Piece piece,
                int row,
                int col)
Add a new piece to this board.

Parameters:
piece - Piece to add, not already found on this board
row - row number in range [0,size-1]
col - column number in range [0,size-1]
Requires:
square at [row,col] is empty
Modifies:
this board
Effects:
Places piece in square [row,col].

move

public void move(Piece piece,
                 int toRow,
                 int toCol)
Move a piece from its current square on this board to a new square.

Parameters:
piece - Piece to move, which must already be found on this board.
toRow - row number in range [0,size-1]
toCol - column number in range [0,size-1]
Requires:
square at [toRow,toCol] is empty
Modifies:
this board
Effects:
Moves piece to square [toRow,toCol].

remove

public void remove(Piece piece)
Remove a piece from this board.

Parameters:
piece - Piece to remove, which must be found on this board
Modifies:
this board
Effects:
Piece is removed from the board.

clear

public void clear()
Remove all pieces from board.

Modifies:
this board
Effects:
Every square on board becomes empty.

addBoardListener

public void addBoardListener(BoardListener listener)
Add a listener to this board.

Parameters:
listener - Listener to add
Modifies:
this board's set of listeners
Effects:
Listener is notified of future changes to this board.

removeBoardListener

public void removeBoardListener(BoardListener listener)
Remove a listener from this board.

Parameters:
listener - Listener to remove
Modifies:
this board's set of listeners
Effects:
Listener is not notified of future changes to this board.

toString

public String toString()
Converts board to a string representation.

Returns:
a string representing the board as a multiline matrix. Spaces represent empty white squares, and periods represent empty black squares. For example, a blank 8x8 board looks like this:
  . . . .
 . . . .  
  . . . .
 . . . . 
 
Each piece on the board is represented by its toString representation.