Tic - Tac Toe Game using c and graphics

Tic - Tac Toe:

Hey guys this is one of my favorite game. With graphics I have coded this game. If you have any queries regarding the code then please free to ask. You can ask any questions related to this code. And one more thing if you can modify the program into simple one then you are always welcome. Please enjoy coding........

/*Program for tic-tac-toe using graphics and mouse keys.*/
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<process.h>

#define FALSE 0
#define TRUE 1
#define EMPTY 0
#define EX 1
#define OH 2
union REGS i, o;
int arr[9],nextchar=EX;
int x1,x2,y1,y2;

int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}

void showmouseptr()
{
   i.x.ax = 1;
   int86(0X33,&i,&o);
}

void getmousepos(int *button, int *x, int *y)
{
   i.x.ax = 3;
   int86(0X33,&i,&o);

   *button = o.x.bx;
   *x = o.x.cx;
   *y = o.x.dx;
}

void hidemouseptr()
{
    i.x.ax=2;
    int86(0x33,&i,&o);
}
restrictmouseptr(int x1,int y1,int x2,int y2)
{
    i.x.ax=7;
    i.x.cx=x1;
    i.x.dx=x2;
    int86(0x33,&i,&o);
    i.x.ax=8;
    i.x.cx=y1;
    i.x.dx=y2;
    int86(0x33,&i,&o);
    return 0;
}

void drawgrid()
{
    cleardevice();
    setcolor(1);
    setfillstyle(1,1);
    bar(200,180,440,185);
    floodfill(201,181,1);
    bar(200,260,440,265);
    floodfill(201,261,1);
    bar(280,110,285,350);
    floodfill(281,111,1);
    bar(360,110,365,350);
    floodfill(361,111,1);
}

void drawex(int a1,int b1,int a2,int b2)
{
    int d=15;
    setcolor(2);
    setlinestyle(1,0,THICK_WIDTH);
    hidemouseptr();
    line(a1+d,b1+d,a2-d,b2-d);
    line(a2-d,b1+d,a1+d,b2-d);
    showmouseptr();
}

void drawoh(int x1,int y1,int x2,int y2)
{
    hidemouseptr();
    setcolor(3);
    circle((x1+x2)/2,(y1+y2)/2,20);
    showmouseptr();
}


int findwinner()
{
    int i;
    if((arr[0]==arr[1])&&(arr[1]==arr[2])&&arr[0]!=0)
        return (arr[0]);
    if((arr[3]==arr[4])&&(arr[4]==arr[5])&&arr[3]!=0)
        return (arr[3]);
    if((arr[6]==arr[7])&&(arr[7]==arr[8])&&arr[6]!=0)
        return (arr[6]);
    if((arr[0]==arr[3])&&(arr[3]==arr[6])&&arr[0]!=0)
        return (arr[0]);
    if((arr[1]==arr[4])&&(arr[4]==arr[7])&&arr[1]!=0)
        return (arr[1]);
    if((arr[2]==arr[5])&&(arr[5]==arr[8])&&arr[2]!=0)
        return (arr[2]);
    if((arr[0]==arr[4])&&(arr[4]==arr[8])&&arr[0]!=0)
        return (arr[0]);
    if((arr[2]==arr[4])&&(arr[4]==arr[6])&&arr[2]!=0)
        return (arr[2]);
    for(i=0;i<8;i++)
        if(arr[i]==0)
            return 0;

    return 3;
}

int isdraw(int arr[])
{
    int i;
    for(i=0;i<8;i++)
    {
    gotoxy(1,1);

    printf("%d  ",arr[i]);
        if(arr[i]==0)
            return (0);
    }
    return (1);
}
isgameover()
{
    int winner;
    winner=findwinner();
    settextstyle(1,0,2);
    setcolor(2);
    if(winner==0)
        return 0;
    else
    {
    if(winner==EX)
    {
        outtextxy(150,380,"       X WON the game !");
        return (TRUE);
    }
    else if(winner==OH)
    {
        outtextxy(150,380,"       O WON the game !") ;
        return (TRUE);
    }
    else if(winner==3)
    {
        outtextxy(150,380,"      GAME DRAWN !") ;
        return (TRUE);
    }
    }
    return 0;
}

char resetgame()
{
    char ch;
    int i;
    outtextxy(150,420,"Do you want to continue (Y/N)");
    fflush(stdin);
    ch=getch();
    if(ch=='y'||ch=='Y')
        return 'y';
    else
        return 'n';
}
mainpage()
{
   clrscr();
   textcolor(2);
   gotoxy(28,5);
   cprintf(" WELCOME  TO TIC TAC TOE");
   gotoxy(16,8);
   textcolor(9);
   cprintf(" PROGRAMMER   :");
   gotoxy(31,8);
   textcolor(11);
   cprintf("    ASHOK  KUMAR  SHRESTHA (0 0 7)");
   gotoxy(30,11);
   textcolor(6);
   cprintf("  RULES TO PLAY GAME:");
   gotoxy(16,13);
   textcolor(7);
   cprintf(" 1.POSITION MOUSE CURSOR WHERE YOU WANT TO MARK X OR O");
   gotoxy(16,15);
   textcolor(7);
   cprintf(" 2.FIRST TURN IS OF X USING ONLY LEFT KEY");
   gotoxy(16,17);
   textcolor(7);
   cprintf(" 3.SECOND TURN IS OF O USING RIGHT KEY ONLY");
   gotoxy(26,22);
   textcolor(9+BLINK);
   cprintf("   ENJOY THE GAME %c %c %c %c %c",1,1,1,1,1);
   getch();
   return 0;
}
void main()
{
    int gd=DETECT,gm,button;
    int x,y,over,i,flag,turn=EX,cnt=0,k=80;
    char reset='y',ch;
    mainpage();
    initgraph(&gd,&gm,"..\\bgi");

    while(reset=='y')
    {
        turn=EX;
        drawgrid();
        showmouseptr();
        restrictmouseptr(205,115,435,340);
        for(i=0;i<9;i++)
            arr[i]=0;
        over=FALSE;

        while(!kbhit()&&(over==FALSE))
        {
            getmousepos(&button,&x,&y);
            int i,mx=0,my=0;

            mx=(x-210)/k;
            my=(y-110)/k;
            x1=mx*k+200;    y1=my*k+100;
            x2=(mx+1)*k+200;    y2=(my+1)*k+100;

            if(mx==0&&my==0)       cnt=0;
            else if(mx==0&&my==1)       cnt=1;
            else if(mx==0&&my==2)       cnt=2;
            else if(mx==1&&my==0)       cnt=3;
            else if(mx==1&&my==1)       cnt=4;
            else if(mx==1&&my==2)       cnt=5;
            else if(mx==2&&my==0)       cnt=6;
            else if(mx==2&&my==1)       cnt=7;
            else if(mx==2&&my==2)       cnt=8;

            gotoxy(20,20);
            if(arr[cnt]==0)
            {
            if(((button&1)==1)&&(turn==EX))
            {
                turn=OH;
                arr[cnt]=EX;
                drawex(x1,y1,x2,y2);
                over=isgameover();
            }
            if(((button&2)==2)&&(turn==OH))
            {
                turn=EX;
                arr[cnt]=OH;
                drawoh(x1,y1,x2,y2);
                over=isgameover();
            }
            }
        }
        reset=resetgame();
        hidemouseptr();
    }
    closegraph();
    restorecrtmode();
}

/* End of program */








 

Comments

Popular posts from this blog

Wizard Quiz: Privacy Policy

Sorting Hat: Privacy Policy

mTranslator