2 Player Tic Tac Toe Game in C++ Posted by Samath Last Updated: January 06, 2021 25742

Tic tac toe is a game for two players who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a diagonal, horizontal, or vertical row is the winner. This is a 2 player tic tac toe game using the C++ programming language. ```#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
char sq = {'o','1','2','3','4','5','6','7','8','9'};
int player = 1;
int choice;
int win = -1;
do
{

cout<<"\tTic Toc Toe Game"<<endl<<endl;
cout<<"Player 1 (X)  ||  Player 2 (0)"<<endl;

cout << endl;
cout << "     |     |     " << endl;
cout << "  " << sq << "  |  " << sq << "  |  " << sq << endl;
cout << "_____|_____|_____" << endl;
cout << "     |     |     " << endl;
cout << "  " << sq << "  |  " << sq << "  |  " << sq << endl;
cout << "_____|_____|_____" << endl;
cout << "     |     |     " << endl;
cout << "  " << sq << "  |  " << sq << "  |  " << sq << endl;
cout << "     |     |     " << endl << endl;

cout<<"(Player "<<player<<") Enter a number: ";
cin>>choice;

if(player == 1)
{
switch(choice)
{
case 1:
if(sq=='X'||sq=='O')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'X';
player = 2;
}
break;

case 2:
if(sq=='X'||sq=='O')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'X';
player = 2;
}
break;

case 3:
if(sq=='X'||sq=='O')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'X';
player = 2;
}
break;
case 4:
if(sq=='X'||sq=='O')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'X';
player = 2;
}
break;
case 5:
if(sq=='X'||sq=='O')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'X';
player = 2;
}
break;
case 6:
if(sq=='X'||sq=='O')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'X';
player = 2;
}
break;
case 7:
if(sq=='X'||sq=='O')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'X';
player = 2;
}
break;
case 8:
if(sq=='X'||sq=='O')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'X';
player = 2;
}
break;
case 9:
if(sq=='X'||sq=='O')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'X';
player = 2;
}
break;

default:cout<<"Invalid Move"<<endl;
system("pause");
break;
}
}
else
{
switch(choice)
{
case 1:
if(sq=='O'||sq=='X')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'O';
player = 1;
}
break;

case 2:
if(sq=='O'||sq=='X')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'O';
player = 1;
}
break;

case 3:
if(sq=='O'||sq=='X')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'O';
player = 1;
}
break;
case 4:
if(sq=='O'||sq=='X')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'O';
player = 1;
}
break;
case 5:
if(sq=='O'||sq=='X')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'O';
player = 1;
}
break;
case 6:
if(sq=='O'||sq=='X')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'O';
player = 1;
}
break;
case 7:
if(sq=='O'||sq=='X')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'O';
player = 1;
}
break;
case 8:
if(sq=='O'||sq=='X')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'O';
player = 1;
}
break;
case 9:
if(sq=='O'||sq=='X')
{
cout<<"Invalid Move"<<endl;
system("pause");
}
else
{
sq = 'O';
player = 1;
}
break;

default:cout<<"Invalid Move"<<endl;
system("pause");
break;
}
}

if (sq == sq && sq == sq)
win = 1;
else if (sq == sq && sq == sq)
win = 1;
else if (sq == sq && sq == sq)
win = 1;
else if (sq == sq && sq == sq)
win = 1;
else if (sq == sq && sq == sq)
win = 1;
else if (sq == sq && sq == sq)
win = 1;
else if (sq == sq && sq == sq)
win = 1;
else if (sq == sq && sq == sq)
win = 1;
else if (sq != '1' && sq != '2' && sq != '3' && sq != '4' &&
sq != '5' && sq != '6' && sq != '7' && sq != '8' && sq != '9')
win = 0;
else
win = -1;

system("cls");
}
while(win == -1);

cout<<"\tTic Toc Toe Game"<<endl<<endl;
cout<<"Player 1 (X)  ||  Player 2 (0)"<<endl;

cout << endl;
cout << "     |     |     " << endl;
cout << "  " << sq << "  |  " << sq << "  |  " << sq << endl;
cout << "_____|_____|_____" << endl;
cout << "     |     |     " << endl;
cout << "  " << sq << "  |  " << sq << "  |  " << sq << endl;
cout << "_____|_____|_____" << endl;
cout << "     |     |     " << endl;
cout << "  " << sq << "  |  " << sq << "  |  " << sq << endl;
cout << "     |     |     " << endl << endl;

if(win==1)
{
if(player == 1)
{
player = 2;
}
else if(player == 2)
{
player = 1;
}
cout<<"\aPlayer "<<player<<" win "<<endl;
}
else
cout<<"\aGame draw"<<endl;

return 0;
}``` man u workin hard... u must be playin hard 2. lik u work bro! Thanks Are you willing to explain the code bits by bits? I'm new to C++ and I want to understand the code better. Thanks in advance. but if when nobody win, how much i write this code?