HOMEWORK 09 - Tic Tac Toe

DUE: Wednesday, Nov 4th, 8am

Tic Tac Toe Game

For this assignment, you will use a 2D array to repesent a game of Tic Tac Toe. You will be able to play against the computer through the magic of SFML! The locations on the game board will mimic the number pad on your keyboard. The game board positions will look like:

   |   |
 7 | 8 | 9
   |   |
-----------
   |   |
 4 | 5 | 6
   |   |
-----------
   |   |
 1 | 2 | 3
   |   |

The user will be able to choose which position to place their 'X' and then the computer will place an 'O'. The game continues until there is a winner or there's a draw.

Instructions

To get started, first download the SFML Template and extract the contents of the zip. If needed, see Lab07C for details on extracting the contents of the provided zip file (specifically in the "Before You Begin" and "Open and Run the Project" sections). Next, replace the main.cpp provided in the SFML template with this main.cpp.

Part I: Create the Game Board

You will represent the tic tac toe game board using a 2D character array. This 2D array will represent the current state of the game keeping tracking of where each player has played. Drawing the game board using SFML has been provided for you. Initialize each position in the array to 'b' to denote the space as being blank. Later on when a player plays, the array element will be marked with an 'x' or 'o' where each player has played.

Part II: Placing the Pieces on the Game Board

Next, you must visualize the game board. Using the provided functions, DrawO() and DrawX(), loop through each array position and draw an O or X in the appropriate locations. The first time you test this section, no pieces will be placed. Once you complete Part Step 4 in Part III, you should start seeing pieces on the game board.

Part III: Playing the Game

Now for the guts of the program. You will need to convert the following pseudocode into actual code so your game is playable. The keyboard handling event was already provided for you and the result of what key was pressed is stored in the variable keyCode. The game play is rather simple and follows this pseudocode:

  1. Check if the key pressed is any of the 1-9 keys.
  2. Convert the key pressed to 2D array location (row & column).
  3. Check if this is a valid location to play to.
  4. Place an 'x' in this location if valid. If not valid, alert the user to select a new location.
  5. Otherwise, check if the game is over after user played.
  6. If game is not over, have the computer play using the provided ComputerMove() function.
  7. Check if the game is over after computer played. If game is not over, return to Step 1.
  8. Otherwise, if game is over alert user who won the game.

There are two functions you MUST implement. It is up to you to define any other functions you might need. The first function you must implement is called IsGameOver(). Its function prototype is below.

/*
* Check if the game is over, and if it is over how did the game end?
* This function can return four different values representing the
* current game state:
* 0 - game is not over
* 1 - game is over, X won
* 2 - game is over, O won
* 3 - game is over, draw
*
* Parameters:
* char board[3][3] : 2D character array representing Tic-Tac-Toe board
* Returns:
* int : integer representing game state
*/
int IsGameOver( char board[3][3] );

This function checks the current state of the game and returns if the game is over or if the game can continue to be played. There are four different values that can possibly be returned by this function and each denotes a different state the game could be in. The function returns 0 if the game is not over. The function returns 1 if X has won the game (by having three in a row along a row, column, or diagonal). The function returns 2 if O has won the game. Lastly, the function returns 3 if the game is a draw.

The second function you must implement is called IsMoveValid(). Its function prototype is below.

/*
* Check if the given move is a valid move. That is, at the
* target row and column position, is the game board blank?
*
* Parameters:
* char board[3][3] : 2D character array representing Tic-Tac-Toe board
* int row : matrix row location
* int col : matrix column location
* Returns:
* bool : True if valid move, False if invalid
*/

This functions checks if where the user wants to play is blank. If the user can go there, it returns true. Otherwise, it returns false.

Part III Hints

Part IV: Print the Winner

Finally, print a message to the terminal using cout alerting the user to how the game ended (whether X or O won, or if it was a draw).

Part V: Play the Game!

And there you have it, a working Tic-Tac-Toe game! It may be easy to beat the computer every time, but try forcing a draw or allowing the computer to win.

Homework Grade and Submission

Your Homework 09 grade will be based on your solution to this assignment and your solution to Lab09A and Lab09B. Two points of extra credit are also possible if you submit Lab09A2. In submitting your homework assignment (due Nov 4th), follow these steps: