The C++ Tic Tac Toe game in c++ project is a timeless classic, beloved by people of all ages learn how to Create Tic Tac Toe Game in C++ Programming. In game development, our detailed guide on creating a Tic Tac Toe game in C++ project is perfect for beginners and intermediate programmers.
This guide will take you through creating a C++ Tic Tac Toe game, covering everything from the initial setup to the final execution.
Setting Up Your Development Environment
Before diving into the coding part, it’s essential to have a proper development environment set up. This involves installing a C++ compiler and an IDE (Integrated Development Environment) of your choice.
Code Structure
The heart of our Tic Tac Toe game lies within its code structure, which comprises several key components. The main function initiates the game by setting up the board and welcoming the players. Global variables and arrays are used to store player names and the game board’s state.
int main()
{
char Ttt[3][3]{
{'-', '-', '-'},
{'-', '-', '-'},
{'-', '-', '-'}
};
string Plyr1;
string Plyr2;
cout << "\t\t\t\t *** TIC - TAC - TOE ***\n\n";
cout << "\t\t\t\t Welcome to our Game\n\n";
// Player information
cout << "Enter player 1 name : ";
getline(cin, Plyr1);
cout << "Enter player 2 name : ";
getline(cin, Plyr2);
// Input Location format
cout << "\nChoose following format to enter input\n\n";
cout << " 1 | 2 | 3 " << endl;
cout << " 4 | 5 | 6 " << endl;
cout << " 7 | 8 | 9 " << endl << endl;
// Input
string Winner = PlayerInput(Ttt, &Plyr1, &Plyr2);
// output
Display(&Winner, Plyr1, Plyr2);
}
Player Input Function
The PlayerInput function is crucial for the game’s interactivity. It allows players to enter their moves, which are then validated to ensure they fall within the acceptable range and aren’t repeating a previously made move. This function alternates between player 1 and player 2, updating the game board accordingly.
Decoding the Winner Function
Determining the winner is the core objective of the game. The Winner function checks the board’s state after each move to see if there’s a winning combination. It handles both scenarios where there’s a winner or the game ends in a draw, displaying the appropriate message.
Note
Download the Zip File Directly By Click on the Download Button.
You can also get the GitHub repository of tic tac toe game in C++ project.
Tic Tac Toe Game Logic
The game’s logic is encapsulated within the main loop, where functions are called in sequence to play out the game. This involves taking player inputs, updating the game board, checking for a winner, and displaying the current state.
The Display Function
Visual feedback is provided through the Display function, which prints the current state of the game board to the console. It also announces the game’s result, whether it’s a win for either player or a draw, adding to the game’s dynamic nature.

