Hello Friends,
This is simple Tutorial about Graphics in C with example program "Tower of Hanoi" problem.
Hope you all know about tower of Hanoi problem. If you don't know , google it :)
or visit this wikipedia page : Tower Of Hanoi
Here is link to source file to c program : Click here
Note : program of c graphics works on 16-bit pc.
it will work on Windows xp. If you have windows 7 then you will need Dosbox to run this program.
If you traverse the program from main() function , then it is self explanatory , if you need help go through the explanation given below.
bar : It contains two variables of loc structure. one is for permanent location of bar called barloc, other is avloc which is to store on which location a ring can b put on bar.
ring : This structure have variable to store location of particular ring and the size of ring (top most ring have smaller size, bottom most ring have biggest size)
This is simple Tutorial about Graphics in C with example program "Tower of Hanoi" problem.
Hope you all know about tower of Hanoi problem. If you don't know , google it :)
or visit this wikipedia page : Tower Of Hanoi
Here is link to source file to c program : Click here
Note : program of c graphics works on 16-bit pc.
it will work on Windows xp. If you have windows 7 then you will need Dosbox to run this program.
If you traverse the program from main() function , then it is self explanatory , if you need help go through the explanation given below.
- loc : containg to integers x, y for storing coordinates of particular object
struct loc { int x; int y; };
struct bar { struct loc barloc; struct loc avloc; };
struct ring { struct loc rloc; int size; };
Functions :
towers : This function is recursive function. to under stand the logic go through this wikipedia page
If you have any query then leave comment, and suggestions are always welcome on this blog !
- draw : this function will draw whole screen,
first it will put all the lables on the screen
then it will draw all bars on the screen
then all the rings will be drawn
void draw(struct bar a,struct bar b,struct bar c,struct ring r[],int no) { int i; outtextxy(250,30, "TOWER OF HANOI"); outtextxy(60, 330, "SOURCE"); outtextxy(240,330, "DESTINATION"); outtextxy(470,330, "TEMPORARY"); setfillstyle(8,13); bar(a.barloc.x-2, a.barloc.y-200, a.barloc.x+1, a.barloc.y); bar(b.barloc.x-2, b.barloc.y-200, b.barloc.x+1, b.barloc.y); bar(c.barloc.x-2, c.barloc.y-200, c.barloc.x+1, c.barloc.y); for(i=1;i<=no;i++) { setfillstyle(8+(i%2),10 /*getmaxcolor()*/); bar(r[i].rloc.x-r[i].size,r[i].rloc.y-20,r[i].rloc.x+r[i].size,r[i].rloc.y); } getch(); cleardevice(); };
void towers (int n,struct bar *from,struct bar *to,struct bar *aux,struct ring r[],int no) { int i=1; if(n==1) { (r[1].rloc.x)= to->avloc.x; (r[1].rloc.y)= to->avloc.y; if(from->avloc.y!=300){from->avloc.y+=20; } to->avloc.y-=20; draw(*from,*to,*aux,r,no); return; } towers(n-1,from,aux,to,r,no); while(from->avloc.x!=r[i].rloc.x){i=i+1;} (r[i].rloc.x)=to->avloc.x; (r[i].rloc.y)=to->avloc.y; if(from->avloc.y!=300){from->avloc.y+=20; } to->avloc.y-=20; draw(*from,*to,*aux,r,no); towers(n-1,aux,to,from,r,no); }I hope this will help.
If you have any query then leave comment, and suggestions are always welcome on this blog !
Gr8 man !
ReplyDeletevery good...
Thank you :)
DeleteThis is not working in my TC :/
ReplyDeleteshowing the following error's:
Linker error :undefined symbol _closegraph in module HANOI.C
and similer 6 errors
Really very good....thanx alot sir
ReplyDeleteafter runing this code it just ask to enter no of rings & then it exits out
ReplyDeletewhat was wrong with just posting the whole code with the main() function and all?
ReplyDeleteI have posted the link to source code. Are you facing any problem to access?