org.jscience.computing.game
Class GameDriver

java.lang.Object
  extended by org.jscience.computing.game.GameDriver
All Implemented Interfaces:
java.io.Serializable, AutoPlay

public class GameDriver
extends java.lang.Object
implements AutoPlay, java.io.Serializable

The GameDriver provides an implementation for an AutoPlay. In addition, it handles Player objects that represent game roles in the GamePlay object, to provide for automated moves.

See Also:
Serialized Form

Constructor Summary
GameDriver(GamePlay game)
          initializes with RandomPlayer objects and level 0
GameDriver(GamePlay game, Player[] players, int level)
           
 
Method Summary
 GameMove autoMove()
          autoMove() makes the move based on heuristics given by the Player; the time taken by the Player to evaluate the move will effect the time kept for that Player.
 Player changePlayer(int gameRole, Player player)
          changePlayer() enables to change the Player for a gameRole while the game is in progress; the old Player object is returned
 java.lang.Object clone()
          Player objects are not cloned, but their references are maintained in the cloned object.
 double evaluateMove(GameMove move)
          This function interfaces with the Player and returns the value of evaluate() while passing on the response time; note that a call to this function does not effect the time kept for the Player in the game.
 long getElapsedTime(int playerRole)
           
 GamePlay getGame()
          returns the underlying GamePlay object
 int getLevel()
          returns the game level
 Player getPlayer(int gameRole)
          getPlayer(gameRole) returns the corresponding Player to the given gameRole - an integer between 0 and (getNumberOfPlayers()-1).
 Player[] getPlayers()
          returns all Player objects that play the game; there may be duplicate Player objects present
 GameMove getRandomLegalMove()
          returns a random legal move or null if no legal moves are available
 long getResponseTime()
          returns the response time limit used by the following functions: hint() autoMove() evaluateMove()
 int[] getRoles(Player player)
          Players could play multiple roles in a game, so getRoles returns an array of Integers.
 GameMove hint(int playerRole)
          hint() interfaces with the Player object that corresponds to the game role of this game.
static GameDriver loadFromFile(java.lang.String fileLocation)
          just a convenience function
 GameMove makeRandomMove()
           
 void saveToFile(java.lang.String fileLocation)
          just a convenience function
 void setLevel(int level)
          The level in the game represents the 'depth' of the search tree (number of subsequent moves) a Player potentially searches through the game tree to evaluate a move.
 void setRandomSeed(long seed)
          sets the seed for the Random object used for generating a random legal move
 void setResponseTime(long milliseconds)
          sets the response time limit for the following functions: hint() autoMove() evaluateMove()
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GameDriver

public GameDriver(GamePlay game)
initializes with RandomPlayer objects and level 0


GameDriver

public GameDriver(GamePlay game,
                  Player[] players,
                  int level)
Method Detail

setRandomSeed

public void setRandomSeed(long seed)
sets the seed for the Random object used for generating a random legal move

See Also:
getRandomLegalMove()

changePlayer

public Player changePlayer(int gameRole,
                           Player player)
                    throws CannotPlayGameException
Description copied from interface: AutoPlay
changePlayer() enables to change the Player for a gameRole while the game is in progress; the old Player object is returned

Specified by:
changePlayer in interface AutoPlay
Throws:
CannotPlayGameException

getElapsedTime

public long getElapsedTime(int playerRole)

setLevel

public final void setLevel(int level)
Description copied from interface: AutoPlay
The level in the game represents the 'depth' of the search tree (number of subsequent moves) a Player potentially searches through the game tree to evaluate a move.

Specified by:
setLevel in interface AutoPlay

getLevel

public final int getLevel()
Description copied from interface: AutoPlay
returns the game level

Specified by:
getLevel in interface AutoPlay
See Also:
AutoPlay.setLevel(int)

autoMove

public GameMove autoMove()
autoMove() makes the move based on heuristics given by the Player; the time taken by the Player to evaluate the move will effect the time kept for that Player. This means that the derived class needs to first determine nextPlayer(), so that hint(nextPlayer()) can be executed. The returned move will be carried out.

Specified by:
autoMove in interface AutoPlay

makeRandomMove

public GameMove makeRandomMove()

getPlayer

public Player getPlayer(int gameRole)
                 throws java.lang.IndexOutOfBoundsException
Description copied from interface: AutoPlay
getPlayer(gameRole) returns the corresponding Player to the given gameRole - an integer between 0 and (getNumberOfPlayers()-1). If that role is not associated with a player, the function returns null.

Specified by:
getPlayer in interface AutoPlay
Throws:
java.lang.IndexOutOfBoundsException

getRoles

public int[] getRoles(Player player)
Description copied from interface: AutoPlay
Players could play multiple roles in a game, so getRoles returns an array of Integers. if a player doesn't play a role in the game, the function returns null.

Specified by:
getRoles in interface AutoPlay

setResponseTime

public void setResponseTime(long milliseconds)
sets the response time limit for the following functions:
  • hint()
  • autoMove()
  • evaluateMove()
  • Specified by:
    setResponseTime in interface AutoPlay

    getResponseTime

    public long getResponseTime()
    returns the response time limit used by the following functions:
  • hint()
  • autoMove()
  • evaluateMove()
  • Specified by:
    getResponseTime in interface AutoPlay

    hint

    public GameMove hint(int playerRole)
                  throws java.lang.IndexOutOfBoundsException
    hint() interfaces with the Player object that corresponds to the game role of this game. The function will select a move based on selectMove() by the Player object and passes on the desired response time set for this AutoPlay object. Note that this implementation doesn't necessarily delegate the evaluation to the player who plays the move, but to GamePlay.nextPlayer(), which may be different at times depending on the game at hand; also, direct calls to this function do not effect the time kept for the Player.

    Specified by:
    hint in interface AutoPlay
    Returns:
    the best legal move based on the preferences of the associated Player object
    Throws:
    java.lang.IndexOutOfBoundsException - if the given playerRole doesn't exist
    See Also:
    Player.selectMove(GamePlay,int[],int,long)

    getPlayers

    public Player[] getPlayers()
    Description copied from interface: AutoPlay
    returns all Player objects that play the game; there may be duplicate Player objects present

    Specified by:
    getPlayers in interface AutoPlay

    getRandomLegalMove

    public GameMove getRandomLegalMove()
    Description copied from interface: AutoPlay
    returns a random legal move or null if no legal moves are available

    Specified by:
    getRandomLegalMove in interface AutoPlay

    evaluateMove

    public double evaluateMove(GameMove move)
    This function interfaces with the Player and returns the value of evaluate() while passing on the response time; note that a call to this function does not effect the time kept for the Player in the game. set for this AutoPlay object

    Specified by:
    evaluateMove in interface AutoPlay
    See Also:
    Player.evaluate(GamePlay,GameMove,int[],int,long)

    saveToFile

    public void saveToFile(java.lang.String fileLocation)
    just a convenience function


    loadFromFile

    public static GameDriver loadFromFile(java.lang.String fileLocation)
    just a convenience function


    toString

    public java.lang.String toString()
    Overrides:
    toString in class java.lang.Object

    clone

    public java.lang.Object clone()
                           throws java.lang.CloneNotSupportedException
    Player objects are not cloned, but their references are maintained in the cloned object.

    Overrides:
    clone in class java.lang.Object
    Throws:
    java.lang.CloneNotSupportedException

    getGame

    public GamePlay getGame()
    Description copied from interface: AutoPlay
    returns the underlying GamePlay object

    Specified by:
    getGame in interface AutoPlay