Othello 1.0

edu.drexel.cs.ai.othello
Class GameState

java.lang.Object
  extended by edu.drexel.cs.ai.othello.GameState
All Implemented Interfaces:
java.lang.Cloneable

public class GameState
extends java.lang.Object
implements java.lang.Cloneable

A class for storing all aspects of the game state of Othello, including the board state and current player. This class also includes utilities such as a successor function.

Author:
Evan A. Sultanik

Nested Class Summary
static class GameState.GameStatus
          An enumeration of the possible states of the game.
static class GameState.Player
          An enumeration of the possible owners of a square in the game board.
 
Constructor Summary
GameState()
          Constructs a new GameState with the initial board configuration, a random initial player, and the random number generator seeded to a random value.
GameState(long randomNumberGeneratorSeed)
          Creates a new GameState with the initial board configuration, a random initial player, and the random number generator seeded to the given value.
 
Method Summary
 GameState applyMove(Square move)
          Equivalent to applyMove(move, true).
 GameState applyMove(Square move, boolean includePreviousStateReference)
          Returns the GameState resulting from applying the given move to this state.
 java.lang.Object clone()
          Returns a copy of this GameState.
 boolean equals(java.lang.Object o)
          Returns whether or not o is equivalent to this GameState.
 GameState.Player getCurrentPlayer()
          Returns the player whose turn it is to make a move.
 GameState.Player getOpponent(GameState.Player player)
          Returns the opponent of a player.
 Square getPreviousMove()
          Returns the previous move that was used to get to this state (or null if this is the initial state).
 GameState getPreviousState()
          Returns the previous state (or null if this is the initial state).
 java.util.Random getRandom()
          Returns the random number generator for this game.
 int getScore(GameState.Player player)
          Returns the number of spaces currently owned by the given player.
 GameState.Player getSquare(int row, int col)
          Returns the player that currently owns the given square.
 GameState.Player getSquare(Square square)
          Returns the player that currently owns the given square.
 GameState.GameStatus getStatus()
          Returns the current status of the game.
 java.util.AbstractSet<GameState> getSuccessors()
          Equivalent to getSuccessors(true).
 java.util.AbstractSet<GameState> getSuccessors(boolean includePreviousStateReference)
          Returns all valid GameStates that may succeed this state.
 java.util.AbstractSet<Square> getValidMoves()
          Returns all valid Moves that may be taken from this state.
 java.util.AbstractSet<Square> getValidMoves(GameState.Player player)
          Returns all valid Moves that may be taken by player from this state.
 GameState.Player getWinner()
          Returns the winner of the game or null if the game was either a tie or the game has not yet finished.
 int hashCode()
          Equivalent to calling hashCode() on the result of uniqueHashCode().
 boolean isLegalMove(Square move, GameState.Player player)
          Returns true if and only if move is legal for player.
static void main(java.lang.String[] args)
           
 java.lang.String toString()
          Returns a string representation of the game board.
 java.math.BigInteger uniqueHashCode()
          Returns a unique number identifying this GameState.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GameState

public GameState()
Constructs a new GameState with the initial board configuration, a random initial player, and the random number generator seeded to a random value.


GameState

public GameState(long randomNumberGeneratorSeed)
Creates a new GameState with the initial board configuration, a random initial player, and the random number generator seeded to the given value.

Method Detail

clone

public java.lang.Object clone()
Returns a copy of this GameState. Note that this is a shallow copy; preceding states are not cloned.

Overrides:
clone in class java.lang.Object

getCurrentPlayer

public GameState.Player getCurrentPlayer()
Returns the player whose turn it is to make a move.


getRandom

public java.util.Random getRandom()
Returns the random number generator for this game.


getOpponent

public GameState.Player getOpponent(GameState.Player player)
Returns the opponent of a player.


getSquare

public GameState.Player getSquare(int row,
                                  int col)
Returns the player that currently owns the given square.


