CIS 260 - TicTacToe (classes)

The objectives of this assignment are:

  1. create a class that represents a TicTacToe board
  2. 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.

  1. The TicTacToe class should contain the following public enumeration: enum Status { ONGOING, DRAW, PLAYER_1_WIN, PLAYER_2_WIN };
  2. The TicTacToe class should contain the following private data members:
    1. char board[3][3]: each element contains a blank or one of the player symbols
    2. int numMoves: the number of moves (= number of player symbols on board)
    3. char player[2]: contains the player symbols (X and O by default)
    4. Status status: contains the status of the game
  3. The TicTacToe class should contain the following methods (which should be public unless otherwise noted):
    1. 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.
    4. Status getStatus()
      Return the game status.
    5. char getPlayer1()
      Return the player 1 symbol.
    6. char getPlayer2()
      Return the player 2 symbol.
    7. char nextToMove()
      Return the symbol for the player whose turn it is.
    8. void displayInstructions()
      Display game instructions. It is suggested that you copy the instructions from the sample output.
    9. 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.
  4. 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:
    create TicTacToe object
    initialize variables
    while the user want to play another game
        reset the TicTacToe board
        display game instructions
        display the TicTacToe board
        while the game is still active
            get a valid move from the user
            make the move
            display the TicTacToe board
        display the game status (who won or if it was a draw)

Grading rubric

Note: Program must compile without errors.

  1. 5 pts: style conventions followed (no tabs, proper indentation, documentation comments)
  2. 35 pts: class is coded properly
  3. 10 pts: game works properly

Sample output [click to expand/collapse]

This is the game of Tic Tac Toe.
X moves first. Each player chooses their
move by selecting the cell they want to place
their mark in. The cells are numbered as follows:
  1  2  3
  4  5  6
  7  8  9

   |   |
-----------
   |   |
-----------
   |   |

Enter move for player X: bob
Error: Invalid integer
Enter move for player X: 10
Error: Integer above maximum value (9)
Enter move for player X: 0
Error: Integer below minimum value (1)
Enter move for player X: 5
   |   |
-----------
   | X |
-----------
   |   |

Enter move for player O: 1
 O |   |
-----------
   | X |
-----------
   |   |

Enter move for player X: 2
 O | X |
-----------
   | X |
-----------
   |   |

Enter move for player O: 8
 O | X |
-----------
   | X |
-----------
   | O |

Enter move for player X: 6
 O | X |
-----------
   | X | X
-----------
   | O |

Enter move for player O: 2
Enter move for player O: 1
Enter move for player O: 4
 O | X |
-----------
 O | X | X
-----------
   | O |

Enter move for player X: 3
 O | X | X
-----------
 O | X | X
-----------
   | O |

Enter move for player O: 7
 O | X | X
-----------
 O | X | X
-----------
 O | O |

Player O has won.
Play again (Y/N)? bob
Invalid choice
Play again (Y/N)? r
Invalid choice
Play again (Y/N)? y
This is the game of Tic Tac Toe.
X moves first. Each player chooses their
move by selecting the cell they want to place
their mark in. The cells are numbered as follows:
  1  2  3
  4  5  6
  7  8  9

   |   |
-----------
   |   |
-----------
   |   |

Enter move for player X: 1
 X |   |
-----------
   |   |
-----------
   |   |

Enter move for player O: 5
 X |   |
-----------
   | O |
-----------
   |   |

Enter move for player X: 9
 X |   |
-----------
   | O |
-----------
   |   | X

Enter move for player O: 6
 X |   |
-----------
   | O | O
-----------
   |   | X

Enter move for player X: 4
 X |   |
-----------
 X | O | O
-----------
   |   | X

Enter move for player O: 7
 X |   |
-----------
 X | O | O
-----------
 O |   | X

Enter move for player X: 3
 X |   | X
-----------
 X | O | O
-----------
 O |   | X

Enter move for player O: 2
 X | O | X
-----------
 X | O | O
-----------
 O |   | X

Enter move for player X: 8
 X | O | X
-----------
 X | O | O
-----------
 O | X | X

The game is a draw.
Play again (Y/N)? sam
Invalid choice
Play again (Y/N)? 5
Invalid choice
Play again (Y/N)? n