sign up I login
 advanced
refer a friend - earn nickels!!

Community Contributions - Articles by goIITians

  Back to Community Shelf like the article? email it to a friend. email this article!  
  C++ project 1 TICTACTOE GAME.........for ur help......   No Nickels Awarded
Tagged with:          [Post New]posted on 15 Jun 2007 10:58:58 IST    
//CORRECTED NOW........

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

    void main()
    {
    begin:
    clrscr();
    void showgame(char,char,char,char,char,char,char,char,char);
    int choicex,choicey,flag=0,counter=1,finalchoice;
    char a1,a2,a3,a4,a5,a6,a7,a8,a9,ch,pl1[20],pl2[20];
    a1=' ';
    a2=' ';
    a3=' ';
    a4=' ';
    a5=' ';
    a6=' ';
    a7=' ';
    a8=' ';
    a9=' ';
    cout<<" WELCOME TO TIC-TAC-TOE THE OBJECTIVE OF THE GAME IS TO MAKE YOUR SYMBOL (EITHER 'X' OR 'O' TO TYPE IN A ROW EITHER VERTICALLY OR HORIZONTALLY OR DIAGONALLY AND ALSO TO PREVENT YOUR  OPPONENT FROM DOING SO SO I WISH BOTH THE PLAYERS BEST OF LUCK!!!";
    cin.get(ch);
    clrscr();


    cout<<"PLAYER1 ENTER YOUR NAME  :";

    cin.getline(pl1,20);

    cout<<" PLAYER2 ENTER YOUR NAME  :";

    cin.getline(pl2,20);
    clrscr();
cout<<"    HELLO "<<pl1<<" & "<<pl2;
    cin.get(ch);
    clrscr();



    rematch:
    counter=1;
    clrscr();
    a1=' ';
    a2=' ';
    a3=' ';
    a4=' ';
    a5=' ';
    a6=' ';
    a7=' ';
    a8=' ';
    a9=' ';


    do
    {
    xagain:
    clrscr();
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
   
    cout<<" "<<pl1<<"  PLEASE ENTER YOUR MOVE";
   
    cin>>choicex;
    switch (choicex)
    {
    case 11:
    clrscr();
    if(a1!='Y')
    {
    counter++;
    a1='X';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a1=='Y')
    {
    cout<<" SORRY "<<pl2<<"  HAS ALREADY MADE THE MOVE HERE PRESS ENTER TO RE-ENTER YOUR MOVE";
    cin.get(ch);

    goto xagain;
    }
    break;

    case 12:
    clrscr();
    if(a2!='Y')
    { counter++;
    a2='X';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a2=='Y')
    {
    cout<<" SORRY "<<pl2<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto xagain;
    }
    break;

    case 13:
    clrscr();
    if(a3!='Y')
    { counter++;
    a3='X';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a3=='Y')
    {
    cout<<" SORRY "<<pl2<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto xagain;
    }
    break;

    case 21:
    clrscr();
    if(a4!='Y')
    { counter++;
    a4='X';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a4=='Y')
    {
    cout<<" SORRY "<<pl2<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto xagain;
    }
    break;
    case 22:
    clrscr();
    if(a5!='Y')
    { counter++;
    a5='X';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a5=='Y')
    {
    cout<<" SORRY "<<pl2<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto xagain;
    }
    break;

    case 23:
    clrscr();
    if(a6!='Y')
    { counter++;
    a6='X';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a6=='Y')
    {
    cout<<" SORRY "<<pl2<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto xagain;
    }
    break;

    case 31:
    clrscr();
    if(a7!='Y')
    { counter++;
    a7='X';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a7=='Y')
    {
    cout<<" SORRY "<<pl2<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto xagain;
    }
    break;

     case 32:
    clrscr();
    if(a8!='Y')
    { counter++;
    a8='X';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a8=='Y')
    {
    cout<<" SORRY "<<pl2<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto xagain;
    }
    break;

    case 33:
    clrscr();
    if(a9!='Y')
    { counter++;
    a9='X';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }

    else if(a9=='Y')
    {
    cout<<" SORRY "<<pl2<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto xagain;
    }
    break;

    default:
    cout<<"SORRY ILLEGAL MOVE";
    goto xagain;
    }


    if(  (a1=='X'&&a2=='X'&&a3=='X')  || (a4=='X' && a5=='X' && a6=='X')  ||  ( a7=='X' && a8=='X' && a9=='X') || (a1=='X'&&a4=='X'&&a7=='X')  || (a2=='X'&& a5=='X' && a8=='X') || (a3=='X' && a6=='X' && a9=='X') || (a1=='X' && a5=='X' && a9=='X') ||  (a3=='X'&& a5=='X' && a7=='X')  )
    {
   
    cout<<pl1<<"  WINS!!!";
    cin.get(ch);

    flag++;
    cin.get(ch);
   
    cout<<" THANK YOU FOR PLAYING TIC-TAC-TOE";
    cout<<" PRESS 1 FOR RE-MATCH BETWEEN THE SAME PLAYERS";
    cout<<" PRESS 2 FOR NEW GAME";
    cout<<" PRESS 3 TO QUIT THE GAME";
    cin>>finalchoice;
    if(finalchoice==1)
    goto rematch;
    else if (finalchoice==2)
    goto begin;
    else if(finalchoice==3)
    goto finish;
    else

    break;
    }

    yagain:
    clrscr();
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    cout<<" "<<pl2<<"  PLEASE ENTER YOUR MOVE";
   
    cin>>choicey;
    switch(choicey)
    {
    case 11:
    clrscr();
    if (a1!='X')
    { counter++;
    a1='Y';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a1=='X')
    {
    cout<<" SORRY "<<pl1<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto yagain;
    }
    break;

    case 12:
    clrscr();
    if (a2!='X')
    { counter++;
    a2='Y';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a2=='X')
    {
    cout<<" SORRY "<<pl1<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto yagain;
    }
    break;

    case 13:
    clrscr();
    if (a3!='X')
    { counter++;
    a3='Y';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a3=='X')
    {
    cout<<" SORRY "<<pl1<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto yagain;
    }
    break;

    case 21:
    clrscr();
    if (a4!='X')
    { counter++;
    a4='Y';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a4=='X')
    {
    cout<<" SORRY "<<pl1<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto yagain;
    }
    break;
    case 22:
    clrscr();
    if (a5!='X')
    { counter++;
    a5='Y';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a5=='X')
    {
    cout<<" SORRY "<<pl1<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto yagain;
    }
    break;

    case 23:
    clrscr();
    if (a6!='X')
    { counter++;
    a6='Y';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }

    else if(a6=='X')
    {
    cout<<" SORRY "<<pl1<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto yagain;
    }
    break;

    case 31:
    clrscr();
    if (a7!='X')
    { counter++;
    a7='Y';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a7=='X')
    {
    cout<<" SORRY "<<pl1<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto yagain;
    }
    break;

     case 32:
    clrscr();
    if (a8!='X')
    { counter++;
    a8='Y';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a8=='X')
    {
    cout<<" SORRY "<<pl1<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto yagain;
    }
    break;

    case 33:
    clrscr();
    if (a9!='X')
    { counter++;
    a9='Y';
    showgame(a1,a2,a3,a4,a5,a6,a7,a8,a9);
    }
    else if(a9=='X')
    {
    cout<<" SORRY "<<pl1<<"  HAS ALREADY MADE THE MOVE HERE";
    cin.get(ch);
    goto yagain;
    }
    break;

    default:
    cout<<"SORRY ILLEGAL MOVE";
    goto yagain;
    }

    if(  (a1=='Y'&&a2=='Y'&&a3=='Y')  || (a4=='Y' && a5=='Y' && a6=='Y')  ||  ( a7=='Y' && a8=='Y' && a9=='Y') || (a1=='Y'&&a4=='Y'&&a7=='Y')  || (a2=='Y'&& a5=='Y' && a8=='Y') || (a3=='Y' && a6=='Y' && a9=='Y') || (a1=='Y' && a5=='Y' && a9=='Y') ||  (a3=='Y'&& a5=='Y' && a7=='Y')  )
    {
   
    cout<<pl2<<"  WINS!!!";
    cin.get(ch);
   
    flag++;
    cin.get(ch);
    cout<<" THANK YOU FOR PLAYING TIC-TAC-TOE";
    cout<<" PRESS 1 FOR RE-MATCH BETWEEN THE SAME PLAYERS";
    cout<<" PRESS 2 FOR NEW GAME";
    cout<<" PRESS 3 TO QUIT THE GAME";
    cin>>finalchoice;
    if(finalchoice==1)
    goto rematch;
    else if (finalchoice==2)
    goto begin;
    else if(finalchoice==3)
    goto finish;
    else

    break;
    }


    }while(counter<=9 );

    if(flag==0)
    {
    cout<<"THE GAME ENDS IN A DRAW";
    cout<<" THANK YOU FOR PLAYING TIC-TAC-TOE";
    cout<<" PRESS 1 FOR RE-MATCH BETWEEN THE SAME PLAYERS";
    cout<<" PRESS 2 FOR NEW GAME";
    cout<<" PRESS 3 TO QUIT THE GAME";
    cin>>finalchoice;
    if(finalchoice==1)
    goto rematch;
    else if (finalchoice==2)
    goto begin;
    else if(finalchoice==3)
    goto finish;




    }

    getch();
    finish:
    }
    void showgame(char z1,char z2,char z3,char z4,char z5,char z6,char z7,char z8,char z9)
    {
    cout<<"          1       2       3   ";
    cout<<"             |         |       ";
    cout<<"             |         |       ";
    cout<<" 1     "<<z1<<"    |    "<<z2<<"    |    "<<z3<<"    ";
    cout<<"     ________|_________|________";
    cout<<"             |         |       ";
    cout<<"             |         |       ";
    cout<<" 2     "<<z4<<"    |    "<<z5<<"    |    "<<z6<<"    ";
    cout<<"     ________|_________|________";
    cout<<"             |         |       ";
    cout<<" 3    "<<z7<<"     |    "<<z8<<"    |    "<<z9<<"    ";
    cout<<"             |         |       ";

    }
