#include<iostream.h>
#include<stdio.h>
char A[3][3]={1,2,3,
4,5,6,
7,8,9};
void printmaze();
void makemove(int,int);
void checkmove1(int,int);
void checkmove();
void checkrow();
void checkcol();
void checkdiag1();
void checkdiag2();
void makealtmove();
void result();
int main()
{
int m11,m12,m21,m22,m31,m32,m41,m42,m51,m52;
char ch;
do
{
cout<<"Welcome to tic-tac-toe"<<endl;
printmaze();
cout<<"for choosing 1--> input 0 0, 2-->0 1, 3-->0 2 \n 4-->1 0, 5-->1 1, 6-->1 2 \n 7-->2 0, 8--> 2 1, 9-->2 2\n";
cout<<"Enter space indices";
cin>>m11>>m12;
makemove(m11,m12);
cout<<"Now the computer will make its move\n";
checkmove1(m11,m12);
cout<<"Make your move";
cin>>m21>>m22;
makemove(m21,m22);
checkmove();
cout<<"Make your move";
cin>>m31>>m32;
makemove(m31,m32);
checkmove();
cout<<"Make your move";
cin>>m41>>m42;
makemove(m41,m42);
checkmove();
cout<<"Make your move";
cin>>m51>>m52;
makemove(m51,m52);
checkmove();
cout<<"Analyzing game results...please wait"<<endl;
result();
cout<<"Do you want to play another game?(Y/N)";
ch=getchar();
}while(ch=='Y');
return 0;
}
//printing function
void printmaze()
{
cout << " | | " << endl;
cout << " " << A[0][0] << " | " << A[0][1] << " | " << A[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << A[1][0] << " | " << A[1][1] << " | " << A[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << A[2][0] << " | " << A[2][1] << " | " << A[2][2] << endl;
cout << " | | " << endl << endl;
}
//replacing function for making moves
void makemove(int i, int j)
{ A[i][j]='X';
printmaze();
}
void checkmove1(int i, int j)
{ if(i==1&&j==1)
{A[0][0]='0';
printmaze();}
else
{A[1][1]='0';
printmaze();}
}
void checkmove()
{
cout<<"Now the computer will make its move\n";
checkrow();
}
void checkrow()
{int i,j,nox=0,flag=1;
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{
if(A[i][j]=='X')
nox++;
}
if(nox==2)
{ for(j=0;j<3;j++)
{if((A[i][j]!='X') && (A[i][j]!='0'))
{A[i][j]='0';
printmaze();
flag++;}
}
}
if(flag==1)
checkcol();
}
void checkcol()
{int i,j,nox=0,flag=1;
for(j=0;j<3;j++)
{for(i=0;i<3;i++)
{
if(A[i][j]=='X')
nox++;
}
if(nox==2)
{ for(i=0;i<3;i++)
{if((A[i][j]!='X') && (A[i][j]!='0'))
{A[i][j]='0';
printmaze();
flag++;}
}
}
if(flag==1)
checkdiag1();
}
void checkdiag2()
{int i,j,nox=0,flag=1;
for(i=0,j=2;i<3;i++,j--)
{if(A[i][j]=='X')
nox++;
}
if(nox==2)
{for(i=0,j=2;i<3;i++,j--)
{if( (A[i][j]!='X') && (A[i][j]!='0') )
{ A[i][j]='0';
printmaze();
flag++;
}
}
}
if(flag==1)
makealtmove();
}
void makealtmove()
{int i,j;
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{if( (A[i][j]!='X') && (A[i][j]!='0') )
{A[i][j]='0'
printmaze();}
}
}
}
void result()
{ int i,j,nox,noo,flag=1;
for(i=0;i<3;i++)
{ nox=0;
noo=0;
for(j=0;j<3;j++)
{ if(A[i][j]=='X')
nox++;
if(A[i][j]=='0')
noo++;
}
if(nox==3)
{cout<<"You win!! Congratulations";
flag++;}
if(noo=3)
{cout<<"You lose!! Computer wins";
flag++;}
}
if(flag==1)
{ for(j=0;j<3;j++)
{ nox=0;
noo=0;
for(i=0;i<3;i++)
{ if(A[i][j]=='X')
nox++;
if(A[i][j]=='0')
noo++;
}
if(nox==3)
{cout<<"You win!! Congratulations";
flag++;}
if(noo=3)
{cout<<"You lose!! Computer wins";
flag++;}
}
}
if(flag==1)
{ nox=0;
noo=0;
for(i=0,j=2;i<3;i++,j--)
{ if(A[i][j]=='X')
nox++;
if(A[i][j]=='0')
noo;
}
if(nox==3)
{cout<<"You win!! Congratulations";
flag++;}
if(noo=3)
{cout<<"You lose!! Computer wins";
flag++;}
}
if(flag==1)
{cout<<"The game is a draw between you and computer";}
}
deepeshmathuria
0
Newbie Poster
Recommended Answers
Jump to PostWhat are the errors the compiler is giving you? Al so formating of you code is terrible. Code block should look like either of the following
some line of code; if (some condintion) { some line of code; another line of code; if (another condition) { some …
All 2 Replies
NathanOliver
429
Veteran Poster
Featured Poster
Moschops
683
Practically a Master Poster
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.