Friday, 24 June 2016

TIC TAC TOE using c++

Hello guys today i am gonna give you the source code to build a TIC TAC TOE in C++.....
for any query you can place a comment..
note this program is build in code blocks compiler
you can also download the source code @ TIC TAC TOE





____________________________________________________________________


#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
using namespace std;
COORD coord={0,0};
 void gotoxy(int x,int y)
 {
 coord.X=x;
 coord.Y=y;
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
 }//for gotoxy
void setup();
void input();
void output();
int check();
bool game,p1;
int ip,pos[9]={98,99,979,79,998,66,77,88,12},counter,
history[9]={98,99,979,79,998,66,77,88,12};
int main()
{
    int result;
    setup();
    while(game==true)
    {
        input();
        output();
        result=check();
    }
    gotoxy(10,21);
    if(result==1)
    {
        switch(p1)
        {
        case true:
            cout<<"\aplayer 1 wins.......\n DEVELOPED BY - MAYANK AGRAWAL\a";
            getch();
            break;
        case false:
            cout<<"\aplayer 2 wins ........\n DEVELOPED BY - MAYANK AGRAWAL\a";
            break;
        }
    }
    else
        cout<<"\a\amatch draw !!! \n DEVELOPED BY - MAYANK AGRAWAL\a";
}
void setup()
{
    counter=0;
    game=true;p1=true;
    gotoxy(20,0);
    cout<<"TIC TAC TOE";
    for(int j=1;j<3;j++)
    {
    for(int i=4;i<=16;i++)
    {
        gotoxy(10*j,i);
        cout<<"|";
    }}
    for(int i=8;i<=12;i+=4)
    {
        for(int j=3;j<=26;j++)
        {
            gotoxy(j,i);
            cout<<"-";
        }
    }
    int x[3]={5,14,24};
    int y[3]={6,10,14};
    for(int i=0,a=1;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            gotoxy(x[j],y[i]);
            cout<<a;
            a++;
        }
    }
    gotoxy(3,2);
    cout<<"PLAYER 1 (X) .....PLAYER 2(O)";
    gotoxy(5,20);
    cout<<"player   ,enter place : ";
}
void input()
{
     gotoxy(12,20);
     if(p1==true)
        cout<<"\a1";
     else
        cout<<"\a2";
     gotoxy(29,20);
     cout<<" ";
     do
     {
     cin.ignore();    //buffer problem
     gotoxy(30,20);
     cout<<" ";
     gotoxy(29,20);
     cin>>ip;
     }while(ip<0||ip>10||ip==history[0]||ip==history[1]||ip==history[2]||
ip==history[3]||ip==history[4]||ip==history[5]||ip==history[6]
||ip==history[7]||ip==history[8]);
     if(p1==true)
     pos[ip-1]=0;
     else
        pos[ip-1]=1;
    history[counter]=ip;
}
void output()
{
    switch(ip)
    {
        case 1: gotoxy(5,6);
        break;
        case 2: gotoxy(14,6);
        break;
        case 3: gotoxy(24,6);
        break;
        case 4: gotoxy(5,10);
        break;
        case 5: gotoxy(14,10);
        break;
        case 6: gotoxy(24,10);
        break;
        case 7: gotoxy(5,14);
        break;
        case 8: gotoxy(14,14);
        break;
        case 9: gotoxy(24,14);
        break;
    }
    switch(p1)
    {
        case true: cout<<"X";
        break;
        case false: cout<<"O";
        break;
    }
}
int check()
{
    counter++;
    for(int i=0;i<=6;i+=3)
    {
   if(pos[0+i]==pos[1+i]&&pos[1+i]==pos[2+i])
      {
          game=false;
          return 1;
      }
    }
     for(int i=0;i<=3;i++)
    {
   if(pos[0+i]==pos[3+i]&&pos[3+i]==pos[6+i])
      {
          game=false;
          return 1;
      }
    }
    if(pos[0]==pos[4]&&pos[4]==pos[8])
    {
        game=false;
        return 1;
    }
    if(pos[2]==pos[4]&&pos[4]==pos[6])
    {
        game=false;
        return 1;
    }
    if(counter==9)
    {
        game=false;
        return 0;
    }
    if(p1==true)
        p1=false;
    else
        p1=true;
    return 0;
}
______________________________________________________

OUTPUT :-


                                 THANK YOU!!!!

1 comment:

  1. Hi Dear Freinds!
    Its Time to enjoy by playing Tic Tac Toe Game.The Game is about Tick Cross.You Can play this Game Online by visit this page.

    ReplyDelete