C++ Project on Tic Tac Toe Game

Submitted by Editor

Tic Tac Toe

#include
#include
#include
#include
#include
#define max 100
int draw=0,winner=0;
class tic
  {
    private : char a[max],a1[max],a2[max],a3[max],b[max],b1[max],b2[max],
	      b3[max],c[max],c1[max],c2[max],c3[max],d[max],d1[max],d2[max],
	      d3[max],e[max],e1[max],e2[max],e3[max],f[max],f1[max],f2[max],
	      f3[max],g[max],g1[max],g2[max],g3[max],h[max],h1[max],h2[max],
	      h3[max],i[max],i1[max],i2[max],i3[max],j[max],j1[max],j2[max],
	      j3[max],k[max],k1[max],k2[max],k3[max],l[max],l1[max],l2[max],
	      l3[max],m[max],m1[max],m2[max],m3[max],n[max],n1[max],n2[max],
	      n3[max],x[max],x1[max],x2[max],x3[max],y[max],y1[max],y2[max],
	      y3[max],z[max],z1[max],z2[max],z3[max],za[max],za1[max],
	      za2[max],za3[max],az[max];
	      int o,p,q,r,s,t,u,v,w,o1,p1,q1,r1,s1,t1,u1,v1,w1;
	      char xa[3][3];
	      int player,row,col,count;
    public : tic();
	     void introduction();
	     void position();
	     void question(int, int, int);
	     void game();
	     void sorry();
	     void check();
    }
//************************************************
// THE CONSTRUCTOR TO INITIALISE THE VALUES
//************************************************
tic::tic()
  {
  count=0;
    strcpy(a,"Which city is called eternal city?");
    strcpy(a1,"ROME");
    strcpy(a2,"PARIS");
    strcpy(a3,"LONDON");
    strcpy(b,"Expand WTC.");
    strcpy(b1,"WORLD TRADE CENTER");
    strcpy(b2,"WITH TEXT COMPARISON");
    strcpy(b3,"WITH TEXT CENTER");
    strcpy(c,"What is the only thing that can be seen from the moon?");
    strcpy(c1,"QUTUB MINAR");
    strcpy(c2,"CHINA GATE");
    strcpy(c3,"TWIN BUILDINGS OF U.S.A.");
    strcpy(d,"Who is the highest Indian test wicket-taker?");
    strcpy(d1,"JAVAGAL SRINATH");
    strcpy(d2,"KAPIL DEV");
    strcpy(d3,"ANIL KUMBLE");
    strcpy(e,"Which country is called the Sickman of Europe?");
    strcpy(e1,"SPAIN");
    strcpy(e2,"INDIA");
    strcpy(e3,"TURKEY");
    strcpy(f,"What is the female goat called?");
    strcpy(f1,"NANNY GOAT");
    strcpy(f2,"PANNY GOAT");
    strcpy(f3,"TANNY GOAT");
    strcpy(g,"Who is considered the father of Modern Astronomy?");
    strcpy(g1,"GALILEO");
    strcpy(g2,"COPERNICUS");
    strcpy(g3,"ABRAHAM LINCOLN");
    strcpy(h,"Name the only bird known to have nostrils on the tip of its bill.");
    strcpy(h1,"THE KINGFISHER");
    strcpy(h2,"THE SPARROW");
    strcpy(h3,"THE KIWI");
    strcpy(i,"Expand PCO");
    strcpy(i1,"PUBLIC CALL OFFICE");
    strcpy(i2,"PRIVATE CALL OFFICE");
    strcpy(i3,"POST CALL OFFICE");
    strcpy(j,"Where is Charminar situated?");
    strcpy(j1,"DELHI");
    strcpy(j2,"HYDERABAD");
    strcpy(j3,"MUMBAI");
    strcpy(k,"Through which of these medium can sound not travel?");
    strcpy(k1,"AIR");
    strcpy(k2,"WATER");
    strcpy(k3,"VACCUM");
    strcpy(l,"Who played the jailer in the film SHOLAY");
    strcpy(l1,"ASRANI");
    strcpy(l2,"KESTO MUKHERJEE");
    strcpy(l3,"JAGDEEP");
    strcpy(m,"In which state is Kanyakumari?");
    strcpy(m1,"KERELA");
    strcpy(m2,"TAMIL NADU");
    strcpy(m3,"KARNATAKA");
    strcpy(n,"What language did the Buddha preach in?");
    strcpy(n1,"MAGADHI");
    strcpy(n2,"SANSKRIT");
    strcpy(n3,"PALI");
    strcpy(x,"Which of these tourists spots is not in Delhi?");
    strcpy(x1,"CHARMINAR");
    strcpy(x2,"RED FORT");
    strcpy(x3,"INDIA GATE");
    strcpy(y,"Which of these is not a state?");
    strcpy(y1,"MIZORAM");
    strcpy(y2,"CHANDIGARH");
    strcpy(y3,"NAGALAND");
    strcpy(z,"In which language was the film ROJA originally made?");
    strcpy(z1,"HINDI");
    strcpy(z2,"MALAYALAM");
    strcpy(z3,"TAMIL");
    strcpy(za,"Which was the first country to send a craft into outter space?");
    strcpy(za1,"USSR");
    strcpy(za2,"USA");
    strcpy(za3,"UK");
    xa[0][0]=' ';
    xa[0][1]=' ';
    xa[0][2]=' ';
    xa[1][0]=' ';
    xa[1][1]=' ';
    xa[1][2]=' ';
    xa[2][0]=' ';
    xa[2][1]=' ';
    xa[2][2]=' ';
  }
