Algorithm
Development Kit 1.0

algs.model.problems.tictactoe.model
Class TicTacToeBoard

java.lang.Object
  extended by algs.model.problems.tictactoe.model.TicTacToeBoard

public class TicTacToeBoard
extends java.lang.Object

Represents the state of a 3x3 TicTacToe board. Each element within the board is a char, either 'x' or 'o'. Note that an empty square should be represented by the char ' ';

Since:
1.0
Version:
1.0, 6/15/08
Author:
George Heineman

Field Summary
protected  char[][] cells
          Store data as a 3x3 array of cells.
static char EMPTY
          Empty Marker.
static Cell[][] filters
          These will reflect the cell ordering.
static int MaxC
          Max number of columns.
static int MaxR
          Max number of rows.
 
Constructor Summary
TicTacToeBoard()
          Constructor for TicTacToe Board.
TicTacToeBoard(char[][] test)
          Helper constructor for initializing boards to arbitrary configurations.
TicTacToeBoard(TicTacToeBoard board)
          Copy Constructor for TicTacToe Board.
 
Method Summary
 void clear(int col, int row)
          Clears the cell at given location.
 boolean equals(java.lang.Object o)
           
 boolean gameWon()
          Determines whether game has been won.
 boolean gameWon(char mark)
          Determines whether game has been won by given mark.
 char get(int col, int row)
          Returns marker at given spot.
 int hashCode()
          To enable this board to be a key in a Hashtable.
 boolean isClear()
          Return true if entire board is empty.
 boolean isClear(int col, int row)
          Determines if cell is empty.
 boolean isDraw()
          Determine if a game is a draw because all cells are occupied but the game is not won.
 int numColumns()
          Return the number of cells on the board.
 int numRows()
          Return the number of rows on the board.
 boolean sameBoard(TicTacToeBoard board2)
          Determine if two Boards are the same (including rotations and reflections).
 boolean set(int col, int row, char mark)
          Sets the cell at given location to contain mark.
 java.lang.String toString()
          Return the state of the board as a String.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

cells

protected char[][] cells
Store data as a 3x3 array of cells.


EMPTY

public static final char EMPTY
Empty Marker.

See Also:
Constant Field Values

MaxC

public static final int MaxC
Max number of columns.

See Also:
Constant Field Values

MaxR

public static final int MaxR
Max number of rows.

See Also:
Constant Field Values

filters

public static final Cell[][] filters
These will reflect the cell ordering.

Constructor Detail

TicTacToeBoard

public TicTacToeBoard()
Constructor for TicTacToe Board.


TicTacToeBoard

public TicTacToeBoard(char[][] test)
Helper constructor for initializing boards to arbitrary configurations. Exercise suitable error control to avoid BAD patterns.


TicTacToeBoard

public TicTacToeBoard(TicTacToeBoard board)
Copy Constructor for TicTacToe Board.

Method Detail

numColumns

public int numColumns()
Return the number of cells on the board.

Returns:
number of columns in the board.

numRows

public int numRows()
Return the number of rows on the board.

Returns:
number of rows in the board.

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
To enable this board to be a key in a Hashtable.

Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Return the state of the board as a String.

Overrides:
toString in class java.lang.Object
Returns:
board state as a String.

gameWon

public boolean gameWon()
Determines whether game has been won.

Returns:
true if some player has won game.

gameWon

public boolean gameWon(char mark)
Determines whether game has been won by given mark. Winning is determine by three in a row: vertical, horizontal, diagonal too. REQUIRES: mark != EMPTY

Parameters:
mark - character of player mark to be investigated.
Returns:
true if player with given mark has won game.

get

public char get(int col,
                int row)
Returns marker at given spot. REQUIRES: 0 <= col <= MaxC AND 0 <= row <= MaxR

Parameters:
col - desired column
row - desired row
Returns:
char marker in given location

isClear

public boolean isClear(int col,
                       int row)
Determines if cell is empty. REQUIRES: 0 <= col <= MaxC AND ) AND 0 <= row <= MaxR

Parameters:
col - desired column
row - desired row
Returns:
true if cell is empty; false otherwise

isDraw

public boolean isDraw()
Determine if a game is a draw because all cells are occupied but the game is not won.

Returns:
true if there is no winning position AND all cells are occupied.

clear

public void clear(int col,
                  int row)
Clears the cell at given location. REQUIRES: 0 < col < numColumns() AND 0 < row < numRows()

Parameters:
col - column of cell to clear
row - row of cell to clear

set

public boolean set(int col,
                   int row,
                   char mark)
Sets the cell at given location to contain mark. REQUIRES: 0 < col < numColumns() AND 0 < row < numRows() AND mark != EMPTY

Parameters:
col - column of cell to set
row - row of cell to set
mark - mark to be inserted.
Returns:
true if cell was empty, otherwise returns false.

isClear

public boolean isClear()
Return true if entire board is empty.

Returns:
true if all cells are set to EMPTY.

sameBoard

public boolean sameBoard(TicTacToeBoard board2)
Determine if two Boards are the same (including rotations and reflections). 123 741 987 369 (ROTATIONS) 456 852 654 258 789 963 321 147 147 789 963 321 (Diagonal reflection) 258 456 852 654 369 123 741 987

Parameters:
board2 -

Algorithm Development Kit 1.0

This code supports the Algorithms in a Nutshell book, published by O'Reilly Media, Inc. in November 2008. Please visit the book web page to learn of any changes to the code repository or to record a potential defect.