Holger's
Java API

com.antelmann.game.ttt
Class TickTackToe

java.lang.Object
  extended by com.antelmann.game.AbstractGame
      extended by com.antelmann.game.ttt.TickTackToe
All Implemented Interfaces:
GamePlay, Serializable, Cloneable

public class TickTackToe
extends AbstractGame

This class implements the game TickTackToe. The board positions are represented as follows:

 0 1 2
 3 4 5
 6 7 8
 

Author:
Holger Antelmann
See Also:
Serialized Form

Constructor Summary
TickTackToe()
           
TickTackToe(String name)
           
 
Method Summary
 Object clone()
          making sure that the internal non-primitive board representation is properly cloned to allow the deep-copy required for spawnChild()
 boolean equals(Object obj)
          required to allow recognizing the same game status even after serialization (to support network games, GameBooks, etc.).
 int[] getWinner()
          getWinner() is checking for a triple (three-in-a-row).
 int hashCode()
          It's just some number derived from a portion of the board; not guaranteed to be unique for every game, but guaranteed to always be the same for the same board.
protected  GameMove[] listLegalMoves()
          listLegalMoves() returns the legal moves for this game.
 int nextPlayer()
          nextPlayer() returns the integer representing the game role of the next player.
protected  boolean popMove()
          popMove undoes the last move by altering the game board to the stage before the last move happened.
protected  boolean pushMove(GameMove move)
          pushMove takes a GameMove and alters the game according to to the move.
 String toString()
          overridden to provide useful information about the game
 
Methods inherited from class com.antelmann.game.AbstractGame
addObserver, clearRedoList, gameOver, getGameName, getLastMove, getLastPlayer, getLegalMoves, getLegalMoves, getMoveHistory, getNumberOfRedoMoves, getObservers, getRedoList, getResult, isLegalMove, isWinner, loadFromFile, makeMove, numberOfMoves, numberOfPlayers, redoMove, removeObserver, resetLegalMoves, resetLists, saveToFile, spawnChild, undoLastMove, undoMoves
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TickTackToe

public TickTackToe()

TickTackToe

public TickTackToe(String name)
Method Detail

nextPlayer

public int nextPlayer()
Description copied from interface: GamePlay
nextPlayer() returns the integer representing the game role of the next player. If there is no next player, it is assumed that there are no more legal moves left in the game, in which case the value of nextPlayer() should be irrelevant.


getWinner

public int[] getWinner()
getWinner() is checking for a triple (three-in-a-row). If found, it returns an array with the player role that has the triple. If the board is full (after 9 moves) and no triple is found, an empty array is returned to represent a draw (i.e. no winner). Otherwise, null is returned (when the game is still in progress).


listLegalMoves

protected GameMove[] listLegalMoves()
Description copied from class: AbstractGame
listLegalMoves() returns the legal moves for this game. This function is used by the final method getLegalMoves(), which is a wrapper around this method to avoid the overhead of generating the legal moves over and over again when called multiple times (as calculating legal moves is commonly expensive). Note that implementations of listLegalMoves() must not check for gameOver(), unless gameOver() is overridden not to check for listLegalMoves(). The same applies to isLegalMove().

Specified by:
listLegalMoves in class AbstractGame

pushMove

protected boolean pushMove(GameMove move)
Description copied from class: AbstractGame
pushMove takes a GameMove and alters the game according to to the move.

Specified by:
pushMove in class AbstractGame
Returns:
true only if the move was put on the board successfully.

popMove

protected boolean popMove()
Description copied from class: AbstractGame
popMove undoes the last move by altering the game board to the stage before the last move happened.

Specified by:
popMove in class AbstractGame
Returns:
true only if the move was taken back successfully.

toString

public String toString()
Description copied from class: AbstractGame
overridden to provide useful information about the game

Overrides:
toString in class AbstractGame

clone

public Object clone()
             throws CloneNotSupportedException
making sure that the internal non-primitive board representation is properly cloned to allow the deep-copy required for spawnChild()

Specified by:
clone in interface GamePlay
Overrides:
clone in class AbstractGame
Throws:
CloneNotSupportedException
See Also:
AbstractGame.spawnChild(GameMove)

hashCode

public int hashCode()
It's just some number derived from a portion of the board; not guaranteed to be unique for every game, but guaranteed to always be the same for the same board. (trading off performance vs. effectiveness - not really an issue for TickTackToe, but just to make a point ..)

Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
required to allow recognizing the same game status even after serialization (to support network games, GameBooks, etc.). A call to super.equals() is not needed here, as we only care about the board status and not the move history or anything else where the AbstractGame may differ.

Overrides:
equals in class Object


(c) 2001-2006 Holger Antelmann - all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads