Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Computer GraphicsRSS Feeds

Program that displays graphical representation of tower of hanoi

Posted By: Easy Tutor     Category: C++ Programming     Views: 8474

Write a program that displays graphical representation of tower of hanoi.

Code for Program that displays graphical representation of tower of hanoi in C++ Programming

/*--------------------------  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() ;
}
  
Share: 

 
 


Easy Tutor
Easy Tutor author of Program that displays graphical representation of tower of hanoi is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!