p2.tictactoe
Class AbstractBoardGame

java.lang.Object
  extended by java.util.Observable
      extended by p2.tictactoe.AbstractBoardGame
All Implemented Interfaces:
BoardGame
Direct Known Subclasses:
Gomoku, TicTacToe

public abstract class AbstractBoardGame
extends java.util.Observable
implements BoardGame

AbstractBoardGame implements common methods to TicTacToe and Gomoku.

Version:
$Version$
Author:
$Author: oscar $

Field Summary
protected  int cols_
           
protected  Player[][] gameState_
           
protected  Player nobody_
           
protected  Player[] player_
           
protected  int rows_
           
protected  int squaresLeft_
           
protected  int toJoin_
           
protected  int turn_
           
protected  Player winner_
           
protected  int winningScore_
           
 
Constructor Summary
AbstractBoardGame(Player playerX, Player playerO)
          The state of the game is represented as 3x3 array of chars marked ' ', 'X', or 'O'.
 
Method Summary
protected  void checkWinner(int col, int row)
          New algorithm needed for larger boards.
 Player currentPlayer()
          Returns which Player must play now.
 int get_cols()
          Number of columns in this game.
 int get_rows()
          Number of rows in this game.
 Player get(int col, int row)
          Returns the Player who has played at the given position.
protected abstract  void init()
          Subclasses should initialize _rows, _cols and _winningScore
 boolean inRange(int col, int row)
          Needed for getter and setter preconditions.
protected  boolean invariant()
          These seem obvious, which is exactly why they should be checked.
 Player join()
          Must be called twice, once for each Player.
 void move(int col, int row, Player p)
          Called by the current player.
 boolean notOver()
          The game is not over as long as there is no winner and somebody can still make a move ...
 boolean ready()
          Two Players have joined.
 void restart()
          (Re-)initialize the game state.
protected  void set(int col, int row, Player player)
          Modifies the state of the game.
protected  void setWinner(Player player)
           
 int squaresLeft()
          The number of squares left unplayed.
protected  void swapTurn()
           
 java.lang.String toString()
          A plain ascii representation of the game, mainly for debugging purposes.
 Player winner()
          Returns the winner of the game.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface p2.tictactoe.BoardGame
addObserver, name
 

Field Detail

rows_

protected int rows_

cols_

protected int cols_

winningScore_

protected int winningScore_

gameState_

protected Player[][] gameState_

nobody_

protected final Player nobody_

winner_

protected Player winner_

player_

protected Player[] player_

turn_

protected int turn_

squaresLeft_

protected int squaresLeft_

toJoin_

protected int toJoin_
Constructor Detail

AbstractBoardGame

public AbstractBoardGame(Player playerX,
                         Player playerO)
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'.

Method Detail

init

protected abstract void init()
Subclasses should initialize _rows, _cols and _winningScore


join

public Player join()
Must be called twice, once for each Player.

Specified by:
join in interface BoardGame

ready

public boolean ready()
Two Players have joined.

Specified by:
ready in interface BoardGame

get_cols

public int get_cols()
Description copied from interface: BoardGame
Number of columns in this game.

Specified by:
get_cols in interface BoardGame
Returns:
Returns the _cols.

get_rows

public int get_rows()
Description copied from interface: BoardGame
Number of rows in this game.

Specified by:
get_rows in interface BoardGame
Returns:
Returns the _rows.

set

protected void set(int col,
                   int row,
                   Player player)
Modifies the state of the game. This method is only called by the BoardGame itself when a valid move has taken place. Any Observers are notified that the state has changed.

Parameters:
col - the column
row - the row
player - the Player attempting the move

get

public Player get(int col,
                  int row)
Description copied from interface: BoardGame
Returns the Player who has played at the given position.

Specified by:
get 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

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)
Needed for getter and setter preconditions. Reports true if coordinates are valid.

Specified by:
inRange in interface BoardGame

move

public void move(int col,
                 int row,
                 Player p)
          throws InvalidMoveException
Called by the current player. Here we check if the move is valid. If so the game state is updated and the Observers are notified by calling this.set().

Specified by:
move in interface BoardGame
Throws:
InvalidMoveException

currentPlayer

public Player currentPlayer()
Description copied from interface: BoardGame
Returns which Player must play now.

Specified by:
currentPlayer in interface BoardGame

winner

public Player winner()
Description copied from interface: BoardGame
Returns the winner of the game.

Specified by:
winner in interface BoardGame

squaresLeft

public int squaresLeft()
Description copied from interface: BoardGame
The number of squares left unplayed.

Specified by:
squaresLeft in interface BoardGame

swapTurn

protected void swapTurn()

checkWinner

protected void checkWinner(int col,
                           int row)
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(Player player)

invariant

protected boolean invariant()
These seem obvious, which is exactly why they should be checked.


restart

public void restart()
(Re-)initialize the game state.

Specified by:
restart in interface BoardGame