Algorithm
Development Kit 1.0

algs.model.problems.tictactoe.model
Class TicTacToeState

java.lang.Object
  extended by algs.model.problems.tictactoe.model.TicTacToeState
All Implemented Interfaces:
IGraphEntity, IGameState

public class TicTacToeState
extends java.lang.Object
implements IGameState, IGraphEntity

The TicTacToe state is determined by a board and the specific logic being used for that board state.

To simplify debugging and dot output, this class supports the IGraphEntity interface.

Since:
1.0
Version:
1.0, 6/15/08
Author:
George Heineman

Nested Class Summary
 
Nested classes/interfaces inherited from interface algs.debug.IGraphEntity
IGraphEntity.Formatter
 
Constructor Summary
TicTacToeState(TicTacToeBoard board, Logic logic)
          The game state is dependent upon a tic-tac-toe board, together with the logic being used to govern the game.
 
Method Summary
 TicTacToeBoard board()
          Expose board state.
 TicTacToeState copy()
          Copy full state information.
 int counter()
          Debugging interface for retrieving counter.
 boolean equivalent(IGameState gameState)
          Determine whether the state is the same by comparing the board state under eight different rotations and reflections.
 void incrementCounter()
          Debugging interface for incrementing count of games.
 boolean isDraw()
          Determine if this game state is a draw.
 boolean isWin()
          Determine if this game state has a winner.
 Logic logic()
          External state may be found in logic.
 java.lang.String nodeLabel()
          Note that gameState changes constantly, so we can do nothing more than grab information and cache it here.
 void reset(TicTacToeBoard newBoard)
          Resets to new game, with new board state and X once again starting.
 java.lang.Object storedData()
          Return external (optional) state information that may have been stored with this state.
 java.lang.Object storedData(java.lang.Object o)
          Store external (optional) state information with this TicTacToe state.
 java.lang.String toString()
          Expose Board state as a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TicTacToeState

public TicTacToeState(TicTacToeBoard board,
                      Logic logic)
The game state is dependent upon a tic-tac-toe board, together with the logic being used to govern the game.

Because tic-tac-toe variations may need to store additional state information with each state, the decision is made to allow the logic to store this additional state. We must ensure that copy() properly deals with logic, then.

Parameters:
board - Board state to use
logic - Logic governing the game. May have additional state information.
Method Detail

copy

public TicTacToeState copy()
Copy full state information. This includes debugging information.

Specified by:
copy in interface IGameState

board

public TicTacToeBoard board()
Expose board state. Not part of the IGameState interface.


logic

public Logic logic()
External state may be found in logic. Not part of the IGameState interface.


toString

public java.lang.String toString()
Expose Board state as a string.

Overrides:
toString in class java.lang.Object
Returns:
board state using its toString method.

isWin

public boolean isWin()
Description copied from interface: IGameState
Determine if this game state has a winner.

Specified by:
isWin in interface IGameState

isDraw

public boolean isDraw()
Description copied from interface: IGameState
Determine if this game state is a draw.

Specified by:
isDraw in interface IGameState

reset

public void reset(TicTacToeBoard newBoard)
Resets to new game, with new board state and X once again starting. Note that the storedData with this state is also updated.


equivalent

public boolean equivalent(IGameState gameState)
Determine whether the state is the same by comparing the board state under eight different rotations and reflections.

Specified by:
equivalent in interface IGameState
Parameters:
gameState - The game state being compared against.

storedData

public java.lang.Object storedData(java.lang.Object o)
Store external (optional) state information with this TicTacToe state.

Parameters:
o - object to be stored

storedData

public java.lang.Object storedData()
Return external (optional) state information that may have been stored with this state.


incrementCounter

public void incrementCounter()
Description copied from interface: IGameState
Debugging interface for incrementing count of games.

Specified by:
incrementCounter in interface IGameState

counter

public int counter()
Description copied from interface: IGameState
Debugging interface for retrieving counter.

Specified by:
counter in interface IGameState

nodeLabel

public java.lang.String nodeLabel()
Note that gameState changes constantly, so we can do nothing more than grab information and cache it here. For this purpose, we store information with each gameState. If that info

Specified by:
nodeLabel in interface IGraphEntity

Algorithm Development Kit 1.0

This code supports the Algorithms in a Nutshell book, published by O'Reilly Media, Inc. in November 2008. Please visit the book web page to learn of any changes to the code repository or to record a potential defect.