Tic Tac Toe is a very popular paper-pencil game often played in classrooms on the last page of the notebook. In this game, two players mark X or 0 one by one in a 3x3 grid. To win the game, one has to complete a pair of 3 symbols in a line, and that can be a horizontal line, a vertical line, or a diagonal line.
In this tutorial on ‘Tic Tac Toe game in C++’, you will create a program, in which you will display the structure on the console screen with the help of cout. Once the game structure is created, you will also be able to play the game.
You will create this Tic Tac Toe game in three parts, and for each part, you must create a function.
- The structure of the tic tac toe game
- Assigning the symbol to a position
- Checking if it’s a win or draw
The Structure of the Tic Tac Toe Game
In this part, you will create the basic structure of the Tic Tac Toe game in C++. You know the structure of this game contains two vertical lines and two horizontal lines passing through each other at an angle of 90 degrees and creating nine empty spaces.
So now, you will create this kind of structure using C++ programming.
In the main function, you have made the tic tac toe game structure with the help of cout for printing and used multiple vertical slashes for vertical lines and multiple underscores for horizontal lines.
This is the output of the above code. You have successfully created the structure of the game.
Now you have to add the digits at these empty spaces inside the structure to make use of these spaces for filling up X or 0’s.
So you will need to create a two-dimensional array of 3x3 matrix of character type, and store the elements of the array in these empty spaces.
In the example, you created a character array named space and initialized it with elements from 1 to 9. Then in the structure at specified positions; you have added the elements of the array by writing the array name with its row index and column index.
It adds these elements at all nine empty spaces, as shown in the figure.
This is the output of the above example. You can see that all it now filled the nine empty spaces with array elements.
The code will be quite long, so you will divide the code into functions. You must change the main function to functionOne() and call it from the main function, which you will create in the end.
You have named the function as functionOne().
Now, you must declare some global variables, and declare the array outside the function.
Move to the next step of this game in C++ tutorial.
Assigning the Symbol to a Position
In this part, you will make a function named functionTwo(), and change or assign the values to the positions based on what player enters.
So, as you can see in the above example, you will create a function named functionTwo(). Inside that function, write two if statements stating that if the token's value is x, then player1 will enter the digit or number. Similarly, if the token’s value is 0, then player2 will enter the digit.
Once the player enters the digit, that number needs to be replaced by the symbol.
To replace it, you should use multiple if conditions and an else if condition.
One by one, using the if statements, you must check for each digit from 0 to 9, and whichever matches with the player’s input, that particular block will execute. If the user enters any other value that is less than 1 and more than 9, it will display Invalid.
In line 88, there is an if condition stating that if player1 turn is there and if the position in which the player enters is vacant, there is no x and 0, then x would be assigned to that place.
Similarly, if player2 turn is there, and the position entered by the user is vacant, then 0 would be assigned to that position; else, “There is no empty space” gets displayed, and functionTwo() is called again for the next turn.
In line 102, functionOne() is getting called which will display the structure again.
So far, if you have any doubts regarding the article tic tac toe game in c++, I suggest you drop your question in the comment section below.
Checking if It’s a Win or Draw
In functionThree(), we will check if it’s a win or draw or the game is still going on.
Here, you used a for loop, which is iterating from 0 to 3. Inside the loop, there is an if condition that checks for three pairs of x’s or 0’s both horizontally and vertically. And if there is a pair, it will return true, which means somebody won the game.
Similarly, you must check for diagonal pairs.
After that, you must use two for loops that iterate from 0 to 3 because it is a 3x3 matrix. You should check if there is any space in the structure that doesn’t have x or 0 or, in other words, check if any space is empty.
If any space is not filled or empty, it will return false because it means the game is still going on. If the condition is not satisfied, that means there is no space left and the game is also not won by anyone then it means it’s a tie so that the control will go outside the loops.
Now, create the main function.
Inside the main function, you must take the input of the names of both the players n1 and n2 and also, you have to declare these n1 and n2 strings as global variables.
Inside the while loop, all the three functions are getting called one by one, and there’s a condition that the loop will keep on iterating till functionThree() is returning false. This function will keep on returning false until anyone wins the game among the two players. So these functions will keep on repeating one by one until the game is won.
Inside the if statement there’s a condition stating that if the token’s value is x and tie is false, then n2 or player2 will be the winner. It means the last turn was played by player2; that is why now the value of turn is x, and it is also not a tie as the value of tie is false. So it means player2 wins the game.
Similarly, there’s a condition for token 0, where player1 would be the winner.
At last, if both the conditions are not met, then the game would be a tie.
This is the output of the above code. Sam and Rob are the name of the players, one by one both the players must enter the symbols in the empty spaces as per their choice.
In the output, you can see Sam successfully created the pair of three crosses. So, he won the game.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
After reading this tutorial on the tic tac toe game in C++, you would have understood how to create a structure of the tic tac toe game in C++ and how to assign the symbols to the empty spaces and create a complete tic tac toe game in C++ with the help of some examples.
If you are looking to build a career in software development, check the Post Graduate Program in Full Stack Development by Simplilearn. It can prove to be the best solution to help you build your career in the right way.
Do you have any questions regarding this tutorial on the tic tac toe game in C++? If you do, then please put them in the comments section. We’ll help you solve your queries. To learn more about Games in C++, click on the following link: https://www.youtube.com/watch?v=dv_75WfQ1rA&t=2689s