sandy.game.core
Class GameBoard

java.lang.Object
  |
  +--sandy.game.core.GameBoard

public abstract class GameBoard
extends java.lang.Object

This class represents an abstract GameBoard for any multi-player, tabular, strategy games likes Chess, Tic-Tac-Toe, Draught, Othello, Chinese-Checkers etc. This class provides the following functionality to its sub-classes:

  1. This game board assumes all the sub-classes ( specific game boards ) will be rectangular (specific rows, columns). Since maximum of the board games are rectangular in nature, this seems a fair assumptions. However, there are games like Chinese-Checkers where the board is not rectangular but star shaped. In these cases the sub-classes need to mask certain positions on the board to get the game started.
  2. This game board has a two dimentional protected array positions for keeping track of the positions on the board. Position is again a generic concept which specific games should implement.
  3. This game board keeps track of all the moves made on this board, in a LIFO manner.
  4. This game board provides funtionality for registering move listeners. Move Listeners are parties which are interested in a notification, whenever a move is made on the game board.


Field Summary
protected  java.util.Stack moves
           
protected  int numCols
           
protected  int numRows
           
protected  Position[][] positions
           
 
Constructor Summary
protected GameBoard(int numRows, int numCols)
          The constructor which takes the number of rows and columns in the game board.
 
Method Summary
 void addMoveListener(GameBoardMoveListener moveListener)
           
abstract  java.util.Vector getAllPossibleMoves(int perspective)
          This abstract method should be implemented by the sub-classes to provide logic to return all the possible moves for a given perspective at the current state of the board.
abstract  int getEnemyPerspective(int currentPerspective)
          This function should return the opponent perspective for the given currentPerspective.
 int getNumCols()
           
 int getNumRows()
           
 Position getPosition(int row, int col)
          Gets a specific position in the game board as specified by the row and col parameters.
abstract  GameBoard getScratchBoard()
          This function should be implemented by the subclasses to return a DEEP COPY of the gameboard in the current state.
abstract  void initializeBoard()
          This is an abstract call back method, which is invoked during the board construction time (constructor).
abstract  boolean isGameActive()
          This function should return whether the game is active.
abstract  boolean isValidMove(Move move)
          This function should analyze the given move and return whether the move is a valid move or not.
 void makeMove(Move move)
          Make a move on the game board.
abstract  void printBoard()
          This is a debug function, which is used to print the board.
 void removeMoveListener(GameBoardMoveListener moveListener)
           
abstract  void undoLastMove()
          This function should undo the last move made.
abstract  void updateGameBoard(Move move)
          This abstract method is called when a move is made on the board.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

numRows

protected int numRows

numCols

protected int numCols

positions

protected Position[][] positions

moves

protected java.util.Stack moves
Constructor Detail

GameBoard

protected GameBoard(int numRows,
                    int numCols)
The constructor which takes the number of rows and columns in the game board. This method calls back an abstract method initializeBoard, which should be implemented by the sub-classes to provide logic for initializing the gameboard to an initial or a predefined state.
Method Detail

initializeBoard

public abstract void initializeBoard()
This is an abstract call back method, which is invoked during the board construction time (constructor). This method should be implemented by the subclasses to initialize the game board to a predefined or an initial state.

getAllPossibleMoves

public abstract java.util.Vector getAllPossibleMoves(int perspective)
This abstract method should be implemented by the sub-classes to provide logic to return all the possible moves for a given perspective at the current state of the board.
Parameters:
perspective - The perspective in the board. Perspective can be thought of as a side in the game. This is a game dependent concept, for example in Tic-Tac-Toe perspective can mean either CROSS or CIRCLE, in Chess it may mean either BLACK or WHITE.. etc.

makeMove

public void makeMove(Move move)
Make a move on the game board. This method, calls back a method on the sub-classes updateGameBoard, which should be implemented the specific logic of making a move, also this method informs all the move listeners of the following events.
Parameters:
move - The game specific move.

updateGameBoard

public abstract void updateGameBoard(Move move)
This abstract method is called when a move is made on the board. Sub-classes should provide game specific implementations for updating the game board. For example in case of Tic-Tac-Toe, a move would mean updating the position with the perspective, whereas in case of Chess it would be more complex as we have to look out for capture moves.

undoLastMove

public abstract void undoLastMove()
This function should undo the last move made. The past moves are available in a Stack named moves.

isValidMove

public abstract boolean isValidMove(Move move)
This function should analyze the given move and return whether the move is a valid move or not.

getEnemyPerspective

public abstract int getEnemyPerspective(int currentPerspective)
This function should return the opponent perspective for the given currentPerspective.

isGameActive

public abstract boolean isGameActive()
This function should return whether the game is active.

getScratchBoard

public abstract GameBoard getScratchBoard()
This function should be implemented by the subclasses to return a DEEP COPY of the gameboard in the current state.

printBoard

public abstract void printBoard()
This is a debug function, which is used to print the board.

addMoveListener

public void addMoveListener(GameBoardMoveListener moveListener)

removeMoveListener

public void removeMoveListener(GameBoardMoveListener moveListener)

getNumRows

public int getNumRows()

getNumCols

public int getNumCols()

getPosition

public Position getPosition(int row,
                            int col)
Gets a specific position in the game board as specified by the row and col parameters.