/*-------------------------- INCLUDE FILES -------------------------------*/
#include<iostream.h>
#include<graphics.h>
#include<process.h>
#include<fstream.h>
#include<process.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<io.h>
#define ESC 27
/*------------------------ GLOBAL DECLERATIONS -----------------------------*/
ofstream fout ;
int i,j,k;
int X[10] ;
int count ;
/*------------------------ FUNCTION DECLERATIONS ----------------------------*/void InitializeScreen() ;
void transfer(int,char,char,char) ;
void StartSimulation(int) ;
void Disk(int) ;
/*----------------------------- MAIN DADDY -------------------------------*/void main()
{
int n;
// To Keep The Graphical Solution Neat Only Upto 9 Disks Are Alloweddo
{
clrscr() ;
cout<<endl<<"ENTER THE NO OF DISKS : ";
cin>>n ;
if(n>9 || n<1)
{
cout<<"\nILLEGAL VALUE FOR No. OF DISKS ENTER BETWEEN 1-10" ;
getch() ;
}
}
while(n>9 || n<1) ;
/* Store The Solution In A File First Note: Could Have Used An Array Or Structure. */
fout.open("database.dat") ;
// Recurrsevly Call The Transfer Function To Devise A Solution
cout<<"\n\nS---O---L---U---T---I---O---N\n\n" ;
cout<<"\n\tTotal No. Of Steps Required -> "<<(pow(2,n)-1)<<"\n\n" ;
transfer(n,'L','R','C');
cout<<"\n\nPress Any Key To View The Solution Graphically ...." ;
cout<<"\n\n\ (Hey! Would Somebody Tell Me Where To Find The Key Named 'ANY' On My Keyboard)" ;
getch();
InitializeScreen() ;
fout.close() ;
// Display 'n' Disks Initially
Disk(n) ;
// Fasten Ur Seat Belts. Simulation Starts
StartSimulation(n) ;
getch();
}
/**************************************** Function Which Finds The Solution *****************************************/void transfer(int n,char from,char to,char temp)
{
if(n>0)
{
transfer(n-1,from,temp,to);
count++ ;
if(count>24)
{
cout<<"\n\tPress Any Key To Continue...\n\n" ;
getch() ;
count=0 ;
}
cout<<"MOVE DISK "<<n<<" FROM "<<from<<" TO "<<to<<endl;
fout<<n<<from<<to<<endl ;
transfer(n-1,temp,to,from);
}
}
/****************************************** Initialize Graphics And The Three Pegs *******************************************/void InitializeScreen()
{
int gDriver=DETECT,gMode ;
initgraph(&gDriver,&gMode," ") ;
setcolor(LIGHTGREEN) ;
line(10,475,600,475) ;
line(100,470,100,100) ;
line(300,470,300,100) ;
line(500,470,500,100) ;
settextstyle(TRIPLEX_FONT,HORIZ_DIR,4) ;
setcolor(YELLOW) ;
outtextxy(90,50,"L") ;
outtextxy(290,50,"C") ;
outtextxy(490,50,"R") ;
}
/**************** DISPLAY DISKS *****************/void Disk(int n)
{
int x1,y1,x2,y2 ;
int k ;
setcolor(MAGENTA) ;
for(i=1,k=n,x1=5,x2=x1+190,y1=470,y2=y1-20 ; i<=n ; i++,x1+=10,x2-=10,y1-=20,y2-=20,k--)
{
rectangle(x1,y1,x2,y2) ;
X[k]=x1 ;
}
}
/**************************************************************** SHOW SOLUTION STEP BY STEP GRAPHICALLY AS USER PRESSES A KEY *****************************************************************/void StartSimulation(int n)
{
char charac[3] ;
char from,to ;
char ch ;
int disk ;
int l,r,c ;
int x1,y1 ;
int length ;
int height=20 ;
l=n ;
r=0 ;
c=0 ;
ifstream fin ;
fin.open("database.dat") ;
while(fin)
{
ch=getch() ;
if(ch==ESC)
exit(0) ;
fin>>charac ;
disk=(int)charac[0] ;
disk-=48 ;
from=charac[1] ;
to=charac[2] ;
x1=X[disk] ;
gotoxy(2,2) ;
cout<<"Move Disk "<<disk<<" From "<<from<<" TO "<<to ;
if(from=='L' && to=='C')
{
length=2*(100-x1) ;
y1=470-l*20 ;
setcolor(BLACK) ;
rectangle(x1,y1,x1+length,y1+height) ;
l-- ;
c++ ;
x1=x1+200 ;
X[disk]=X[disk]+200 ;
y1=470-c*20 ;
}
if(from=='C' && to=='L')
{
length=2*(300-x1) ;
y1=470-c*20 ;
setcolor(BLACK) ;
rectangle(x1,y1,x1+length,y1+height) ;
c-- ;
l++ ;
x1=x1-200 ;
X[disk]=X[disk]-200 ;
y1=470-l*20 ;
}
if(from=='C' && to=='R')
{
length=2*(300-x1) ;
y1=470-c*20 ;
setcolor(BLACK) ;
rectangle(x1,y1,x1+length,y1+height) ;
r++ ;
c-- ;
x1=x1+200 ;
X[disk]=X[disk]+200 ;
y1=470-r*20 ;
}
if(from=='R' && to=='C')
{
length=2*(500-x1) ;
y1=470-r*20 ;
setcolor(BLACK) ;
rectangle(x1,y1,x1+length,y1+height) ;
r-- ;
c++ ;
x1=x1-200 ;
X[disk]=X[disk]-200 ;
y1=470-c*20 ;
}
if(from=='L' && to=='R')
{
length=2*(100-x1) ;
y1=470-l*20 ;
setcolor(BLACK) ;
rectangle(x1,y1,x1+length,y1+height) ;
l-- ;
r++ ;
x1=x1+400 ;
X[disk]=X[disk]+400 ;
y1=470-r*20 ;
}
if(from=='R' && to=='L')
{
length=2*(500-x1) ;
y1=470-r*20 ;
setcolor(BLACK) ;
rectangle(x1,y1,x1+length,y1+height) ;
r-- ;
l++ ;
x1=x1-400 ;
X[disk]=X[disk]-400 ;
y1=470-l*20 ;
}
setcolor(MAGENTA) ;
rectangle(x1,y1,x1+length,y1+height) ;
// TO REMOVE THE LAST USELESS RECTANGLEif(l==0 && c==0)
break ;
}
fin.close() ;
}