//copy the contents in notepad.......
//save it in the BIN folder of your TC....
//open it in the editor......
//save it with extension .cpp.....using save as.....
//here u have it.....ur project is ready.......









About the Author:
Aatish (2308)

Blazing goIITian

Olaaa!! Perrrfect answer. 414  [533 rates]

Aatish's Avatar

total posts: 1102    
online Offline
 this article: 25 points  (with Olaaa!! Perrrfect answer.   in 5 votes )   [?]
 
You have to be logged on to rate
  
Aatish
Aatish is offline comment by Aatish    (posted on 15 Jun 2007 10:59:32 IST)
many more to come.......
Mr.IITIAN007
Mr.IITIAN007 is offline comment by Mr.IITIAN007    (posted on 15 Jun 2007 11:33:28 IST)
As always........gr88888888888888888 work man !
Nishant2808 is offline comment by Nishant2808    (posted on 15 Jun 2007 12:28:40 IST)
gud yaar,.......
a4asd
a4asd is offline comment by a4asd    (posted on 15 Jun 2007 12:33:56 IST)
thanx
a4asd
a4asd is offline comment by a4asd    (posted on 15 Jun 2007 12:34:25 IST)
one more question
how to hack the games
gayathri is offline comment by gayathri    (posted on 15 Jun 2007 18:02:46 IST)
thnx a lot
Aatish
Aatish is offline comment by Aatish    (posted on 16 Jun 2007 14:16:51 IST)
any more..............
kunal123 is offline comment by kunal123    (posted on 17 Jun 2007 21:41:59 IST)
thanks buddy was in gr8 neeed.
Go to:   

Top Offers for goIITians
Correspondence Courses
Brilliant Tutorials
Narayana Institute
Aakash Institute
Classroom/Crash Courses
Narayana - Kota , Delhi , Others
Brilliant Tutorials - Class , Crash
Aakash Institute - Medical , Engg
Online Test Series
Brilliant Tutorials
Narayana Institute
Aakash Institute
Mahesh Tutorials
AMITY      Sri Chaitanya