Thursday 26 December 2013

TICK TOCK GAME USING C++

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


void final();
void winner();
void condition();
void print();
void next();
void initialise();

int m=0,i,j,f=0,l=0,rw,co,k;
char a[3][3],ch='y';

class player
{
public:
char nm[20];

void getdata()
{
cout<<nm<<" enter position\n";
cout<<"row no - ";
cin>>i;
cout<<"coloumn no - ";
cin>>j;
if(i>2 || j>2)
{
getdata();
}

}
} p1,p2;


class game
{
public:
 void check1()
{
//s1=clock();
final();

if(f==0)
{
  condition();
  if(l>0)
 {
   p1.getdata();
   if(a[i][j] == 'x'|| a[i][j] =='o')
    {
     cout<<"re-enter \n";
     check1();
    }
   else
   {
    a[i][j]='x';
    print();
    check2();
   }
 }
else
 {
  cout<<"game draw";
  next();
 }
}

else
{
m=1;
winner();
}


}



void check2()
{
final();

if(f==0)
{
  condition();
  if(l>0)
{
  p2.getdata();
 if(a[i][j] == 'x'||a[i][j] =='o')
 {
  cout<<"re-enter \n";
  check2();

 }

 else
 {
  a[i][j]='o';
  print();
  check1();
 }
 }
else
{
cout<<"game draw";
next();
}
}
else
{
winner();
}


}
};

void main()
{
clrscr();
cout<<"enter name of 1st player \n";
cin>>p1.nm;
cout<<"enter name of 2nd player \n";
cin>>p2.nm;
game g;
for(i=0;i<20;i++)
{
if(ch=='y'||ch=='Y')
{
initialise();
print();
g.check1();
}
}
getch();
}


void final()
{
 if((a[0][0]=='x' && a[0][1]=='x'&& a[0][2]=='x')||(a[0][0]=='o' && a[0][1]=='o'&& a[0][2]=='o'))
 {
  f=1;
 }
 else if((a[1][0]=='x'&&a[1][1]=='x'&&a[1][2]=='x')||(a[1][0]=='o'&&a[1][1]=='o'&&a[1][2]=='o'))
 {
 f=1;
 }
 else if((a[2][2]=='x'&&a[2][1]=='x'&&a[2][2]=='x')||(a[2][2]=='o'&&a[2][1]=='o'&&a[2][2]=='o'))
 {
 f=1;
 }
 else if((a[0][0]=='x'&&a[1][1]=='x'&&a[2][2]=='x')||(a[0][0]=='o'&&a[1][1]=='o'&&a[2][2]=='o'))
 {
 f=1;
 }
 else if((a[0][0]=='x'&&a[1][0]=='x'&&a[2][0]=='x')||(a[0][0]=='o'&&a[1][0]=='o'&&a[2][0]=='o'))
 {
 f=1;
 }
 else if((a[0][1]=='x'&&a[1][1]=='x'&&a[2][1]=='x')||(a[0][1]=='o'&&a[1][1]=='o'&&a[2][1]=='o'))
 {
 f=1;
 }
 else if((a[0][2]=='x'&&a[1][2]=='x'&&a[2][2]=='x')||(a[0][2]=='o'&&a[1][2]=='o'&&a[2][2]=='o'))
 {
 f=1;
 }
 else if((a[0][2]=='x'&&a[1][1]=='x'&&a[2][0]=='x')||(a[0][2]=='o'&&a[1][1]=='o'&&a[2][0]=='o'))
 {
 f=1;
 }
 else
 {
 f=0;
 }
}




void condition()
{
l=0;
 for(rw=0;rw<3;rw++)
  {
  for(co=0;co<3;co++)
  {
   if(a[rw][co] == 'x' || a[rw][co] =='o')
   {
     k=0;
   }
   else
   {
   l++;
   }
  }
  }
}

void print()
{
cout<<"\n";
cout<<a[0][0]<<" | "<<a[0][1]<<" | "<<a[0][2]<<"\n";
cout<<"_"<<"  "<<"_"<<"  "<<"_"<<"  "<<"_"<<"\n";

cout<<a[1][0]<<" | "<<a[1][1]<<" | "<<a[1][2]<<"\n";
cout<<"_"<<"  "<<"_"<<"  "<<"_"<<"  "<<"_"<<"\n";

cout<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<"\n";
}

void winner()
{
if(m==1)
{
cout<<p2.nm<<" is the winner\n";
next();
}
else
{
cout<<p1.nm<<" is the winner\n";
next();
}
}

void next()
{
cout<<"do you want to play another game (y/n) ??\n";
ch=getche();
}

void initialise()
{
for(rw=0;rw<3;rw++)
{
 for(co=0;co<3;co++)
 {
  a[rw][co]=' ';
 }
}
}

1 comment:

  1. you have solve my critical problem

    thanks..

    ReplyDelete