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

Home » C Home » Beginners / Lab Assignments Home » Tower of Hanoi

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

Search Projects & Source Codes:

Title Tower of Hanoi
Author Shan Justus
Author Email shan_j2005 [at] yahoo.com
Description
Category C » Beginners / Lab Assignments
Hits 369280
Code Select and Copy the Code
Code : // Tower of Hanoi Written by SHAN JUSTUS // Msc(Computer Science), Kariavattom, Trivandrum, Kerala // Window Xp pattern #include<stdio.h> #include<graphics.h> #include<conio.h> int rad=25; const int xwidth=50; int xcord[10],ycord[10],radius[10],colr[10]; int sx=100,dx=500,ax=300; int sy=400,dy=400,ay=400,ssy=400; void drawStick(int,int,int); void main() { int driver,mode; char source='A',dest='B',aux='C'; void hanoi(int,char,char,char),drawDisks(int); int n,i; driver=DETECT; initgraph(&driver,&mode,"\tc\bgi"); cleardevice(); printf(" Enter the no.of Disks: "); scanf("%d",&n); cleardevice(); outtextxy(200,0,"SIMULATION OF TOWER OF HANOI"); outtextxy(200,8,"========== == ===== == ====="); for(i=n;i>0;i--) { colr[i]=i; xcord[i]=sx; ycord[i]=sy; radius[i]=rad; sy=sy-20;rad=rad-5; } sy=ycord[1];dy=400;ay=400; drawDisks(n); drawStick(sx,200,sy); drawStick(ax,200,ay); drawStick(dx,200,dy); hanoi(n,source,dest,aux); printf(" Press a Key to Exit"); getch(); } void drawStick(int lt,int tp,int btm) { setfillstyle(1,WHITE); bar3d(lt,tp,lt+5,btm,0,0); } void hanoi(int n,char source,char dest,char aux) { void moveDown(int,int),moveUp(int),moveRight(int,int),moveLeft(int,int); if(n==0) return; hanoi(n-1,source,aux,dest); delay(1000); if((source=='A' && dest=='B') || (source=='C' && dest=='B')) { if(source=='A') sy=sy+20; if(source=='C') ay=ay+20; moveUp(n); moveRight(n,dx); moveDown(n,dy); xcord[n]=dx; ycord[n]=dy; dy=dy-20; } else if((source=='A' && dest=='C') || (source=='B' && dest=='C')) { if(source=='A') { sy=sy+20; moveUp(n); moveRight(n,ax); moveDown(n,ay); } else if(source=='B') { dy=dy+20; moveUp(n); moveLeft(n,ax); moveDown(n,ay); } xcord[n]=ax; ycord[n]=ay; ay=ay-20; } else if((source=='B' && dest=='A') || (source=='C' && dest=='A')) { if(source=='B') dy=dy+20; if(source=='C') ay=ay+20; moveUp(n); moveLeft(n,sx); moveDown(n,sy); xcord[n]=sx; ycord[n]=sy; sy=sy-20; } hanoi(n-1,aux,dest,source); } void moveRight(int n,int destx) { int i,j; for(i=xcord[n];i<=destx;i=i+5) { setcolor(BLACK); for(j=radius[n];j>=0;j--) { ellipse(i,200,0,360,j+xwidth,j); ellipse(i,205,0,360,j+xwidth,j); } setcolor(colr[n]); for(j=radius[n];j>=0;j--) ellipse(i+5,200,0,360,j+xwidth,j); }xcord[n]=i; } void moveLeft(int n,int destx) { int i=xcord[n],j; for(i=xcord[n];i>destx+5;i=i-5) { setcolor(BLACK); for(j=radius[n];j>=0;j--) { ellipse(i,200,0,360,j+xwidth,j); ellipse(i,205,0,360,j+xwidth,j); } setcolor(colr[n]); for(j=radius[n];j>=0;j--) ellipse(i-5,200,0,360,j+xwidth,j); }xcord[n]=i; } void moveUp(int n) { int desty=ycord[n],j; for(desty=ycord[n];desty>=200;desty=desty-5) { setcolor(BLACK); for(j=radius[n];j>=0;j--) { ellipse(xcord[n],desty+5,0,360,j+xwidth,j); ellipse(xcord[n],desty,0,360,j+xwidth,j); } setcolor(colr[n]); for(j=radius[n];j>=0;j--) ellipse(xcord[n],desty,0,360,j+xwidth,j); drawStick(xcord[n],desty,ssy); } ycord[n]=desty; } void moveDown(int n,int y) { int desty=200,j; for(desty=200;desty<=y;desty=desty+5) { setcolor(BLACK); for(j=radius[n];j>=0;j--) { ellipse(xcord[n],desty+5,0,360,j+xwidth,j); ellipse(xcord[n],desty,0,360,j+xwidth,j); } setcolor(colr[n]); for(j=radius[n];j>=0;j--) ellipse(xcord[n],desty+5,0,360,j+xwidth,j); drawStick(xcord[n],ycord[n],ssy); } setcolor(WHITE); for(j=radius[n];j>=0;j--) ellipse(xcord[n],desty+5,0,360,j+xwidth,j); setcolor(colr[n]); for(j=radius[n];j>=0;j--) ellipse(xcord[n],desty,0,360,j+xwidth,j); ycord[n]=desty; } void drawDisks(int n) { int i,j; for(i=n;i>0;i--) { setcolor(WHITE); for(j=radius[i];j>=0;j--) ellipse(xcord[i],ycord[i]+5,0,360,j+xwidth,j); setcolor(colr[i]); for(j=radius[i];j>=0;j--) ellipse(xcord[i],ycord[i],0,360,j+xwidth,j); } }

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi ¦Õ¤ ?¤Ð Ãß??ÝÐÝ
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

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=1053


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