Holger's
Java API

com.antelmann.game
Class SocketPlayer

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

public class SocketPlayer
extends Object
implements Player, Serializable

SocketPlayer is a wrapper around a standard Player object. It handles all requests to the Player by routing it through the network to a SocketPlayerServer, who embedds the actual object that returns the results for the AutoPlay. Limitation: any exceptions that may be thrown by the SocketPlayerServer are not properly propagated, but show up as simple IOExceptions, which are not handled with much sophistication.

Author:
Holger Antelmann
See Also:
SocketPlayerServer, Player, AutoPlay, Serialized Form

Field Summary
static int SO_TIMEOUT
           
 
Constructor Summary
SocketPlayer(String hostName, int port)
          sets the host and port number where the SocketPlayerServer is listening
SocketPlayer(String hostName, int port, boolean keepAlive)
          when keepAlive is set to true, the SocketPlayer will reuse the current socket connection for further requests
 
Method Summary
 boolean canPlayGame(GamePlay game)
          canPlayGame() returns true only if the Player provides an applicable heuristic for the type of game given.
protected  void cleanup()
           
 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.
protected  void finalize()
           
 String getHostname()
           
 String getPlayerName()
           
 int getPort()
           
protected  boolean handshake()
           
 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)
          selectMove() asks the Player to select a move out of GamePlay.legalMove() based on the role it plays; milliseconds is an indication of how long the calling function is willing to wait for an answer before continuing with potentially a randomMove or something else.
 void sendMessage(Object message)
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SO_TIMEOUT

public static int SO_TIMEOUT
Constructor Detail

SocketPlayer

public SocketPlayer(String hostName,
                    int port)
sets the host and port number where the SocketPlayerServer is listening

See Also:
SocketPlayerServer

SocketPlayer

public SocketPlayer(String hostName,
                    int port,
                    boolean keepAlive)
when keepAlive is set to true, the SocketPlayer will reuse the current socket connection for further requests

Method Detail

handshake

protected boolean handshake()

cleanup

protected void cleanup()

finalize

protected void finalize()
Overrides:
finalize in class Object

getPlayerName

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

sendMessage

public void sendMessage(Object message)

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

heuristic

public double heuristic(GamePlay game,
                        GameMove move,
                        int[] role)
                 throws CannotPlayGameException
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
Throws:
CannotPlayGameException - if the game cannot be played by the player
See Also:
TemplatePlayer

evaluate

public double evaluate(GamePlay game,
                       GameMove move,
                       int[] role,
                       int level,
                       long milliseconds)
                throws CannotPlayGameException
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
Throws:
CannotPlayGameException - if the game cannot be played by the player
See Also:
GameUtilities, TemplatePlayer, GamePlay.getResult(int)

selectMove

public GameMove selectMove(GamePlay game,
                           int[] role,
                           int level,
                           long milliseconds)
                    throws CannotPlayGameException
Description copied from interface: Player
selectMove() asks the Player to select a move out of GamePlay.legalMove() based on the role it plays; milliseconds is an indication of how long the calling function is willing to wait for an answer before continuing with potentially a randomMove or something else. The functionality of selectMove() can easily be implemented generically by using evaluate() and/or heuristic(), but for a Player that may rely on user interaction or remote connections to Player objects, this function call is more natural. If milliseconds is set to 0, only the level is used to limit the evaluation during move selection; otherwise milliseconds indicates that the function is expected to return within the timeframe given through milliseconds. A template implementation of this function (utilizing evaluate() and heuristic()) is available in the class TemplatePlayer.

Specified by:
selectMove in interface Player
Throws:
CannotPlayGameException
See Also:
TemplatePlayer

getHostname

public String getHostname()

getPort

public int getPort()

toString

public String toString()
Overrides:
toString 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