Objective:

To review many of the topics from COMP 51.

Description:

For this project you are going to write the code for a console based game of Tic-Tac-Toe in C++. If you do not know how to play Tic-Tac-Toe, the goal is to get three across a row, column, or diagonal.

The game board can be simulated with a two dimensional array. The player chooses a square by selecting its number. Play then alternates between the computer and player. When someone makes a move that wins the game, play stops. If all nine squares have been taken and no one has won - the game is a draw.

Make sure you show the game board after the final play. For simplicity, have the player go first and always be "X".

Sample Output:

A sample output of a program that plays the game is shown here (with user input in bold):

0 | 1 | 2
-----------
3 | 4 | 5
-----------
6 | 7 | 8
Enter the number of the square where you want to play: 4
0 | 1 | 2
-----------
3 | X | 5
-----------
6 | 7 | 8
Computer's turn
0 | 1 | 2
-----------
3 | X | 5
-----------
6 | O | 8
Enter the number of the square where you want to play: 3
0 | 1 | 2
-----------
X | X | 5
-----------
6 | O | 8
Computer's turn
0 | O | 2
-----------
X | X | 5
-----------
6 | O | 8
Enter the number of the square where you want to play: 5
0 | O | 2
-----------
X | X | X
-----------
6 | O | 8
X wins!

Requirements:

Since the purpose of this assignment is to help you review some of the fundamental topics from your first programming course that you will need to be successful in this course, you may NOT use classes to complete this assignment. The game should also be player between a human player and the computer player. Significant points will be deducted from submissions that implement a two human player game.

Your program should have some error checking. The user should not be allowed to enter illegal values. If an illegal value is entered, an appropriate message should be given, and the user re-prompted. The player obviously should not be able to play in a square that has already been taken either.

You should use appropriate commenting and indentation in your code, as discussed in your previous programming course. Make sure your name and a description of the program are in comments at the top of your code. If this is not done, expect significant points to be deducted from your score.

You should have at least two functions (in addition to main) in this program: one to print the game board and one to check if someone has won the game. Feel free to have more functions if you would like, but you must have at least these two. Furthermore, you are not allowed to use GLOBAL VARIABLES . You need to pass parameters to these functions.

Just for fun:

Since the objective of the assignment is to help you get caught up on what you need to know about C++ to be successful in COMP 53, you do not have to add any features to your program past what is described above. However, if you want, there are lots of things you can do to improve the game. For instance, the computer could play with some intelligence, i.e. have the computer attempt to make plays in this order:

1.Play in a square that wins the game
2.Play in a square that blocks the player from winning the game
3.Play in the middle square
4.Play in a corner
5.Play in any other available position

You can also randomly select who goes first. In the game I've described above, the player always goes first. However, if you are interested, you can randomly select between the computer and player who gets the first move. The one who plays first should always be "X".

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.