CPTR 124 Fundamentals of Programming


In this lab you will enhance your Tic-Tac-Toe program from the previous assignment.


Teams

If you wish, you may continue to work with a partner for this assignment.

Game Enhancement

Add the logic to your Tic-Tac-Toe program so that it draws a line across the winning configuration at the end of a game. The figure below illustrates:

Screenshot of Tic-Tac-Toe winning configuration

This likely will involve adding a function named check_status that your mouse press handling function can call to see how the status of the game has changed due to the user's move.

You should add your own custom enumeration type that specifies all the possible game ending board configurations; for example, if your enumeration class type is named GameStatus, the value GameStatus::Win_Northeast_Southeast would indicate the game is over with a win from the right-top corner to the right-bottom corner, as shown in the figure above. You necessarily would have values such as GameStatus::Draw to indicate a tie game (all squares filled but no winner) and GameStatus::Playing representing a game that is not yet complete (empty squares are available but no one has won). Your check_status function would return one of these enumerated type values after consulting the global variables that are keeping track of the marks in the squares.

Once the game is over (that is, game status is anything other than GameStatus::Playing) the user should not be allowed to mark anymore squares, even if empty squares still are available. At this point the user has only two options:

  1. press the Esc key to clear the board and start a new game or
  2. close the window to quit the program.

Show a draw (tie game) by drawing lines everywhere, as shown in the following figure:

Screenshot of Tic-Tac-Toe draw configuration

How to do it

Your new check_status function would contain the conditional logic to examine the values of the global variables you are using to keep track which player's mark is in which square. To check for the win shown in the first figure above, your code would need to compare your northeast variable, east variable, and southeast variable to see if they all hold the same player's mark. Be careful—just because they all are equal does not necessarily mean it is a winning configuration—they all could be equal to Player::None, and that obviously is not a win for either player!

You can add a function named draw_win that draws the line(s) through the proper squares as needed. As an example, if player X has won in the right column as shown in the first figure above, the call

draw_win(check_status());

would draw the line through the Xs in the right column as shown in the figure. As another example, if the call to check_status returns GameStatus::Draw, the draw_win function would draw lines everywhere as shown in the second figure.

Things to do

  1. Add the GameStatus enumerated type. It covers all possible winning configurations, a draw, and still playing.
  2. Add the check_status function. It returns a GameStatus value.
  3. Add the draw_win function. It draws lines as needed when a game is over.
  4. Add a key handler function to check for the Esc keypress. It resets all the global variables in making everything ready for a new game.
  5. Modify your mouse pressed function so it does not allow the user to select any remaining empty squares when the game is over.
  6. Modify your drawing function so that it calls the draw_win function when the game is over.

Check out

Your finished program will be evaluated for correctness and compliance. Once you have been checked out you may submit your code to eclass.e.southern.edu.