Add to Favorites    Make Home Page 22033 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Computer Graphics Home » Tower of Hanoi - A Graphical Representation

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Tower of Hanoi - A Graphical Representation
Author Rakesh Juyal
Author Email Rakesh_In_Uk [at] Yahoo.com
Description Tower of Hanoi Graphical Representation.
Category C++ » Computer Graphics
Hits 392801
Code Select and Copy the Code
// Towers Of Hanoi // // By: Rakesh Juyal // // I.T.S. Mohan Nagar, Gzb // // mailme: juyalrules@yahoo.com // // mailme: rakesh@mindless.com // // [ You can use more than 10 Disks too, just change the value of MAX ] // #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #define MAX 12 #define BegPos 105 #define AuxPos 305 #define EndPos 505 int width; typedef struct disc { char val1[MAX]; char top,pos; }; void push(disc *tt,int x); pop(disc *tt); void tower(int,disc *,disc *,disc *); void draw_stack(disc *beg,disc *,disc *); int main(void) { int gdriver = DETECT, gmode, errorcode; int i,x=2; disc beg,end,aux; printf(" TOWER OF HANOI "); printf("======================================================="); printf(" How Many Disks[1-10]:- "); scanf("%d",&x); initgraph(&gdriver, &gmode, "d:\TC\BGI"); errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s ", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } width=50/x; beg.top=end.top=aux.top=0; beg.pos=1;end.pos=3;aux.pos=2; for(i=0;i<x;i++) push(&beg,(x-i)+1); draw_stack(&beg,&end,&aux); tower(x,&beg,&end,&aux); closegraph(); return 0; } void tower(int n,disc *beg,disc *aux,disc *end) { if(n>0) /* { push(end,pop(beg)); draw_stack(beg,end,aux); } else*/ { tower(n-1,beg,end,aux); push(end,pop(beg)); draw_stack(beg,end,aux); tower(n-1,aux,beg,end); } // } void push(disc *tt,int x) { tt->val1[tt->top]=x; tt->top++; } pop(disc *tt) { int a; tt->top--; a=tt->val1[tt->top]; tt->val1[tt->top]=0; return a; } void draw_stack(disc *beg,disc *end,disc *aux) { int ypos=295,i,height=10,xpos; int ver=0; cleardevice(); setfillstyle(1,2); bar(20,300,580,310); bar(100,100,110,300); bar(300,100,310,300); bar(500,100,510,300); rectangle(20,300,580,310); rectangle(100,100,110,300); rectangle(300,100,310,300); rectangle(500,100,510,300); /* END TOWER*/ ypos=295; if(end->pos==1) xpos=BegPos; else if(end->pos==2) xpos=AuxPos; else if(end->pos==3) xpos=EndPos; for(i=0;i<end->top;i++) { setfillstyle(end->val1[i],end->val1[i]); bar(xpos-(end->val1[i]*width),ypos,xpos+(end->val1[i]*width),ypos-height); rectangle(xpos-(end->val1[i]*width),ypos,xpos+(end->val1[i]*width),ypos-height); ypos-=(height+2); } ver=end->pos; /* BEG TOWER*/ if(beg->pos==1) xpos=BegPos; else if(beg->pos==2) xpos=AuxPos; else if(beg->pos==3) xpos=EndPos; ypos=295; for(i=0;i<beg->top;i++) { setfillstyle(beg->val1[i],beg->val1[i]); bar(xpos-(beg->val1[i]*width),ypos,xpos+(beg->val1[i]*width),ypos-height); rectangle(xpos-(beg->val1[i]*width),ypos,xpos+(beg->val1[i]*width),ypos-height); ypos-=(height+2); } /* AUX TOWER*/ ver=ver*10+beg->pos; if(ver<20) { if(ver%10==2) xpos=EndPos; else xpos=AuxPos; } else if(ver<30) { if(ver%10==1) xpos=EndPos; else xpos=BegPos; } else if(ver<40) { if(ver%10==1) xpos=AuxPos; else xpos=BegPos; } ypos=295; for(i=0;i<aux->top;i++) { setfillstyle(aux->val1[i],aux->val1[i]); bar(xpos-(aux->val1[i]*width),ypos,xpos+(aux->val1[i]*width),ypos-height); rectangle(xpos-(aux->val1[i]*width),ypos,xpos+(aux->val1[i]*width),ypos-height); ypos-=(height+2); } getch(); }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=822


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review