getSquare

public GameState.Player getSquare(Square square)
Returns the player that currently owns the given square.


isLegalMove

public boolean isLegalMove(Square move,
                           GameState.Player player)
Returns true if and only if move is legal for player.


getValidMoves

public java.util.AbstractSet<Square> getValidMoves()
Returns all valid Moves that may be taken from this state.


getValidMoves

public java.util.AbstractSet<Square> getValidMoves(GameState.Player player)
Returns all valid Moves that may be taken by player from this state.


getScore

public int getScore(GameState.Player player)
Returns the number of spaces currently owned by the given player.


getWinner

public GameState.Player getWinner()
Returns the winner of the game or null if the game was either a tie or the game has not yet finished.


getStatus

public GameState.GameStatus getStatus()
Returns the current status of the game.


getSuccessors

public java.util.AbstractSet<GameState> getSuccessors()
Equivalent to getSuccessors(true).

See Also:
getSuccessors(boolean)

getSuccessors

public java.util.AbstractSet<GameState> getSuccessors(boolean includePreviousStateReference)
Returns all valid GameStates that may succeed this state.

Note that if includePreviousStateReference is false, the returned states will return null when getPreviousState() is called. This is useful to reduce memory if the back-references are not required.

Parameters:
includePreviousStateReference - whether or not the returned states should have back-references to this.
See Also:
applyMove(Square, boolean)

applyMove

public GameState applyMove(Square move)
                    throws InvalidMoveException
Equivalent to applyMove(move, true).

Throws:
InvalidMoveException
See Also:
applyMove(Square,boolean)

applyMove

public GameState applyMove(Square move,
                           boolean includePreviousStateReference)
                    throws InvalidMoveException
Returns the GameState resulting from applying the given move to this state.

applyMove does not apply the given move to the current state; it does not in any way alter this. Instead, applyMove returns the state that results from making the given move in the current state.

Note that if includePreviousStateReference is false, the returned state will return null when getPreviousState() is called. This is useful to save memory if the back-references to previous states are not required (i.e. the previous states may be garbage collected).

Parameters:
includePreviousStateReference - whether or not the returned state should have a back-reference to this.
Throws:
InvalidMoveException - if move is not a valid move from this state.

getPreviousState

public GameState getPreviousState()
Returns the previous state (or null if this is the initial state). This function may also return null if this was created without a back-reference to the previous state (to save memory).

See Also:
applyMove(Square, boolean)

getPreviousMove

public Square getPreviousMove()
Returns the previous move that was used to get to this state (or null if this is the initial state).


toString

public java.lang.String toString()
Returns a string representation of the game board.

Example:

    a b c d e f g h   [@=2 O=5]
  0 . . . . . . . .
  1 . . . . . . . .
  2 . . . O . . . .
  3 . . . O O . . .
  4 . . @ O @ . . .
  5 . . O . . . . .
  6 . . . . . . . .
  7 . . . . . . . .

Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Returns whether or not o is equivalent to this GameState.

Overrides:
equals in class java.lang.Object
See Also:
uniqueHashCode(), hashCode()

uniqueHashCode

public java.math.BigInteger uniqueHashCode()
Returns a unique number identifying this GameState.

The following are always true:
x.uniqueHashCode() == y.uniqueHashCode()x.equals(y)
x.equals(y)x.uniqueHashCode() == y.uniqueHashCode()
x.equals(y)x.hashCode() == y.hashCode()
However, the following are not necessarily true:
x.hashCode() == y.hashCode()x.equals(y)

See Also:
equals(Object), hashCode()

hashCode

public int hashCode()
Equivalent to calling hashCode() on the result of uniqueHashCode().

Overrides:
hashCode in class java.lang.Object
See Also:
equals(Object), hashCode()

main

public static void main(java.lang.String[] args)

Othello 1.0

Copyright 2006–2007 Evan A. Sultanik