top of page

Animated Circles In C++

 

#include<stdlib.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

 

 

void main()

{

       //////////////////////////////////////////////////

int x,y,i;

                int g=DETECT,d;

       initgraph(&g,&d,"\tc\bgi");

       cleardevice();

       x=getmaxx()/2;

       y=getmaxy()/2;

       settextstyle(TRIPLEX_FONT, HORIZ_DIR, 3);

       setbkcolor(rand());

       setcolor(4);

       outtextxy(30,100,"Press");

       outtextxy(30,130,"any");

       outtextxy(30,160,"key");

       outtextxy(30,190, "to");

       outtextxy(30,220,"Quit");

       while (!kbhit())

      //////////////////////////////////////////////////

       {

       setcolor(rand());

       for (int i=0;i<50;i++)

       circle(x,y,i );

       setcolor(rand());

       for (int j=70;j<120;j++)

       circle(x,y,j);

       setcolor(rand());

       for (int k=140;k<190;k++)

       circle(x,y,k);

       setcolor(rand());

       for (int l=210;l<230;l++)

       circle(x,y,l);

       delay(200);

      //////////////////////////////////////////////////

       }

       getch();

       closegraph();

}

Analog Clock Program

 

#include<graphics.h>

#include<conio.h>

#include<math.h>

#include<dos.h>

void main()

{

int gd=DETECT,gm;

int x=320,y=240,r=200,i,h,m,s,thetamin,thetasec;

struct  time t;

char n[12][3]={"3","2","1","12","11","10","9","8","7","6","5","4"};

initgraph(&gd,&gm,"f:\arun\tc");\put the directory which contains

egavga.bgi

circle(x,y,210);

setcolor(4);

settextstyle(4,0,5);

for(i=0;i<12;i++)

{

if(i!=3)

outtextxy(x+(r-14)*cos(M_PI/6*i)-10,y-(r-14)*sin(M_PI/6*i)-26,n[i]);

else

outtextxy(x+(r-14)*cos(M_PI/6*i)-20,y-(r-14)*sin(M_PI/6*i)-26,n[i]);

}

gettime(&t);

printf("The current time is: %2d:%02d:%02d.%02d

",t.ti_hour, t.ti_min,

t.ti_sec, t.ti_hund);

while(!kbhit())

{

setcolor(5);

setfillstyle(1,5);

circle(x,y,10);

floodfill(x,y,5);

gettime(&t);

if(t.ti_min!=m)

{

setcolor(0);

line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180

)));

circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))

,10);

line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h

-((m/2)*(M_PI/180))));

circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(

(m/2)*(M_PI/180))),10);

}

if(t.ti_hour>12)

t.ti_hour=t.ti_hour-12;

if(t.ti_hour<4)

h=abs(t.ti_hour-3);

else

h=15-t.ti_hour;

m=t.ti_min;

if(t.ti_min<=15)

thetamin=(15-t.ti_min)*6;

else

thetamin=450-t.ti_min*6;

if(t.ti_sec<=15)

thetasec=(15-t.ti_sec)*6;

else

thetasec=450-t.ti_sec*6;

setcolor(4);

line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h

-((m/2)*(M_PI/180))));

circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(

(m/2)*(M_PI/180))),10);

line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180

)));

circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))

,10);

setcolor(15);

line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180

)));

delay(1000);

setcolor(0);

line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180

)));

}

}

A tictactoe game in C++.

Code :

 

#include<iostream.h>          //opening headerfile iostream for in-out

#include<conio.h>             //opening headerfile conio for clrscr();

#include<stdlib.h>            //opening headerfile stdlib for

random(int);

char tic[3][3];               //global matrix declerations

int d,e,f,a,t,i,j,x,y;        //global variables declerations

void display();               //displays the matrix

void user();                  //function for user's move

void newdisp();               //function for display of matrix after

every

move

void pc();                    //function for pc's move

int check();                  //function for finding out the winner

int horcheck();               //function for horizontal line check

int vercheck();               //function for vertical line check

int diagcheck();              //function for diagonal line check

main()                        //main function

{

clrscr();                     //clears the previous output screen

randomize();                  //initialize random function calling

int d=random(2);              //random function call

for(i=0;i<3;i++)

                for(j=0;j<3;j++)

                tic[i][j]=' ';        //assigning space ' ' to all elements of matrix

display();                    //display function call

d==0?user():pc();             //random starting of the game depending

on d

getch();                      //provides output by getting input

without

returning to program

return 0;                     //return int to main function

}

