Tic Tac Toe game in C++
- Overview
In this article, you will learn how to make a simple Tic Tac Toe game in C++ using the 2D array. Making a game like Tic Tac Toe will boost up your skills in c++ programming. You can use data structure for making the Tic Tac Toe game in c++.
But Using a 2D array is the best choice for this game.
Making a game like Tic-Toe-Toe in c++ is a very basic level program but not that easy to understand. so, you can't expect an easy set of codes from anyone.
You can use any c++ compiler to execute this game but the best choice will be Turbo C++. And Tic Tac Toe game is the best project for learning data structure and algorithms.
You will need some basic understanding of C++ concepts like:
- Loops
- datatypes
- Array
- Classes and objects
- Functions
- Conditions like if-else
- Scope operator(::) , etc.
- Code explanation
So in the code of the Tic Tac Toe game, There is a class named "cls1". In the cls1 class, there are two methods which are void fill_array(char choice) and void disp().
So as above Image, the position for arrays like no 1 is [0][0],no2 is [0][1] and no3 is [0][2] _ _ so if the user enters any number so that position will be filled in the array by 'O' or 'X'. So for that purpose, we are using the fill_array(char choice) method.
- Code
#include<iostream>
using namespace std;
class cls1{
public:
char arr[3][3];
int position_no;
void fill_array(char choice)
{
if(position_no==1){ arr[0][0]=choice; }
if(position_no==2){ arr[0][1]=choice; }
if(position_no==3){ arr[0][2]=choice; }
if(position_no==4){ arr[1][0]=choice; }
if(position_no==5){ arr[1][1]=choice; }
if(position_no==6){ arr[1][2]=choice; }
if(position_no==7){ arr[2][0]=choice; }
if(position_no==8){ arr[2][1]=choice; }
if(position_no==9){ arr[2][2]=choice; }
}
void disp();
};
void cls1::disp()
{
cout<<"#################### TIC TAC TOE ####################"<<endl<<endl;
cout<<"EXAMPLE POSITION NUMBER :\n\n";
cout<<"1 | 2 | 3\n";
cout<<"_ _ _ _ _\n\n";
cout<<"4 | 5 | 6\n";
cout<<"_ _ _ _ _\n\n";
cout<<"7 | 8 | 9\n\n";
for(int i=0;i<4;i++)
{
cout<<"ENTER POSITION NUMBER PLAYER1 : ";cin>>position_no;
fill_array('X');
cout<<"ENTER POSITION NUMBER PLAYER2 : ";cin>>position_no;
fill_array('O');cout<<endl;
}
cout<<"ENTER POSITION NUMBER PLAYER1 : ";cin>>position_no;
fill_array('X');
//result
cout<<"\nRESULT :\n\n";
cout<<arr[0][0]<<" | "<<arr[0][1]<<" | "<<arr[0][2]<<endl;
cout<<"_ _ _ _ _\n\n";
cout<<arr[1][0]<<" | "<<arr[1][1]<<" | "<<arr[1][2]<<endl;
cout<<"_ _ _ _ _\n\n";
cout<<arr[2][0]<<" | "<<arr[2][1]<<" | "<<arr[2][2]<<endl;
//winner
if(arr[0][0]=='X' && arr[0][1]=='X' && arr[0][2]=='X')
{cout<<"\n\nPLAYER 1 IS WINNER";}
else if(arr[0][0]=='O' && arr[0][1]=='O' && arr[0][2]=='O')
{cout<<"\n\nPLAYER 2 IS WINNER";}
if(arr[1][0]=='X' && arr[1][1]=='X' && arr[1][2]=='X')
{cout<<"\n\nPLAYER 1 IS WINNER";}
else if(arr[1][0]=='O' && arr[1][1]=='O' && arr[1][2]=='O')
{cout<<"\n\nPLAYER 2 IS WINNER";}
if(arr[2][0]=='X' && arr[2][1]=='X' && arr[2][2]=='X')
{cout<<"\n\nPLAYER 1 IS WINNER";}
else if(arr[2][0]=='O' && arr[2][1]=='O' && arr[2][2]=='O')
{cout<<"\n\nPLAYER 2 IS WINNER";}
if(arr[0][0]=='X' && arr[1][0]=='X' && arr[2][0]=='X')
{cout<<"\n\nPLAYER 1 IS WINNER";}
else if(arr[0][0]=='O' && arr[1][0]=='O' && arr[2][0]=='O')
{cout<<"\n\nPLAYER 2 IS WINNER";}
if(arr[0][1]=='X' && arr[1][1]=='X' && arr[2][1]=='X')
{cout<<"\n\nPLAYER 1 IS WINNER";}
else if(arr[0][1]=='O' && arr[1][1]=='O' && arr[2][1]=='O')
{cout<<"\n\nPLAYER 2 IS WINNER";}
if(arr[0][2]=='X' && arr[1][2]=='X' && arr[2][2]=='X')
{cout<<"\n\nPLAYER 1 IS WINNER";}
else if(arr[0][2]=='O' && arr[1][2]=='O' && arr[2][2]=='O')
{cout<<"\n\nPLAYER 2 IS WINNER";}
if(arr[0][0]=='X' && arr[1][1]=='X' && arr[2][2]=='X')
{cout<<"\n\nPLAYER 1 IS WINNER";}
else if(arr[0][0]=='O' && arr[1][1]=='O' && arr[2][2]=='O')
{cout<<"\n\nPLAYER 2 IS WINNER";}
if(arr[0][2]=='X' && arr[1][1]=='X' && arr[2][0]=='X')
{cout<<"\n\nPLAYER 1 IS WINNER";}
else if(arr[0][2]=='O' && arr[1][1]=='O' && arr[2][0]=='O')
{cout<<"\n\nPLAYER 2 IS WINNER";}
}
int main()
{
cls1 s1;
s1.disp();
return 0;
}
- Output
So in this way you can create a basic Tic Tac Toe game in C++ using 2D Array.
Leave a comment if you had any queries regarding this post and I will reply as soon as possible. Anyway, if you enjoy reading my content, you can subscribe to my blog to receive all the latest updates. Happy Learning!
Share this article with your friends✌!!




0 Comments