Thursday, 5 January 2017

TIC TAC TOE IN C++

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



int main()
{
clrscr();
char box1='1';
char box2='2';
char box3='3';
char box4='4';
char box5='5';
char box6='6';
char box7='7';
char box8='8';
char box9='9';
char mark,choice;

int player,i,j,z;


   for(i=1,j=1;j<=9  ;++j,++i)
   {
   clrscr();
   cout<<"\n";
      cout<<"\t";cout<<"\t";cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
      cout<<"\t";cout<<"\t";cout<<"-|-|-"<<endl;
      cout<<"\t";cout<<"\t";cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
      cout<<"\t";cout<<"\t";cout<<"-|-|-"<<endl;
      cout<<"\t";cout<<"\t";cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
z=i%2;
if(z==1)player=1;
if(z==0) player=2;


   cout<<"Player "<<player<<" enter your choice";cin>>choice;
   if(player==1){mark='0';}
   if(player==2){mark='X';}


      if(box1=='1'&& choice=='1')box1=mark;
 else if(box2=='2'&& choice=='2')box2=mark;
 else if(box3=='3'&& choice=='3')box3=mark;
 else if(box4=='4'&& choice=='4')box4=mark;
 else if(box5=='5'&& choice=='5')box5=mark;
 else if(box6=='6'&& choice=='6')box6=mark;
 else if(box7=='7'&& choice=='7')box7=mark;
 else if(box8=='8'&& choice=='8')box8=mark;
 else if(box9=='9'&& choice=='9')box9=mark;
 else { cout<<"INVALID MOVE";if(player==1)i=0;if(player==2)i=1;  }




      if(box1==box2 && box2==box3){cout<<"Player "<<player<<" wins"; j=8; }
      if(box4==box5 && box5==box6){cout<<"Player "<<player<<" wins"; j=8; }
      if(box7==box8 && box8==box9){cout<<"Player "<<player<<" wins"; j=8; }
      if(box1==box5 && box5==box9){cout<<"Player "<<player<<" wins"; j=8; }
      if(box3==box5 && box5==box7){cout<<"Player "<<player<<" wins"; j=8; }
      if(box1==box4 && box4==box7){cout<<"Player "<<player<<" wins"; j=8; }
      if(box2==box5 && box5==box8){cout<<"Player "<<player<<" wins"; j=8; }
      if(box3==box6 && box6==box9){cout<<"Player "<<player<<" wins"; j=8; }

}  cout<<"\n \t \t GAME OVER";


getch();
}



------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TO RUN PROGRAM

COPY THE CODE IN NOTEPAD
SAVE FILE AS ".CPP"  (EG SAURAV.CPP)
SAVE IT IN "BIN" IN "TC"
OPEN "TURBO"
CLICK ON FILE
CLICK ON OPEN
SELECT THE SAVED FILE
RUN THE PROGRAM








A GAMER VISIT THIS : GAMING ONLY