Home

TicTacToe


The TicTacToe class represents the game board as a 3x3 array of char. Possible values for the array items are ‘X’, ‘O’, and the space character. An empty game is all spaces.


X

O

O


X



X

1) write the class outline and a public constructor that initializes gameBoard to an empty game. Make gameBoard be private.

SaveGame and loadGame are public functions that convert gameBoard to and from a 9-digit integer. For the example above, the saved game value is 122313331.

2) write a int saveGame() function that returns a 9-digit integer using 1 for 'X', 2 for 'O', and 3 for space. gameBoard[0][0] should be the high-order digit, followed by [0][1] . . . all the way to [2][2] for the low order digit. Consider using nested loops to access the gameBoard and multiplying by 10 to shift the digits of the saved answer.

3) write a void loadGame(int g) function that unpacks the 9-digit integer from a saved game back into the gameBoard. Consider extracting the saved game digits using %, then dividing by 10. Make sure the gameBoard is recreated in the original order.

Extra Credit: explain why you would choose 0 or 3 to encode the space value.