Class AbstractBoardGame

java.lang.Object
  |
  +--AbstractBoardGame
Direct Known Subclasses:
Gomoku, TicTacToe

public abstract class AbstractBoardGame
extends java.lang.Object
implements BoardGame

AbstractBoardGame implements common methods to TicTacToe and Gomoku

Version:
1.6 1999-02-07
Author:
Oscar.Nierstrasz@acm.org

Field Summary
protected  int _cols
           
protected  char[][] _gameState
           
protected  Player[] _player
           
protected  int _rows
           
protected  int _squaresLeft
           
protected  int _turn
           
protected  Player _winner
           
protected  int _winningScore
           
 
Constructor Summary
AbstractBoardGame()
           
 
Method Summary
protected  void assert(boolean assertion)
           
protected  void checkWinner(int col, int row)
          New algorithm needed for larger boards.
 Player currentPlayer()
           
 char get(int col, int row)
          Needed by Runner
protected  int getCol(java.lang.String coord)
           
protected  int getRow(java.lang.String coord)
           
protected  void init(int rows, int cols, int score, Player playerX, Player playerO)
          Generic initialization for BoardGame.
 boolean inRange(int col, int row)
           
 void move(java.lang.String coord, char mark)
          Called by the current player during an update().
 boolean notOver()
          The game is not over as long as there is no winner and somebody can still make a move ...
protected  void set(int col, int row, char mark)
          set() and get() translate between chess coordinates and array indices.
protected  void setWinner(char player)
          Look up which player is the winner, and set _winner accordingly.
 int squaresLeft()
           
protected  void swapTurn()
           
 void test()
          Some generic tests for both TicTacToe and Gomoku.
 java.lang.String toString()
          A plain ascii representation of the game, mainly for debugging purposes.
 void update()
          Ask the current player to make a move.
 Player winner()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_gameState

protected char[][] _gameState

_rows

protected int _rows

_cols

protected int _cols

_winningScore

protected int _winningScore

_winner

protected Player _winner

_player

protected Player[] _player

_turn

protected int _turn

_squaresLeft

protected int _squaresLeft
Constructor Detail

AbstractBoardGame

public AbstractBoardGame()
Method Detail

init

protected void init(int rows,
                    int cols,
                    int score,
                    Player playerX,
                    Player playerO)
Generic initialization for BoardGame. The state of the game is represented as 3x3 array of chars marked ' ', 'X', or 'O'. We index the state using chess notation, i.e., column is 'a' through 'c' and row is '1' through '3'.

set

protected void set(int col,
                   int row,
                   char mark)
            throws AssertionException
set() and get() translate between chess coordinates and array indices.

get

public char get(int col,
                int row)
         throws AssertionException
Needed by Runner
Specified by:
get in interface BoardGame

update

public void update()
            throws java.io.IOException
Ask the current player to make a move.
Specified by:
update in interface BoardGame

currentPlayer

public Player currentPlayer()
Specified by:
currentPlayer in interface BoardGame

move

public void move(java.lang.String coord,
                 char mark)
          throws AssertionException
Called by the current player during an update(). The player attempts to put a mark at a location.
Specified by:
move in interface BoardGame

getCol

protected int getCol(java.lang.String coord)
              throws AssertionException

getRow

protected int getRow(java.lang.String coord)
              throws AssertionException

swapTurn

protected void swapTurn()

winner

public Player winner()
Specified by:
winner in interface BoardGame

notOver

public boolean notOver()
The game is not over as long as there is no winner and somebody can still make a move ...
Specified by:
notOver in interface BoardGame

squaresLeft

public int squaresLeft()
Specified by:
squaresLeft in interface BoardGame

checkWinner

protected void checkWinner(int col,
                           int row)
                    throws AssertionException
New algorithm needed for larger boards. Create a "Runner" that starts at the current position, and runs back in forth in a given direction, returning the length of the run (i.e., number of consecutive pieces of the same Player). If the number is big enough, the current Player wins.

setWinner

protected void setWinner(char player)
Look up which player is the winner, and set _winner accordingly. Hm. Maybe we should store Players instead of chars in our array!

toString

public java.lang.String toString()
A plain ascii representation of the game, mainly for debugging purposes.
Overrides:
toString in class java.lang.Object

inRange

public boolean inRange(int col,
                       int row)
Specified by:
inRange in interface BoardGame

assert

protected void assert(boolean assertion)
               throws AssertionException

test

public void test()
Some generic tests for both TicTacToe and Gomoku.
Specified by:
test in interface BoardGame