edu.lhup.ai.tictactoe
Class Board

java.lang.Object
  |
  +--edu.lhup.ai.tictactoe.Board
All Implemented Interfaces:
IBoard

public class Board
extends Object
implements IBoard

A concrete board implementation that implements the rules of tic-tac-toe.

This software is for educational purposes only.

Author:
Mark Cohen

Field Summary
(package private) static int OTURN
           
(package private) static int OWINS
           
(package private) static int TIE
           
(package private) static int XTURN
           
(package private) static int XWINS
           
 
Constructor Summary
Board()
          Creates an empty Board object w/o players
Board(Board other)
          Creates a new Board object based on an existing Board object.
Board(IMove[] moves, IPlayer[] players)
          Creates a new Board object from an array of moves.
Board(IPlayer[] players)
          Creates an empty Board object.
 
Method Summary
(package private) static StringBuffer createStringBuffer()
           
(package private) static IPiece emptyPiece()
           
 boolean equals(Object other)
           
 IPiece[][] getBoard()
           
 String getDescription()
           
(package private) static Move getMove(IPiece piece, int row, int col)
           
 IPlayer[] getPlayers()
           
 String getShortDescription()
           
 int getState()
           
 IPlayer getWinner()
           
 int hashCode()
           
 Iterator moveIterator()
           
 void moves(Collection col)
          Populates the specified Collection with the set of all currently legal moves.
(package private) static IPiece oPiece()
           
 IMove peekMove()
           
 Iterator playerIterator()
           
 IMove popMove()
          Removes the last move from this board's stack.
 void pushMove(IMove move)
          Adds the specified move to this board's stack.
 void pushMove(String strMove)
          Adds the specified move to this board's stack.
 void resetState()
          Returns this board to its initial state so it will be ready for a new game.
 void setPlayers(IPlayer[] players)
          Specifies the players that will be playing this game.
 String toString()
           
(package private) static IPiece xPiece()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

XWINS

static final int XWINS
See Also:
Constant Field Values

OWINS

static final int OWINS
See Also:
Constant Field Values

TIE

static final int TIE
See Also:
Constant Field Values

XTURN

static final int XTURN
See Also:
Constant Field Values

OTURN

static final int OTURN
See Also:
Constant Field Values
Constructor Detail

Board

public Board()
Creates an empty Board object w/o players


Board

public Board(IPlayer[] players)
      throws StateException
Creates an empty Board object.


Board

public Board(IMove[] moves,
             IPlayer[] players)
      throws StateException
Creates a new Board object from an array of moves.


Board

public Board(Board other)
      throws StateException
Creates a new Board object based on an existing Board object. The new object is an exact copy of the existing object.

Method Detail

setPlayers

public void setPlayers(IPlayer[] players)
                throws StateException
Description copied from interface: IBoard
Specifies the players that will be playing this game.

Specified by:
setPlayers in interface IBoard
Parameters:
players - an array of players that will be playing the game.
Throws:
StateException - if the specified number of type of players cannot play this game.

getPlayers

public IPlayer[] getPlayers()
Specified by:
getPlayers in interface IBoard
Returns:
the players currently playing this game.

playerIterator

public Iterator playerIterator()
Specified by:
playerIterator in interface IBoard
Returns:
an Iterator of the players currently playing this game. The players will be iterated in the order in which they are allowed to take their turns. The iterator will be empty if this game has ended.

getWinner

public IPlayer getWinner()
Specified by:
getWinner in interface IBoard
Returns:
the player that has won this game, or null if there is currently no winner, or if this game has ended in a tie.

peekMove

public IMove peekMove()
Specified by:
peekMove in interface IBoard
Returns:
a reference to the last move that was added to this board's stack,

pushMove

public void pushMove(String strMove)
              throws StateException
Adds the specified move to this board's stack. The move is specified using a string representation of the move. For example the move "2,1" would place a piece at row 2, column 1. The piece placed on the board will be the piece that is assocated with the player whos turn is next. For example, if it is the "X player's" turn then an X piece will be placed on the board.

The first move must be made by the "X player". Moves must alternate between the "X player" and the "O player". Finally, moves can only be made on an empty space.

Specified by:
pushMove in interface IBoard
Parameters:
strMove - the move that will be added to the stack.
Throws:
StateException - if the specified move is illegal.

pushMove

public void pushMove(IMove move)
              throws StateException
Adds the specified move to this board's stack. The first move must be made by the "X player". Moves must alternate between the "X player" and the "O player". Finally, moves can only be made on an empty space.

Specified by:
pushMove in interface IBoard
Throws:
StateException - if the specified move is illegal.

popMove

public IMove popMove()
Description copied from interface: IBoard
Removes the last move from this board's stack.

Specified by:
popMove in interface IBoard
Returns:
the move that was removed from the stack.

getBoard

public IPiece[][] getBoard()
Specified by:
getBoard in interface IBoard
Returns:
an array of pieces currently on this board.

getState

public int getState()
Specified by:
getState in interface IBoard
Returns:
a integer representation of this games current state.

resetState

public void resetState()
Description copied from interface: IBoard
Returns this board to its initial state so it will be ready for a new game.

Specified by:
resetState in interface IBoard

moveIterator

public Iterator moveIterator()
Specified by:
moveIterator in interface IBoard
Returns:
an Iterator of the set of all currently legal moves.

moves

public void moves(Collection col)
Description copied from interface: IBoard
Populates the specified Collection with the set of all currently legal moves.

Specified by:
moves in interface IBoard
Parameters:
col - the Collection that will be populated with the set of all currently legal moves.

getDescription

public String getDescription()
Specified by:
getDescription in interface IBoard
Returns:
a description of this board.

getShortDescription

public String getShortDescription()
Specified by:
getShortDescription in interface IBoard
Returns:
a short description of this player.

toString

public String toString()
Overrides:
toString in class Object

equals

public boolean equals(Object other)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

emptyPiece

static IPiece emptyPiece()

xPiece

static IPiece xPiece()

oPiece

static IPiece oPiece()

createStringBuffer

static StringBuffer createStringBuffer()

getMove

static Move getMove(IPiece piece,
                    int row,
                    int col)