org.generation5.ai
Class BoardGame

java.lang.Object
  extended byorg.generation5.ai.BoardGame
All Implemented Interfaces:
Steppable, Visualizable

public abstract class BoardGame
extends java.lang.Object
implements Visualizable, Steppable

This class is designed to faciliate development of board games, with AI agent players (see BoardGameAgent). This class implements both Visualizable and Steppable. The board is visualized simply as celled circles denoting the pieces: colours can be specified at runtime. This class also provides a handy means of displaying an influence map, if used by the AI agents. Steppable has been implemented for the purposes of running quick simulations between AI agents without human intervention/participation. Any deriving class handles all the rules of the game, and all moves are supplied using a generic array of integers, allowing for simple {x,y} coordinate moves, or more complicated moves that (for example) require a starting piece too.

See Also:
BoardGameAgent, InfluenceMap

Field Summary
protected  BoardGameAgent[] gameAgents
          The game agents.
protected  int[][] gameBoard
          The game board.
protected  int height
          The height of the board.
protected  Gradient influenceGradient
          The gradient used to render the influence map.
protected  int pieceSize
          The size of the pieces when rendered.
protected  java.awt.Color[] playerColors
          The player colours.
protected  InfluenceMap renderMap
          The influence map to render.
static int STATUS_DRAW
          The game has been drawn.
static int STATUS_INPROGRESS
          The game is still in progress.
protected  int width
          The width of the board.
 
Constructor Summary
BoardGame(int width, int height, int numPlayers)
          Create an instance of BoardGame, specifying the dimensions and number of players involved.
 
Method Summary
 void addPlayer(int playerID, BoardGameAgent agent)
          Add a player to the game.
 void addPlayer(int playerID, BoardGameAgent agent, java.awt.Color pieceColor)
          Add a player, and specify the colour.
 void doStep()
          Step the board game.
 int getBoardAt(int x, int y)
          Return the board value.
 int getCountOf(int type)
          Return the number of values with a specified value (player ID).
 int getHeight()
          Get the height of the board.
 int getPieceSize()
          Return the piece size.
 int getStatus()
          Simply calls getStatus(0)
abstract  int getStatus(int currPlayer)
          Return the status of this game.
abstract  int getTotalPlayers()
          Get the total number of participating players.
 int getWidth()
          Get the width of the board.
abstract  void init()
          Initialize the board game.
 boolean isWithinBoard(int x, int y)
          A utility method to check whether the given coordinate is within the board game bounds.
abstract  int move(int playerID, int[] move)
          Make the move.
 void render(java.awt.Graphics g, int ww, int hh)
          Render the board game.
 void reset()
          Reset the board game (simply calls init).
 void resetBoard()
          Reset the board to zero.
 void setBoardAt(int x, int y, int board)
          Set the board at the specified position.
 void setPieceColors(java.awt.Color[] pieceColors)
          Set the piece colours.
 void setPieceSize(int pieceSize)
          Set the piece size.
 void setRenderMap(InfluenceMap map)
          Set the influence map to render.
 void setRenderMap(InfluenceMap map, Gradient gradient)
          Set the influence map to render, along with a gradient.
abstract  boolean validMove(int playerID, int[] move)
          This abstract method must return whether the supplied move is valid for the given player.
 void writeImage(java.lang.String s, int width, int height)
          Write an image to disk.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATUS_DRAW

public static final int STATUS_DRAW
The game has been drawn.

See Also:
getStatus(int), Constant Field Values

STATUS_INPROGRESS

public static final int STATUS_INPROGRESS
The game is still in progress.

See Also:
getStatus(int), Constant Field Values

width

protected int width
The width of the board.


height

protected int height
The height of the board.


pieceSize

protected int pieceSize
The size of the pieces when rendered.


renderMap

protected InfluenceMap renderMap
The influence map to render.


gameBoard

protected int[][] gameBoard
The game board.


gameAgents

protected BoardGameAgent[] gameAgents
The game agents.


playerColors

protected java.awt.Color[] playerColors
The player colours.


influenceGradient

protected Gradient influenceGradient
The gradient used to render the influence map.

Constructor Detail

BoardGame

public BoardGame(int width,
                 int height,
                 int numPlayers)
Create an instance of BoardGame, specifying the dimensions and number of players involved. This is the only constructor, to force deriving classes to specify these details upon construction.

Parameters:
width - board width.
height - board height.
numPlayers - number of participating players.
Method Detail

validMove