Customizing the Game
Once you’ve got the basics down, there’s room for customization. You might want to tweak the game to allow for player names to be inputted or to add an AI opponent for a single-player mode. The possibilities are endless.
Advanced Concepts in C++ Tic Tac Toe Game Project
Encountering bugs and issues is part of the programming journey. This section offers debugging tips and solutions to common problems you might face while developing the game. But this project introduces several advanced C++ concepts, such as arrays for storing the game board, loops for iterating through player moves, and functions for organizing the code logically.
conclusion
Creating a C++ Tic Tac Toe game project from scratch is an excellent way for programmers to practice and showcase their skills. It offers a comprehensive understanding of basic programming concepts, the use of functions, and conditional logic. As you advance, remember that the world of C++ game development is vast and full of opportunities to explore and innovate.
FAQ
How do I start with C++ game development?
Begin with simple projects like this Tic Tac Toe game to grasp the basics, then gradually move on to more complex games.
What are some common errors to watch out for?
Syntax errors, logical errors in the win condition checks, and not properly validating player inputs are common pitfalls.
How can I make the game graphical? Explore
Explore using libraries such as wxWidgets, SFML, or SDL, which allow for creating windowed applications with graphics.
Is it necessary to use OOP for this project?
While not necessary, using Object-Oriented Programming can help organize your code better, especially for more complex projects.
Source Code of C++ tic tac toe game project
#include <iostream>
#include <string>
using namespace std;
string PlayerInput(char Ttt[][3], string*, string*);
string Winner(char Ttt[][3], string, string);
void Display(string*, string, string);
int main()
{
char Ttt[3][3]{
{'-', '-', '-'},
{'-', '-', '-'},
{'-', '-', '-'}
};
string Plyr1;
string Plyr2;
cout << "\t\t\t\t *** TIC - TAC - TOE ***\n\n";
cout << "\t\t\t\t Welcome to our Game\n\n";
// Player information
cout << "Enter player 1 name : ";
getline(cin, Plyr1);
cout << "Enter player 2 name : ";
getline(cin, Plyr2);
// Input Location format
cout << "\nChoose following format to enter input\n\n";
cout << " 1 | 2 | 3 " << endl;
cout << " 4 | 5 | 6 " << endl;
cout << " 7 | 8 | 9 " << endl << endl;
// Input
string Winner = PlayerInput(Ttt, &Plyr1, &Plyr2);
// output
Display(&Winner, Plyr1, Plyr2);
}string PlayerInput(char Ttt[][3], string* P1, string* P2)
{
string p1;
string p2;
char ReptValu[10];
string Win;
for (int i = 0; i < 5; i++)
{
// Player 1 Input
cout << *P1 << " turn : ";
do
{
cin >> p1;
if (p1.length() == 1 && p1[0] < 58 && p1[0] > 48 && p1[0] != ReptValu[0] && p1[0] != ReptValu[1]
&& p1[0] != ReptValu[2] && p1[0] != ReptValu[3] && p1[0] != ReptValu[4] && p1[0] != ReptValu[5]
&& p1[0] != ReptValu[6] && p1[0] != ReptValu[7] && p1[0] != ReptValu[8] && p1[0] != ReptValu[9])
{
cout << endl;
break;
}
else if (p1[0] == ReptValu[0] || p1[0] == ReptValu[1] || p1[0] == ReptValu[2] || p1[0] == ReptValu[3]
|| p1[0] == ReptValu[4] || p1[0] == ReptValu[5] || p1[0] == ReptValu[6] || p1[0] == ReptValu[7]
|| p1[0] == ReptValu[8] || p1[0] == ReptValu[9])
{
cout << "\n * *This position is occupied.Please choose an another position. * *\n\n";
cout << *P1 << " turn again : ";
}
else
{
cout << "\nInvalid input.\nPlease enter a natural number from the range 1 to 9.\n\n";
cout << *P1 << " turn again : ";
}
} while (true);
// Storing Player 1 Input in Matrix
if (p1[0] == '1') { Ttt[0][0] = 'X'; }
if (p1[0] == '2') { Ttt[0][1] = 'X'; }
if (p1[0] == '3') { Ttt[0][2] = 'X'; }
if (p1[0] == '4') { Ttt[1][0] = 'X'; }
if (p1[0] == '5') { Ttt[1][1] = 'X'; }
if (p1[0] == '6') { Ttt[1][2] = 'X'; }
if (p1[0] == '7') { Ttt[2][0] = 'X'; }
if (p1[0] == '8') { Ttt[2][1] = 'X'; }
if (p1[0] == '9') { Ttt[2][2] = 'X'; }
// Checking winner if there is.
Win = Winner(Ttt, *P1, *P2);
// showing user input in matrix form
cout << " " << Ttt[0][0] << " | " << Ttt[0][1] << " | " << Ttt[0][2];
cout << "\t\t 1 | 2 | 3 " << endl;
cout << " " << Ttt[1][0] << " | " << Ttt[1][1] << " | " << Ttt[1][2] ;
cout << "\t\t 4 | 5 | 6 " << endl;
cout << " " << Ttt[2][0] << " | " << Ttt[2][1] << " | " << Ttt[2][2] ;
cout << "\t\t 7 | 8 | 9 " << endl << endl;
if (Win == *P1 || Win == *P2) break; // End the game if there's a winner
else if (i == 4) { Win = "Draw"; break; }// End the game if game is draw
ReptValu[i] = p1[0];
// Player 2 Input
cout << *P2 << " turn : ";
do
{
cin >> p2;
if (p2.length() == 1 && p2[0] > 48 && p2[0] < 58 && p2[0] != p1[0] && p2[0] != ReptValu[0] &&
p2[0] != ReptValu[1] && p2[0] != ReptValu[2] && p2[0] != ReptValu[3] && p2[0] != ReptValu[4]
&& p2[0] != ReptValu[5] && p2[0] != ReptValu[6] && p2[0] != ReptValu[7] && p2[0] != ReptValu[8]
&& p2[0] != ReptValu[9])
{
cout << endl;
break;
}
else if (p2[0] == ReptValu[0] || p2[0] == ReptValu[1] || p2[0] == ReptValu[2] || p2[0] == ReptValu[3]
|| p2[0] == ReptValu[4] || p2[0] == ReptValu[5] || p2[0] == ReptValu[6] || p2[0] == ReptValu[7]
|| p2[0] == ReptValu[8] || p2[0] == ReptValu[9])
{
cout << "\n * *This position is occupied.Please choose an another position. * *\n\n";
cout << *P2 << " turn again : ";
}
else
{
cout << "\nInvalid input.\nPlease enter a natural number from the range 1 to 9.\n\n";
cout << *P2 << " turn again : ";
}
} while (true);
// Storing Player 2 Input in Matrix
if (p2[0] == '1') { Ttt[0][0] = 'O'; }
if (p2[0] == '2') { Ttt[0][1] = 'O'; }
if (p2[0] == '3') { Ttt[0][2] = 'O'; }
if (p2[0] == '4') { Ttt[1][0] = 'O'; }
if (p2[0] == '5') { Ttt[1][1] = 'O'; }
if (p2[0] == '6') { Ttt[1][2] = 'O'; }
if (p2[0] == '7') { Ttt[2][0] = 'O'; }
if (p2[0] == '8') { Ttt[2][1] = 'O'; }
if (p2[0] == '9') { Ttt[2][2] = 'O'; }
// Checking winner if there is.
Win = Winner(Ttt, *P1, *P2);
// showing user input in matrix form
cout << " " << Ttt[0][0] << " | " << Ttt[0][1] << " | " << Ttt[0][2];
cout << "\t\t 1 | 2 | 3 " << endl;
cout << " " << Ttt[1][0] << " | " << Ttt[1][1] << " | " << Ttt[1][2];
cout << "\t\t 4 | 5 | 6 " << endl;
cout << " " << Ttt[2][0] << " | " << Ttt[2][1] << " | " << Ttt[2][2];
cout << "\t\t 7 | 8 | 9 " << endl << endl;
if (Win == *P1 || Win == *P2) break; // End the game if there's a winner
// storing value in array for not repeating same input by using in if statement
ReptValu[i + 5] = p2[0];
}
return Win;
}
string Winner(char Ttt[][3], string P1, string P2)
{
string Winner = " ";
// condition for player 1 as winner
if ((Ttt[0][0] == 'X' && Ttt[0][1] == 'X' && Ttt[0][2] == 'X') ||
(Ttt[1][0] == 'X' && Ttt[1][1] == 'X' && Ttt[1][2] == 'X') ||
(Ttt[2][0] == 'X' && Ttt[2][1] == 'X' && Ttt[2][2] == 'X') ||
(Ttt[0][0] == 'X' && Ttt[1][0] == 'X' && Ttt[2][0] == 'X') ||
(Ttt[0][1] == 'X' && Ttt[1][1] == 'X' && Ttt[2][1] == 'X') ||
(Ttt[0][2] == 'X' && Ttt[1][2] == 'X' && Ttt[2][2] == 'X') ||
(Ttt[0][0] == 'X' && Ttt[1][1] == 'X' && Ttt[2][2] == 'X') ||
(Ttt[0][2] == 'X' && Ttt[1][1] == 'X' && Ttt[2][0] == 'X'))
{
Winner = P1;
}
// condition for player 2 as winner
if ((Ttt[0][0] == 'O' && Ttt[0][1] == 'O' && Ttt[0][2] == 'O') ||
(Ttt[1][0] == 'O' && Ttt[1][1] == 'O' && Ttt[1][2] == 'O') ||
(Ttt[2][0] == 'O' && Ttt[2][1] == 'O' && Ttt[2][2] == 'O') ||
(Ttt[0][0] == 'O' && Ttt[1][0] == 'O' && Ttt[2][0] == 'O') ||
(Ttt[0][1] == 'O' && Ttt[1][1] == 'O' && Ttt[2][1] == 'O') ||
(Ttt[0][2] == 'O' && Ttt[1][2] == 'O' && Ttt[2][2] == 'O') ||
(Ttt[0][0] == 'O' && Ttt[1][1] == 'O' && Ttt[2][2] == 'O') ||
(Ttt[0][2] == 'O' && Ttt[1][1] == 'O' && Ttt[2][0] == 'O'))
{
Winner = P2;
}
return Winner;
}
void Display(string* W, string P1, string P2)
{
// showing result if there is winner
if (*W == P1 || *W == P2)
{
cout << "\n\n\t\t\t\t\t Winner of Game is : " << *W << endl << endl;
}
// showing result if Game is Draw
else cout << "\t\t\t\t\t\t\tGame is Draw \n";
cout << "\t\t\t\t\t *** Thank You for using our Game Program ***\n";
cout << "\t\t\t\t_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \n\n";
}