WHAT'S NEW?
Loading...

Code for the game tic-tac -toe





// tic-tac-toe


#include<iostream.h>
#include<process.h>
#include<conio.h>


   int  continuee(void);


void main()
{
   char xox[10][10], choice;
   int  m[10][10], i, j, count, boxnum, a=0,flag1=0;

   int  print(char[10][10], int[10][10], char, int);          //function for print x \o on a box
   char check(char[10][10], char);      //function to check if anyone among the two players have won the game

   do
   {
count=0;
clrscr();
cout<<"\t\t\t TIC TAC TOE"
<<"\n\n Instruction"
<<"\n\n To enter x/o enter the box number to print x/o in that box"
<<"\n The box numbering is done as shown below"
<<endl;

for (i=0;i<3;++i)
{
for (j=0;j<3;++j)
{
m[i][j]=++count;   //giving a number to the boxes
cout<<"|"<<m[i][j]<<"|";
}
cout<<endl;
}

for (i=0;i<3;++i)
{
for(j=0;j<3;++j)
xox[i][j]=' ';
}

count=1;
cout<<"\n\n Frist decided among yourself who will play first as x"<<endl;

while (count!=10)
{
if(count%2!=0)
{
do
{

cout<<"First player enter the box number :";
cin>>boxnum;
choice='x';
flag1=print (xox, m, choice, boxnum);
}while(flag1==2);
}
++count;
a=check(xox, choice);
if(count==10)
continue;

if(a==0)
goto newgame;

if(count%2==0)
{
do
{
cout<<"Second player enter the box number";
cin>>boxnum;
choice='o';
flag1=print(xox, m, choice, boxnum);
}while(flag1==2);
}
++count;
a=check(xox, choice);
if(a==0)
goto newgame;

}
if(count==10)
{
cout<<"\n\n the game is a draw";
a=continuee();
}
newgame:
   }while(a==0);
}

int print(char xox[10][10], int m[10][10], char choice, int boxnum)
{
   int i, j;

    for (i=0;i<3;++i)
   {
       for (j=0;j<3;++j)
       {
if(m[i][j]==boxnum)
{
if(xox[i][j]=='x'||xox[i][j]=='o')
{
cout<<"\n\n the box is already in use"<<endl<<" try again"<<endl;
return 2;
}
}
}
   }

   for (i=0;i<3;++i)
   {
       for (j=0;j<3;++j)
       {
if(m[i][j]==boxnum)
{
xox[i][j]=choice;
}

       }
   }

   for (i=0;i<3;++i)
   {
       for (j=0;j<3;++j)
  cout<<"|"<<xox[i][j]<<"|";
       cout<<endl;
   }
   return 0;
}

char check(char m[10][10], char choice)
{
   int i, j, len;
   char flag='a';

   for (i=0;i<3;++i)
   {

if (m[0][i]==choice&&m[1][i]==choice&&m[2][i]==choice)
flag=choice;
if (m[i][0]==choice&&m[i][1]==choice&&m[i][2]==choice)
      flag=choice;
   }
   if (m[0][0]==choice&&m[1][1]==choice&&m[2][2]==choice)
flag=choice;
   if (m[2][0]==choice&&m[1][1]==choice&&m[0][2]==choice)
flag=choice;

if (flag=='x')
{
cout<<"\n\first player as won the game";
continuee();
return(0);
}
if (flag=='o')
{
cout<<"\n\second player as won the game";
continuee();
return(0);
}

   return(1);
}

int continuee(void)
{
char choice;
cout<<"\n\n Enter (y/n) to continue or not to continue the game : ";
cin>>choice;
if(choice=='y')
return 0;
if(choice=='n')
exit(0);
return 0;
}

0 comments:

Post a Comment