public abstract boolean validMove(int playerID,
                                  int[] move)
This abstract method must return whether the supplied move is valid for the given player.

Parameters:
playerID - the player ID.
move - the move.
Returns:
true if move is valid, otherwise false.

move

public abstract int move(int playerID,
                         int[] move)
Make the move. This is normally where many of the board game rules will be played out.

Parameters:
playerID - the player moving.
move - the move itself.
Returns:
a generic return value, for use by the programmer.

getTotalPlayers

public abstract int getTotalPlayers()
Get the total number of participating players.

Returns:
total players.

getStatus

public abstract int getStatus(int currPlayer)
Return the status of this game. This function should return STATUS_INPROGRESS (0) if the game is currently running, STATUS_DRAW (-1) if the game has been drawn, otherwise it should return the ID of the player who has won. The method can be called with the current player to satisfy some other board game rules.

Parameters:
currPlayer - the current player.
Returns:
game status code (see above).

getStatus

public int getStatus()
Simply calls getStatus(0)

Returns:
the result of getStatus(0).
See Also:
getStatus(int)

getWidth

public int getWidth()
Get the width of the board.

Returns:
the board width.

getHeight

public int getHeight()
Get the height of the board.

Returns:
the board height.

setPieceSize

public void setPieceSize(int pieceSize)
Set the piece size.

Parameters:
pieceSize - the new piece size.

getPieceSize

public int getPieceSize()
Return the piece size.

Returns:
the current piece size.

setBoardAt

public void setBoardAt(int x,
                       int y,
                       int board)
Set the board at the specified position.

Parameters:
x - x-position to set.
y - y-position to set.
board - the value to set.

getBoardAt

public int getBoardAt(int x,
                      int y)
Return the board value.

Parameters:
x - the x-position.
y - the y-position.
Returns:
the board value.

getCountOf

public int getCountOf(int type)
Return the number of values with a specified value (player ID).

Parameters:
type - the piece type/player ID to count.
Returns:
the number of pieces specified as type.

setPieceColors

public void setPieceColors(java.awt.Color[] pieceColors)
Set the piece colours. Note that Player x will be rendered in colour x-1.

Parameters:
pieceColors - the array of piece colours.

addPlayer

public void addPlayer(int playerID,
                      BoardGameAgent agent)
Add a player to the game. This method sets the player's board game pointer and player ID accordingly. Note that player IDs start at 1 (as in, there is no player 0).

Parameters:
playerID - the player ID.
agent - the agent to add.

addPlayer

public void addPlayer(int playerID,
                      BoardGameAgent agent,
                      java.awt.Color pieceColor)
Add a player, and specify the colour.

Parameters:
playerID - the player ID.
agent - the agent.
pieceColor - the piece colour.
See Also:
addPlayer(int, BoardGameAgent)

isWithinBoard

public boolean isWithinBoard(int x,
                             int y)
A utility method to check whether the given coordinate is within the board game bounds.

Parameters:
x - the x-coordinate.
y - the y-coordinate.
Returns:
true, if a valid position, otherwise false.

resetBoard

public void resetBoard()
Reset the board to zero.


doStep

public void doStep()
Step the board game. This method is defined as: int[] nextMove; int players = getTotalPlayers(); for (int p=0; p<players; p++) { do { nextMove = gameAgents[p].think(); } while (validMove(p+1, nextMove) == false); move(p+1, nextMove); }

Specified by:
doStep in interface Steppable

init

public abstract void init()
Initialize the board game.

Specified by:
init in interface Steppable

setRenderMap

public void setRenderMap(InfluenceMap map)
Set the influence map to render.

Parameters:
map - the influence map to render.

setRenderMap

public void setRenderMap(InfluenceMap map,
                         Gradient gradient)
Set the influence map to render, along with a gradient. The default gradient is white to yellow to red.

Parameters:
map - the influence map to render.
gradient - the gradient with which to render it.

render

public void render(java.awt.Graphics g,
                   int ww,
                   int hh)
Render the board game.

Specified by:
render in interface Visualizable
Parameters:
g - the graphics context.
ww - the width of the context.
hh - the height of the context.

reset

public void reset()
Reset the board game (simply calls init).

Specified by:
reset in interface Steppable

writeImage

public void writeImage(java.lang.String s,
                       int width,
                       int height)
Write an image to disk.

Specified by:
writeImage in interface Visualizable
Parameters:
s - the filename.
width - the image width.
height - the image height.

This documentation is part of the Generation5 JDK.