Class TicTacToeBoard

java.lang.Object
  |
  +--TicTacToeBoard

public class TicTacToeBoard
extends Object

A TicTacToe Board. This class accepts and records moves (or marks) on a 3 x 3 grid. Squares are numbered 1 through 9, ordered from left to right, starting on the top row and moving down. An example layout is as follows:

      |     |     
   X  |     |     
     1|  2  |3    
 -----+-----+-----
      |     |     
   X  |  O  |     
     4|  5  |6    
 -----+-----+-----
      |     |     
      |     |     
     7|  8  |9    
 
Version:
31 Aug 2003
Author:
Zach Tomaszewski

Field Summary
static int BLANK
          Mark of an empty square
static int O
          Player 2's mark
static int X
          Player 1's mark.
 
Constructor Summary
TicTacToeBoard()
          Default constructor.
 
Method Summary
 int checkEndGame()
          Checks to see if the game has ended.
 void clearBoard()
          Sets all squares on this board to BLANK.
 boolean setMark(int square, int player)
          Sets a player's mark on a square of the board.
protected  char squareToString(int sq)
          Returns a char representation of the mark in this square.
 String toString()
          Returns a string version of this board.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

X

public static final int X
Player 1's mark.

O

public static final int O
Player 2's mark

BLANK

public static final int BLANK
Mark of an empty square
Constructor Detail

TicTacToeBoard

public TicTacToeBoard()
Default constructor. Creates a new board of all BLANK squares.
Method Detail

clearBoard

public void clearBoard()
Sets all squares on this board to BLANK.

setMark

public boolean setMark(int square,
                       int player)
                throws SquareFullException,
                       NoSuchSquareException,
                       NoSuchPlayerException
Sets a player's mark on a square of the board. See class description for a diagram of how squares are numbered. If this play results in a win (presumably for the currently player; a win here means that checkEndGame returns > 0), returns true. Otherwise, returns false.
Parameters:
square - the number of the square to place the player's mark
player - the mark of the current player (such as X or O)
Returns:
true if this move results in a win; false otherwise.
Throws:
SquareFullException - when a mark already exists in the given square
InputOutOfBoundsException - when there is no such square on the board (NoSuchSquareException) or no such player's mark (NoSuchPlayerException)

checkEndGame

public int checkEndGame()
Checks to see if the game has ended. A player has won if there exist 3 of the same marks (other than BLANK) in a row, either horizontally, vertically, or diagonally. If so, this method returns the mark of the winning player. (If more than one player has a winning setup--such as when the calling program has not checked for a winner every turn--will return the mark of the first winner determined). If the game has not yet ended, returns 0 (BLANK). If the game has ended and no one had won, returns -1.
Returns:
the mark of the winning player

squareToString

protected char squareToString(int sq)
Returns a char representation of the mark in this square. For example, BLANK returns ' ', X returns 'X', etc. Will return ' ' if the player mark is unrecognized (an unrecognized player meaning not either X or O).
Parameters:
sq - the number of the square to return
Returns:
a char representation of the given square's mark

toString

public String toString()
Returns a string version of this board. This version is like the diagram shown in the class description: a 3x3 board with each square taking up 5x3 characters. Total dimensions of displayed string is 19 (width; includes a space on each side of the board) by 13 (height; includes a newline above and below the board) characters. Also includes the numbering of the squares.
Overrides:
toString in class Object
Returns:
a String representation of this TicTacToeBoard