/*-------------------------- 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>
#include<dos.h>
#include<string.h>
/*------------------------ GLOBAL DECLERATIONS -----------------------------*/
ofstream fout ;
int i,j,k;
int X[10] ;
int count ;
char* driver;
/*------------------------ FUNCTION DECLERATIONS ----------------------------*/
void InitializeScreen() ;
int TowerOfHanoi(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 Allowed
do
{
clrscr() ;
cout<<"enter the deriver name where you setup the TC++:";
cin>>driver;
cout<<endl;
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("Hanio.out") ;
// Recurrsevly Call The Transfer Function To Devise A Solution
cout<<"\n\tTotal No. Of Steps Required -> "<<(pow(2,n)-1)<<"\n\n" ;
TowerOfHanoi(n,'B','A','E');
cout<<"\n\nPress Any Key To View The Solution Graphically ...." ;
getch();
InitializeScreen() ;
fout.close() ;
// Display 'n' Disks Initially
Disk(n) ;
// Simulation Starts
StartSimulation(n) ;
getch();
}
/**************************************
** Function Which Finds The Solution **
***************************************/
int TowerOfHanoi(int n,char big,char aux,char end)
{
if(n==1)
{
cout<<"MOVE DISK "<<n<<" FROM "<<big<<" TO "<<end<<endl;
fout<<n<<big<<end<<endl ;
return 0;
}
TowerOfHanoi(n-1,big,end,aux);
cout<<"MOVE DISK "<<n<<" FROM "<<big<<" TO "<<end<<endl;
fout<<n<<big<<end<<endl ;
TowerOfHanoi(n-1,aux,big,end);
return 0;
}
/*****************************************
* Initialize Graphics And The Three Pegs *
******************************************/
void InitializeScreen()
{
int gDriver=DETECT,gMode ;
initgraph(&gDriver,&gMode,strcat(driver,":\\TC\\BGI")) ;
setcolor(WHITE) ;
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(RED) ;
outtextxy(90,50,"Big") ;
outtextxy(290,50,"Aux") ;
outtextxy(490,50,"End") ;
}
/***************
* DISPLAY DISKS *
****************/
void Disk(int n)
{
int x1,y1,x2,y2 ;
int k ;
setcolor(CYAN) ;
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("Hanio.out") ;
while(fin)
{
delay(1000);
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=='B' && to=='A')
{
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=='A' && to=='B')
{
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=='A' && to=='E')
{
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=='E' && to=='A')
{
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=='B' && to=='E')
{
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=='E' && to=='B')
{
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(CYAN) ;
rectangle(x1,y1,x1+length,y1+height) ;
// TO REMOVE THE LAST USELESS RECTANGLE
if(l==0 && c==0)
break ;
}
fin.close() ;
}
/************************ END OF TOWER OF HANOI ****************************/