de.tabacha.cgo
Class DefaultGame

java.lang.Object
  extended byde.tabacha.cgo.DefaultGame
All Implemented Interfaces:
BackgroundGame, Constants, Game

public class DefaultGame
extends java.lang.Object
implements Constants, BackgroundGame

Represents one game of conwaygo: two players and a board.
$Id: DefaultGame.java,v 1.16 2004/12/23 21:42:43 mk Exp $

Version:
$Revision: 1.16 $
Threadsafe?
false

Field Summary
 
Fields inherited from interface de.tabacha.cgo.Constants
BALL, COL_COUNT, DCOL, DOWN, DROW, E, EMPTY, FIRST_DIRECTION, KICK_OFF_FIELD, LAST_DIRECTION, N, NE, NW, OPPOSITE, PIECE, ROW_COUNT, S, SE, SW, UP, UPSIDEDOWN, W
 
Constructor Summary
DefaultGame(Engine playerUp, Engine playerDown)
          Constructor.
 
Method Summary
 void abort()
          Interrupts the game.
 void addGameListener(GameListener listener)
          Adds a GameListener that will be notified of game events.
 void destroy()
          Kills the threads.
protected  void fireGameAborted(boolean direction, Move move, java.lang.String message)
          Method to support listener events.
protected  void fireGameEnded(boolean winnerDirection)
          Method to support listener events.
protected  void fireGameStarted()
          Method to support listener events.
protected  void fireHasMoved(boolean direction, Move move)
          Method to support listener events.
protected  void firePlayerChanged(boolean newPlayerDirection)
          Method to support listener events.
 Board getBoard()
          Returns the board with the game position.
 Engine getPlayer(boolean direction)
          Returns the engine that plays in the specified direction.
 Engine getPlayerToMove()
          Returns the player who has to move now.
 boolean isAlive()
          Returns true if a game is running.
 void play()
          Starts the game.
 void removeGameListener(GameListener listener)
          Removes a GameListener.
 void setPlayer(boolean direction, Engine player)
          Sets the routine that makes the moves in the specified direction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultGame

public DefaultGame(Engine playerUp,
                   Engine playerDown)
Constructor.

Method Detail

getPlayer

public Engine getPlayer(boolean direction)
Returns the engine that plays in the specified direction.

Specified by:
getPlayer in interface Game

getPlayerToMove

public Engine getPlayerToMove()
Returns the player who has to move now.

Specified by:
getPlayerToMove in interface Game

setPlayer

public void setPlayer(boolean direction,
                      Engine player)
Sets the routine that makes the moves in the specified direction. Change will not take place for the current move.

Specified by:
setPlayer in interface Game

getBoard

public Board getBoard()
Returns the board with the game position. Never returns zero.

Specified by:
getBoard in interface Game

addGameListener

public void addGameListener(GameListener listener)
Description copied from interface: Game
Adds a GameListener that will be notified of game events.

Specified by:
addGameListener in interface Game

removeGameListener

public void removeGameListener(GameListener listener)
Description copied from interface: Game
Removes a GameListener. Nothing will happen if the listener has not been registered.

Specified by:
removeGameListener in interface Game

fireGameStarted

protected void fireGameStarted()
Method to support listener events.


fireGameAborted

protected void fireGameAborted(boolean direction,
                               Move move,
                               java.lang.String message)
Method to support listener events.


fireGameEnded

protected void fireGameEnded(boolean winnerDirection)
Method to support listener events.


fireHasMoved

protected void fireHasMoved(boolean direction,
                            Move move)
Method to support listener events.


firePlayerChanged

protected void firePlayerChanged(boolean newPlayerDirection)
Method to support listener events.


play

public void play()
          throws java.lang.IllegalStateException
Starts the game.

Specified by:
play in interface BackgroundGame
Throws:
java.lang.IllegalStateException - If a player is null or the game is already running.

isAlive

public boolean isAlive()
Returns true if a game is running.

Specified by:
isAlive in interface BackgroundGame

abort

public void abort()
Interrupts the game.

Specified by:
abort in interface BackgroundGame

destroy

public void destroy()
Kills the threads.

In a normal application, the threads will run forever - as long as the VM. This is intended to save the overhead of initializing and starting new threads for each new game.

But when the applet is destroyed, it has to free system resources even if the VM works on.

Specified by:
destroy in interface BackgroundGame