CPTR 124 Fundamentals of Programming


In this lab you will rewrite your graphical Tic-Tac-Toe game so that it is object oriented.


What to do

For this assignment you must rewrite your Tic-Tac-Toe code from an earlier assignment so that it eliminates all global variables and free functions. You will wrap your existing code within a class named TicTacToeBoard.

Create a new project

Create a new project and add the following three new files:

Your original global variables become instance variables in the class, and the original free functions become methods.

As before, we identify the areas of the board by the Square enumeration types (Square::Northwest, Square::North, etc.) as shown in the following table:

Square

Meaning

Role

Square::Northwest

Top-left square

Board location

Square::North

Top-middle square

Board location

Square::NorthEast

Top-right square

Board location

Square::West

Left-middle square

Board location

Square::Center

Center square

Board location

Square::East

Right-middle square

Board location

Square::SouthWest

Bottom-left square

Board location

Square::South

Bottom-middle square

Board location

Square::SouthEast

Bottom-right square

Board location

You should use nine instance variables to keep track of the mark corresponding to each of these locations (I called mine nw, n, etc.). Each one of these variables holds a Player value. They all start out with the value Player::None to indicate at the beginning of the game no square holds any marks.

You should use an instance variable to keep track of whose turn it is (I called mine current_player). This variable holds a Player value as well—Player::X or Player::O. In Tic-Tac-Toe X always has the first move.

These Square and Player types remain global, so they are assessable as is to client code.

Your TicTacToeBoard class should enforce player alternating turns and prevent a player from placing a mark over an existing mark. Your code also should detect a win or a draw. If a move results in a win for one of the players, indicate the winning configuration by drawing a line as shown here:

Screenshot of Tic-Tac-Toe win for player X

When the game results in a draw, draw lines everywhere, as shown here:

Screenshot of Tic-Tac-Toe tie game

When a win or draw occurs the game is over, and no more moves are possible.

Your TicTacToeBoard class must contain the methods that appear in the skeletal code:

You should not touch the main function.

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.