Tuesday, October 4, 2011

TIC TOE Game in C

#include<iostream.h>

#include<graphics.h>

#include<stdlib.h>

#include<dos.h>

#include<conio.h>

#define UP 72

#define DOWN 80

#define LEFT 75

#define RIGHT 77

#define ESC 27

void main()

{

                clrscr();

                int gd=0,gm,i,j,k,x,y,a[8];

                int x1,y1;

                initgraph(&gd,&gm,"");

                randomize();

                char *aa;

                for(i=1;i<=8;i++)

                {

                start:

                                j=random(8)+1;

 

                                for(k=1;k<=i;k++)

                                                if(a[k]==j)

                                                                goto start;

                                                a[i]=j;

                }

                char c[10];

                i=1;

                for(x=100,y=100;x<=300;x+=100)

                {

                                for(y=100;y<=300;y+=100)

                                {

                                                itoa(i,c,10);

                                                setcolor(GREEN);

                                                setlinestyle(0,0,3);

                                                rectangle(x,y,x+100,y+100);

                                                rectangle(x+20,y+20,x+80,y+80);

                                                if(i<=8)

                                                {

                                                setfillstyle(1,YELLOW);

                                                floodfill(x+30,y+30,GREEN);

                                                setcolor(RED);

                                                setlinestyle(0,0,3);

                                                circle(x+45,y+45,10);

                                                setfillstyle(1,RED);

                                                floodfill(x+45,y+45,RED);

                                                setcolor(BLUE);

                                                outtextxy(x+43,y+43,c);

                                                }

                                                i++;

                                }

                }

                setcolor(BLUE);

                setlinestyle(0,1,3);

                rectangle(320,320,380,380);

                x=320,y=320;

                char z;

                while(1)

                {

                                while(kbhit())

                                {

                                                z=getch();

                                                setlinestyle(0,1,3);

                                                setcolor(GREEN);

                                                rectangle(x,y,x+60,y+60);

                                                if(z==UP)

                                                {

                                                                if(y>120)

                                                                                y-=100;

                                                }

                                                else if(z==DOWN)

                                                {

                                                                if(y<320)

                                                                                y+=100;

                                                }

                                                else if(z==LEFT)

                                                {

                                                                if(x>120)

                                                                                x-=100;

                                                }

                                                else if(z==RIGHT)

                                                {

                                                                if(x<320)

                                                                                x+=100;

                                                }

                                                else if (z==ESC)

                                                {

                                                                exit(0);

                                                }

                                                else if(z==' ')

                                                {

                                                                x1=x+30,y1=y+30;

                                                                if(getpixel(x1,y1)==RED)

                                                                {        //putpixel(x1,y1,YELLOW);

                                                                                if(getpixel(x1,y1+100)==BLACK&&y<320)

                                                                                {

 

                                                                                                getimage(x+10,y+10,x+40,y+40,aa);

                                                                                                setfillstyle(1,BLACK);

                                                                                                floodfill(x+10,y+10,GREEN);

                                                                                                setfillstyle(1,YELLOW);

                                                                                                floodfill(x+10,y+120,GREEN);

                                                                                                putimage(x+10,y+110,aa,0);

                                                                                }

                                                                                else if(getpixel(x1,y1-100)==BLACK&&y>200)

                                                                                {

                                                                                                getimage(x+10,y+10,x+40,y+40,aa);

                                                                                                setfillstyle(1,BLACK);

                                                                                                floodfill(x+10,y+10,GREEN);

                                                                                                setfillstyle(1,YELLOW);

                                                                                                floodfill(x+10,y-90,GREEN);

                                                                                                putimage(x+10,y-90,aa,0);

                                                                                }

                                                                                else if(getpixel(x1-100,y1)==BLACK&&x>120)

                                                                                {

                                                                                                getimage(x+10,y+10,x+40,y+40,aa);

                                                                                                setfillstyle(1,BLACK);

                                                                                                floodfill(x+10,y+10,GREEN);

                                                                                                setfillstyle(1,YELLOW);

                                                                                                floodfill(x-90,y+10,GREEN);

                                                                                                putimage(x-90,y+10,aa,0);

                                                                                }

                                                                                else if(getpixel(x1+100,y1)==BLACK&&x<320)

                                                                                {

                                                                                                getimage(x+10,y+10,x+40,y+40,aa);

                                                                                                setfillstyle(1,BLACK);

                                                                                                floodfill(x+10,y+10,GREEN);

                                                                                                setfillstyle(1,YELLOW);

                                                                                                floodfill(x+110,y+10,GREEN);

                                                                                                putimage(x+110,y+10,aa,0);

                                                                                }

                                                                }

 

                                                     }

                                                     }

                                setcolor(BLUE);

                                setlinestyle(0,0,3);

                                rectangle(x,y,x+60,y+60);

                }

                getch();

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

No comments :

Post a Comment