Holger's
Java API

com.antelmann.game
Class BookPlayer

java.lang.Object
  extended by com.antelmann.game.BookPlayer
All Implemented Interfaces:
Player, Serializable

public class BookPlayer
extends Object
implements Player, Serializable

BookPlayer is a Player wrapper that can significantly improve normal Player performance by predefining move selections. This is achieved by internally using a Hashtable object as a book. When asked to select a move in a game, the BookPlayer first checks its book (Hashtable) to see whether a given game is present as a key; If so, it will return the GameMove object stored as the associated value to that key (which is assumed to be the desired move earlier put in the book (which is the Hashtable). Only if the GameBook doesn't contain the game in question, the selection is passed on to the embedded Player object for standard procedure, as all other requests defined in the Player interface are routed to the embedded player as well. Naturally, the BookPlayer relies on a properly maintained book to perform well.

Author:
Holger Antelmann
See Also:
Serialized Form

Constructor Summary
BookPlayer(Player player)
           
BookPlayer(String name, Player player)
          allows the BookPlayer to have a name different from the embedded player
 
Method Summary
 int bookHits()
          returns the number of times that this BookPlayer had a hit on the embedded GameBook while processing a move selection
 boolean canPlayGame(GamePlay game)
          canPlayGame() returns true only if the Player provides an applicable heuristic for the type of game given.
 void changePlayer(Player player)
           
 double evaluate(GamePlay game, GameMove move, int[] role, int level, long milliseconds)
          evaluate() asks the Player to rate a move in the context of a given game stage relative to its role (if the Player has multiple roles in the game, they will all be found in the role array - giving the Player maximum flexibility) while considering the game level for potential game tree search operations and then using heuristic() to evaluate the leaves of the game tree.
 Hashtable getBook()
          allows access to the embedded book to add/remove GamePlay-GameMove pairs
 Player getPlayer()
           
 String getPlayerName()
           
 double heuristic(GamePlay game, GameMove move, int[] role)
          This function - often used as a callback function - evaluates the given move in the context of the given game; it is expected to return quickly.
 boolean pruneMove(GamePlay game, GameMove move, int[] role)
          This method allows the Player to prune a game tree branch by determining that a particular move is not to be considered for further recursive tree search; this method is expected to return quickly.
 GameMove selectMove(GamePlay game, int[] role, int level, long milliseconds)
          first checks the embedded book for a pre-stored move
 void setBook(Hashtable<GamePlay,GameMove> book)
          Sets the internal book used by this BookPlayer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BookPlayer

public BookPlayer(Player player)

BookPlayer

public BookPlayer(String name,
                  Player player)
allows the BookPlayer to have a name different from the embedded player

See Also:
getPlayerName()
Method Detail

getPlayerName

public String getPlayerName()
Specified by:
getPlayerName in interface Player

canPlayGame

public boolean canPlayGame(GamePlay game)
Description copied from interface: Player
canPlayGame() returns true only if the Player provides an applicable heuristic for the type of game given. This way, a player can control whether it can a game.
 //Example:
 if (game instanceof MyGameClass) return true; else return false;
 // or:
 if (game.getClass() == myFavoriteGame.getClass()) return true; else return false;
 

Specified by:
canPlayGame in interface Player

pruneMove

public boolean pruneMove(GamePlay game,
                         GameMove move,
                         int[] role)
Description copied from interface: Player
This method allows the Player to prune a game tree branch by determining that a particular move is not to be considered for further recursive tree search; this method is expected to return quickly.

By default, this method should always return false unless there is a good reason found by a Player to dismiss the tree branch emerging from this move.

Specified by:
pruneMove in interface Player
Returns:
true only if the given move is to be pruned from the game tree search; false otherwise

evaluate

public double evaluate(GamePlay game,
                       GameMove move,
                       int[] role,
                       int level,
                       long milliseconds)
Description copied from interface: Player
evaluate() asks the Player to rate a move in the context of a given game stage relative to its role (if the Player has multiple roles in the game, they will all be found in the role array - giving the Player maximum flexibility) while considering the game level for potential game tree search operations and then using heuristic() to evaluate the leaves of the game tree. Helpful tools to implement this function can be found in the classes TemplatePlayer and GameUtilities. Any double value is allowed for a return value. In general, the higher the rating, the higher the return value should be. Note, though, that the result is not connected to the actual result that a game may return for a player, which may be completely different. The parameter milliseconds is used to indicate that the function is expected to return within that time frame; if milliseconds is 0, only the level limits the thoroughness of the evaluation.

Specified by:
evaluate in interface Player
See Also:
GameUtilities, TemplatePlayer, GamePlay.getResult(int)

heuristic

public double heuristic(GamePlay game,
                        GameMove move,
                        int[] role)
Description copied from interface: Player
This function - often used as a callback function - evaluates the given move in the context of the given game; it is expected to return quickly. This function is really the only function that contains proprietory knowlege about the game (as all other functions could be implemented generically without domain knowlege; this is why the class TemplatePlayer provides already most methods except this function for easy implementations of this interface); heuristic() asks for a heuristic of the move given the game status - treating the status as a leaf in a potential search tree (whereas evaluate() may perform a game tree search before returning a value).

Specified by:
heuristic in interface Player
See Also:
TemplatePlayer

selectMove

public GameMove selectMove(GamePlay game,
                           int[] role,
                           int level,
                           long milliseconds)
first checks the embedded book for a pre-stored move

Specified by:
selectMove in interface Player
See Also:
TemplatePlayer

bookHits

public int bookHits()
returns the number of times that this BookPlayer had a hit on the embedded GameBook while processing a move selection


setBook

public void setBook(Hashtable<GamePlay,GameMove> book)
Sets the internal book used by this BookPlayer. The given book is expected to have GamePlay objects as keys and a GameMove object (to be returned if selectMove() is called on an equal GamePlay object) as values, where the move is expected to be a legal move for each game.


getBook

public Hashtable getBook()
allows access to the embedded book to add/remove GamePlay-GameMove pairs


changePlayer

public void changePlayer(Player player)

getPlayer

public Player getPlayer()


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