//************************************************
//FUNCTION TO INTRODUCE THE GAME
//************************************************
void tic::introduction()
  { clrscr();
    cout< <"\n\n     ****** TIC-TAC-TOE ******               "<>row;
	      }while(row<0||row>3);
	      row=row-1;
	      do
	      {
	      cout< <"\n\nPlayer "<>col;
	      }while(col<0||col>3);
	      col=col-1;
	      if(xa[row][col]=='A'||xa[row][col]=='B')
		{
		f=1;
		cout< <"\nThis space is already occupied";
		getch();
		}
	      else
		question(player,row,col);
if(f==0)
    count++;
      }
int ch=0;
//***********************************************
//THIS FUNCTION CHECKS FOR THE WINNER.
//***********************************************
void tic::check()
  {
    for(int line=0;line<3;line++)
     {
     if(xa[line][0]=='A' && xa[line][1]=='A' && xa[line][2]=='A')
	 {
	 winner=player;
	 }
     if(xa[line][0]=='B' && xa[line][1]=='B' && xa[line][2]=='B')
	 {
	 winner=player;
	 }
     if(xa[0][line]=='A' && xa[1][line]=='A' && xa[2][line]=='A')
	 {
	 winner=player;
	 }
     if(xa[0][line]=='B' && xa[1][line]=='B' && xa[2][line]=='B')
	 {
	 winner=player;
	 }
     }
     if(xa[0][0]=='A' && xa[1][1]=='A' && xa[2][2]=='A')
	 {
	 winner=player;
	 }
  if(xa[0][0]=='B' && xa[1][1]=='B' && xa[2][2]=='B')
	 {
	 winner=player;
	 }
  if(xa[0][2]=='A' && xa[1][1]=='A' && xa[2][0]=='A')
	 {
	 winner=player;
	 }
  if(xa[0][2]=='B' && xa[1][1]=='B' && xa[2][0]=='B')
	 {
	 winner=player;
	 }
  if(winner==1)
      {
      cout<<"\n\nA IS THE WINNER";
      getch();
      }
  if(winner==2)
      cout<<"\n\nB IS THE WINNER";
      getch();
  }
//*********************************************
//
//*********************************************
  void tic::sorry()
   {
   cout<<"\n\nSORRY YOUR ANSWER IS WRONG";
   cout<<"\n\nHENCE NO SPACE OCCUPIED";
   getch();
   }
//***********************************************
//
//***********************************************
void tic::question(int player,int row,int col)
  {
  char play;
  if(player==1)
     play='A';
  if(player==2)
      play='B';
  ch++;
    switch(ch)
      {
	case 1:cout<>o;
	       if(o==1)
		  {
		  xa[row][col]=play;
		  draw=draw+1;
		   }
	       else
		  sorry();
		break;
	case 2:cout< >p;
	       if(p==1)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
	       else
		   sorry();
	break;
	case 3:cout< >q;
	       if(q==2)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
	       else
		   sorry();
	break;
	case 4:cout< >r;
	       if(r==3)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
	       else
		   sorry();
	break;
	case 5:cout< >s;
	       if(s==3)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
	       else
		   sorry();
	break;
	case 6:cout< >t;
	       if(t==1)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
	       else
		   sorry();
	break;
	case 7:cout< >u;
	       if(u==2)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
	       else
		   sorry();
	break;
	case 8:cout< >v;
	       if(v==3)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
	       else
		   sorry();
	break;
	case 9:cout< >w;
	       if(w==1)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
	       else
		   sorry();
	 break;
	 case 10:cout< >o1;
	       if(o1==2)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
	       else
		   sorry();
	 break;
	 case 11:cout< >p1;
	       if(p1==3)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
	       else
		   sorry();
	 break;
	 case 12:cout< >q1;
	       if(q1==1)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
	       else
		   sorry();
	 break;
	 case 13:cout< >r1;
		 if(r1==2)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
		 else
		   sorry();
	 break;
	 case 14:cout< >s1;
		 if(s1==3)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
		 else
		   sorry();
	 break;
	 case 15:cout< >t1;
		 if(t1==1)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
		 else
		   sorry();
	 break;
	 case 16:cout< >u1;
		 if(u1==2)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
		 else
		   sorry();
	 break;
	 case 17:cout< >v1;
		 if(v1==3)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
		 else
		   sorry();
	 break;
	 case 18:cout< >w1;
		 if(w1==1)
		 {
		 xa[row][col]=play;
		 draw=draw+1;
		 }
		 else
		   sorry();
      }
    }
////**********************************************
////THE MAIN FUNCTION
//************************************************
void main()
  {
    tic tac;
    int choice=0,i=1;
    do
    {
    clrscr();
//************************************************
//A FUNCTION FOR THE WELCOME TO THE GAME
//************************************************
    textmode(C40);
    textbackground(WHITE);
    textcolor(BLUE);
    clrscr();
    cout< <"\n\n\n   WELCOME TO THE GAME OF TIC-TAC-TOE"<>choice;
    if(choice==1)
      {
	tac.introduction();
	getch();
      }
    if(choice==2)
      {
while((i< =18)&&(draw<9))
	  {
	    tac.position();
	    tac.check();
	    if(winner==0)
	      {
		tac.game();
	      }
	    else
	    break;
	    i++;
	  }
	tac.position();
	if(winner==0)
	cout<<"\n\n THE GAME IS A DRAW ";
	getch();
      }
    if(choice==3)
      {
	exit(0);
      }
    }while(choice<3);
    getch();
  }

Add Your Comment - Guidelines
You can express your opinion or reaction in the form below!

100 characters required

0 Comments:

Be the first one to comment!

You may also want to see:
QUOTE OF THE DAY
Love isn't something you find. Love is something that finds you. - Loretta Young
April 26th, 2024 - Friday
background

Sign in to continue