CPTR 124 Fundamentals of Programming
In this lab you will write functions that control the logic of a graphical Tic-Tac-Toe game.
- Teams
You are welcome to work with a partner on this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time. You may work by yourself if you wish.
- What to do
For a background on the Tic-Tac-Toe game, see http://en.wikipedia.org/wiki/Tic-tac-toe. Your task is to provide the controlling logic for a graphical two-player Tic-Tac-Toe computer game. A game in progress might look like
Since it is a two-player game, your program does not play against a human opponent but simply allows the user(s) to interact with the board, making marks with the mouse.
Your program will consist of multiple C++ source files. The logic of the program is divided, so one file (tictactoe.cpp) is responsible for the graphical presentation, and another file (ttt_logic.cpp) is responsible for making the game work properly. The ttt_logic.h header file contains function prototypes and enumerated types used by the code in ttt_logic.cpp. Your job is to write a ttt_logic.cpp file that implements the functions declared in ttt_logic.h. All the code to make the graphical interface work is provided for you, so your job is to program the rules of the game. For examples, your code must:
- ignore illegal moves like trying to make a mark over an existing mark.
- control whose turn it is
- detect when the game is over.
Study the comments in the ttt_logic.h header file. The header file contains the definitions of several enumerated types and the prototypes for five functions:
- The enumerated type Player represents
the players (X and O) or their marks on the board.
- The enumerated type Location represents
the positions on a Tic-Tac-Toe board.
- The enumerated type Status represents
the current status of the game.
- The check_status function
determines if one player has
won, the game is a draw, or if the game can continue.
It returns a value of type Status that
the graphical system can use to
render the board properly; for example, if a player has
three marks in a line from the northwest corner of the board
to the southeast corner (see the figure below),
the check_status
function would return the value WIN_NW_SE to
the graphical system which in turn would visualize the win as
- The move function puts the mark of a player in a given
square, if possible. The exact mark depends on
the player with the current turn. A mark may be placed
in a square only if the square is unoccupied.
- The look function
returns the mark in a particular square
or a result indicating the square is empty. The graphical
system uses the look function to determine
what, if anything, to draw in a given square.
- The clear_board function clears all marks
off the board
making it ready for a new game. It also makes player X
the current player. The function should reset any
data the program may be using to monitor or control
the progress of a game.
- The current_player function
returns the player whose turn it
is. The graphical system can call this function to
provide feedback to the user indicating whose turn it is;
for example, the cursor may
assume an "X" shape during player X's turn and an "O"
shape during player O's turn.
- The set_player function
allows client code to set the
current player. In the graphical interface, users
can type the X or O keys to change the turn
order. This feature is not used in normal Tic-Tac-Toe
games where players simply alternate turns. It can be used
in contests similar to "Hollywood Squares," where a player
can take a turn only if he or she answers a question correctly.
An incorrect answer results in a lost turn.
Think about it: If you can clear the board, put marks in specified squares, see what is in a given square, and check to see if the game is over or should continue, you have all the pieces necessary to model the control logic for a two-player Tic-Tac-Toe game.
- Code Organization
Add tictactoe.cpp and ttt_logic.h to your project. Create a file named ttt_logic.cpp. In that file implement the functions declared in ttt_logic.h. The comments in ttt_logic.h provide additional detail about what the functions do.
In addition to implementing the functions specified in the header file, you will need to add global variables that maintain the state of the game. You should use these variables to store the marks in each location on the board (this means you will need nine variables to keep track of the nine locations on the board) and to keep track of whose turn it is. You may want to use a variable to keep track of the number of moves made in the current game; once both players together have made nine legal moves and no winner has been detected the game must be over with a tie. These global variables that you use are essential for the functions described above to work correctly.
Important!. You should not modify the files ttt_logic.h and tictactoe.cpp; they must be used as is. Also, your ttt_logic.cpp file should contain NO input or output statements; that is, neither cout nor cin should appear in your code. It is fine to use cout statements during development to help debug your code, but you should remove them when your work is complete.
- Testing
When you are finished, thoroughly play with your Tic-Tac-Toe game, trying all combinations moves, to ensure that your logic is correct.
- Check out
Your finished program will be evaluated for correctness and compliance. Double check to ensure the following:
- that your ttt_logic.cpp file
contains no cout or
cin statements,
- that your ttt_logic.cpp file builds
properly with the
unmodified ttt_logic.h and
tictactoe.cpp files, and
- that two players can play the graphical Tic-Tac-Toe and things work as they would in the paper version.
Follow the special instructions given during lab for the evaluation component. Your ttt_logic.cpp file will be subjected to a thorough testing procedure to gauge its correctness. You will have up to three attempts to produce a correctly working ttt_logic.cpp file. Once you think your code is working, make sure you have checked these common problem areas:
- A tie game shades all the squares on the board. If your board does not do this, your status function likely has a problem.
- Player X always has the first turn. If player X wins a game, player O should not have the first move in the following game.
- Be sure the players alternate turns properly, X, O, X, O, etc.
- Do not allow a player to mark a square already taken.
When approved, you should submit your ttt_logic.cpp C++ source file to eclass.e.southern.edu. Be sure your name (and your partner's name, if necessary) are included in comments at the top of each source file.
- that your ttt_logic.cpp file
contains no cout or
cin statements,