Algorithm
Development Kit 1.0

algs.model.problems.tictactoe.model
Class Player

java.lang.Object
  extended by algs.model.problems.tictactoe.model.Player
All Implemented Interfaces:
IPlayer
Direct Known Subclasses:
IntelligentAgent, RandomPlayer

public abstract class Player
extends java.lang.Object
implements IPlayer

Represents a Player in a Tic Tac Toe variation.

For maximum flexibility, each player is associated with an IScore scoring function so different players can have different strategies for playing games. This enables an AlphaBeta player to go up against a MiniMax player, for example. The game tree algorithms would otherwise be more complicated.

Make sure that you assign an effective scoring function with each player, otherwise the move search algorithms will not perform effectively.

Since:
1.0
Version:
1.0, 6/15/08
Author:
George Heineman

Field Summary
protected  Logic logic
          Logic for game.
protected  char mark
          Knows its character to play.
static char OMARK
          Global access to OMark.
protected  IGameScore score
          Scoring method used.
static char XMARK
          Global access to XMark.
 
Constructor Summary
Player(char mark)
          Creates a player with a certain mark.
 
Method Summary
abstract  IGameMove decideMove(IGameState board)
          Decide upon a move with the given board state.
 int eval(IGameState state)
          Return the evaluation of this game state from the perspective of the given player.
 char getMark()
          Return the mark for the player.
 char getOpponentMark()
          Return opponent mark.
 int hashCode()
          Enable Player objects to be used in hash table.
 Logic logic()
          Returns logic.
 void logic(Logic logic)
          Set the logic of the game being played.
 IGameScore score()
          Returns scoring method.
 void score(IGameScore score)
          Set the scoring method to use.
 java.lang.String toString()
          Reasonable toString method, revealing logic for player.
 java.util.Collection<IGameMove> validMoves(IGameState state)
          Return the valid moves for this player given the game state.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

mark

protected char mark
Knows its character to play.


OMARK

public static final char OMARK
Global access to OMark.

See Also:
Constant Field Values

XMARK

public static final char XMARK
Global access to XMark.

See Also:
Constant Field Values

score

protected IGameScore score
Scoring method used.


logic

protected Logic logic
Logic for game.

Constructor Detail

Player

public Player(char mark)
Creates a player with a certain mark.

Installs default scoring method that evaluates all boards as 0.

Parameters:
mark - Mark to be played (cannot be ' ')
Throws:
java.lang.IllegalArgumentException - if ' ' is used as the mark.
Method Detail

getMark

public char getMark()
Return the mark for the player.

Returns:
char representing the mark. Will never be ' '.

getOpponentMark

public char getOpponentMark()
Return opponent mark.

Returns:
char representing the mark of the opponent. Will never be ' ' or mark.

decideMove

public abstract IGameMove decideMove(IGameState board)
Decide upon a move with the given board state.

If the player decides to forfeit the game, return null; otherwise a valid Move object is returned that can be played on the given board state.

Parameters:
board -
Returns:
valid Move object, otherwise null to forfeit game.

toString

public java.lang.String toString()
Reasonable toString method, revealing logic for player.

Overrides:
toString in class java.lang.Object

eval

public int eval(IGameState state)
Return the evaluation of this game state from the perspective of the given player. If no evaluation function is in place, then return 0.

Specified by:
eval in interface IPlayer
Parameters:
state - The game state

logic

public Logic logic()
Returns logic.


logic

public void logic(Logic logic)
Set the logic of the game being played.


score

public IGameScore score()
Returns scoring method.


score

public void score(IGameScore score)
Set the scoring method to use.

Specified by:
score in interface IPlayer

hashCode

public int hashCode()
Enable Player objects to be used in hash table.

Note that equals is restricted to be '==' since we do not override .equals

Overrides:
hashCode in class java.lang.Object

validMoves

public java.util.Collection<IGameMove> validMoves(IGameState state)
Return the valid moves for this player given the game state.

Specified by:
validMoves in interface IPlayer
Parameters:
state - Current game state position

Algorithm Development Kit 1.0

This code supports the Algorithms in a Nutshell book, published by O'Reilly Media, Inc. in November 2008. Please visit the book web page to learn of any changes to the code repository or to record a potential defect.