tictactoe
Class AbstractBoardGame
java.lang.Object
|
+--java.util.Observable
|
+--tictactoe.AbstractBoardGame
- Direct Known Subclasses:
- Gomoku, TicTacToe
- public abstract class AbstractBoardGame
- extends java.util.Observable
- implements BoardGame
AbstractBoardGame implements common methods to
TicTacToe and Gomoku.
- Version:
- 2.0 1999-03-26
- Author:
- Oscar.Nierstrasz@acm.org
- See Also:
BoardGame
,
TicTacToe
,
Gomoku
Method Summary |
protected void |
assert(boolean assertion)
|
protected void |
checkWinner(int col,
int row)
This algorithm works both for TicTacToe and Gomoku. |
int |
cols()
|
Player |
currentPlayer()
|
Player |
get(int col,
int row)
Used by Runner to detect a winning run of marks. |
protected void |
init(int cols,
int rows,
int score,
Player playerX,
Player playerO)
Generic initialization for BoardGame. |
protected void |
initFailed(AssertionException err)
|
boolean |
inRange(int col,
int row)
|
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 ... |
int |
rows()
|
protected void |
set(int col,
int row,
Player player)
Modifies the state of the game. |
protected void |
setWinner(Player player)
|
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. |
Player |
winner()
|
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 |
_gameState
protected Player[][] _gameState
_rows
protected int _rows
_cols
protected int _cols
_winningScore
protected int _winningScore
_nobody
protected final Player _nobody
_winner
protected Player _winner
_player
protected Player[] _player
_turn
protected int _turn
_squaresLeft
protected int _squaresLeft
AbstractBoardGame
public AbstractBoardGame()
init
protected void init(int cols,
int rows,
int score,
Player playerX,
Player playerO)
- Generic initialization for BoardGame.
The state of the game is represented as an
array of Players.
- Parameters:
cols
- the number of columnsrows
- the number of rowsscore
- the winning score (3 for TicTacToe; 5 for Gomoku)playerX
- the player whose mark is 'X'playerO
- the player whose mark is 'Y'
initFailed
protected void initFailed(AssertionException err)
cols
public int cols()
- Specified by:
- cols in interface BoardGame
- Returns:
- the number of columns of this game
rows
public int rows()
- Specified by:
- rows in interface BoardGame
- Returns:
- the number of rows
set
protected void set(int col,
int row,
Player player)
throws AssertionException
- 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 columnrow
- the rowplayer
- the Player attempting the move
get
public Player get(int col,
int row)
throws AssertionException
- Used by Runner to detect a winning run of marks.
- Specified by:
- get in interface BoardGame
- Returns:
- the current mark at the given location on the Board
- See Also:
Runner#samePlayer
currentPlayer
public Player currentPlayer()
- Specified by:
- currentPlayer in interface BoardGame
move
public void move(int col,
int row,
Player p)
throws AssertionException
- 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
- See Also:
StreamPlayer.move()
,
AppletPlayer.move(int, int)
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
- This algorithm works both for TicTacToe and Gomoku.
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.
- Parameters:
col
- the column of the current moverow
- the row of the current move
setWinner
protected void setWinner(Player player)
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
- Returns:
- true if the arguments identify a valid position on the board
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