Classy TicTacToe

Objectives

  • Create a class that represents a TicTacToe board
  • Develop and implement code that uses the TicTacToe class to play the game

Assignment

This assignment is all about creating and using a Java class. You start by creating a TicTacToe class which represents a TicTacToe board. You then have to develop the code that uses the TicTacToe class to actually play a game of TicTacToe. You should use the sample output given on this page to test your program.

Requirements

  • (2) The TicTacToe class should contain the following public enumeration: enum Status { ONGOING, DRAW, PLAYER_1_WIN, PLAYER_2_WIN };
  • (4) The TicTacToe class should contain the following private data members:
    • char board[3][3]: each element contains a blank or one of the player symbols
    • int numMoves: the number of moves (= number of player symbols on board)
    • char player[2]: contains the player symbols (X and O by default)
    • Status status: contains the status of the game
  • The TicTacToe class should contain the following methods (which should be public unless otherwise noted):
    • (7) private boolean checkForWin(char c)
      Checks to see if player represented by symbol c has won the game. Returns true if player won or false otherwise.
    • (2) TicTacToe()
      This constructor just calls reset().
    • (3) void reset()
      Set the player symbols to X and O. Reset numMoves. Set the status to ONGOING. Set all board elements to spaces.
    • (1) Status getStatus()
      Return the game status.
    • (1) char getPlayer1()
      Return the player 1 symbol from the player array.
    • (1) char getPlayer2()
      Return the player 2 symbol from the player array.
    • (2) char nextToMove()
      Return the symbol for the player whose turn it is.
    • (2) void displayInstructions()
      Display game instructions. It is suggested that you copy the instructions from the sample output.
    • (5) String toString()
      Returns a String representation of the current board. See the sample output for an example of what the output of this method should look like.
    • (10) boolean move(int cell);
      This is the workhorse of the class. It has to do some error checking, then record the move, and set the game status. It returns true if the move was accepted and false otherwise. Error checking consists of making sure the cell number is between 1 and 9, making sure the game status is ONGOING and making sure the specified cell is currently blank. If a move is valid, then the specified board element is set to the symbol for the player whose turn it is and the number of moves is incremented. The last step is to update the game status. If the player who made the move has won, then set the status to reflect who has won. If the player has not won, then check for a draw. You will know a draw because the player who moved didn't win and the number of moves is 9.
  • (10) The main program that actually plays the TicTacToe game is not too difficult if the TicTacToe class has been implemented properly. A general pseudocode outline of the algorithm would go something like this:

Grading rubric

Note: Program must compile without errors. Point values are given for each item in the requirements section.

Sample output