void display()                //display function definition

{

for(t=0;t<3;t++)

                {

                cout<<"                "<<tic[t][0]<<" | "<<tic[t][1]<<" | "<<tic[t][2]<<endl;

//figure formation

                if(t!=2)

                cout<<"                --|---|--"<<endl;

                }

}

void user()                  //user function definition

{

cout<<"               

 

ENTER THE CO-ORDINATES WHERE YOU WANT TO PUT UR 'X' i.e

0,1,2

";

cin>>x>>y;

if((x<0)||(x>2)&&(y<0)||(y>2))  //check for valid co-ordinates

                {

                cout<<"               

 

ENTER THE CORRECT CO-ORDINATES";

                user();    //user function call

                }

else

                {

                if(tic[x][y]==' ')    //check for vacant space at entered co-ordinates

                                {

                                tic[x][y]='X';  //assigning user 'X' to the co-ordinates

                                newdisp();    //newdisp function call

                                }

                else

                                {

                                cout<<"               

 

THIS POSITION IS ALREADY FILLED. CHOOSE SOME OTHER

CO-ORDINATES";

                                user();       //user function call

                                }

                }

d=check();            //check function call

if(d==0)

pc();                 //pc function call

else

                {

                cout<<"               

 

YOU ARE THE WINNER";

                getche();     //requires enter to return to program. prevents return

without display

                exit(1);      //program termination

                }

}

void newdisp()        //newdisp function definition

{

for(t=0;t<3;t++)

                {

                cout<<"                "<<tic[t][0]<<" | "<<tic[t][1]<<" | "<<tic[t][2]<<endl;

//displays new tictactoe after user or pc turn

                if(t!=2)

                cout<<"                --|---|--"<<endl;

                }

}

void pc()          //pc function call

{

int f,z;

randomize();       //initialize random function calling

cout<<"               

 

THIS IS THE COMPUTER'S MOVE

 

";

for(i=0;i<=20;i++)

                {

                f=random(3);

                z=random(3);

                if(tic[f][z]==' ')      //check for vacant space at entered

co-ordinates

                                {

                                tic[f][z]='O';  //assigning pc 'O' to the co-ordinates

                                goto x;         //exiting for loop to display new tictactoe

                                }

                else

                continue;               //

                }

x:newdisp();                    //newdisp function call

d=check();                      //check function call

if(d==0)

user();                         //user function call

else

                {

                cout<<"               

 

I'M THE WINNER";

                getche();           //requires enter to return to program. prevents

return without display

                exit(1);            //program termination

                }

}

check()                     //check function definition

{

horcheck();                 //horcheck function call

vercheck();                 //vercheck function call

diagcheck();                //diagcheck function call

return (d||e||f);

}

horcheck()                  //horcheck function definition

{

if(((tic[0][0]==tic[0][1])&&(tic[0][1]==tic[0][2])&&(tic[0][1]!='

'))||((tic[1][0]==tic[1][1])&&(tic[1][1]==tic[1][2])&&(tic[1][1]!='

'))||((tic[2][0]==tic[2][1])&&(tic[2][1]==tic[2][2])&&(tic[2][2]!='

')))

d=1;                        //checks each element of a horizontal line

to

be same

else                        //returns 1 if all 3 elements of any

horizontal line are same

d=0;                        //else returns 0

return d;

}

vercheck()                  //vercheck function definition

{

if(((tic[0][0]==tic[1][0])&&(tic[1][0]==tic[2][0])&&(tic[0][0]!='

'))||((tic[0][1]==tic[1][1])&&(tic[1][1]==tic[2][1])&&(tic[0][1]!='

'))||((tic[0][2]==tic[1][2])&&(tic[1][2]==tic[2][2])&&(tic[0][2]!='

')))

e=1;                        //checks each element of a vertical line to

be

same

else                        //returns 1 if all 3 elements of any

vertical

line are same

e=0;                        //else returns 0

return e;

}

diagcheck()                 //diagcheck function definition

{

if(((tic[0][0]==tic[1][1])&&(tic[1][1]==tic[2][2])&&(tic[0][0]!='

'))||((tic[0][2]==tic[1][1])&&(tic[1][1]==tic[2][0])&&(tic[1][1]!='

')))

f=1;                        //checks each element of a diagonal line to

be

same

else                        //returns 1 if all 3 elements of any

diagonal

line are same

f=0;                        //else returns 0

return f;

}

bottom of page