Thread: Little Problem on Presentation in the Tower of Hanoi

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    Post Little Problem on Presentation in the Tower of Hanoi

    HI!!!

    I have this program made Running and working well, but there is a little problem on the presentation at the middle of the program. It is made on Dev++. You Can change where says: double size = 3;
    to any other number so you could see better the problem. It is good seeing it with 5. Here is the Code. Thanks for the help you could give me.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <vector>
    #include <cassert>
    #include <iomanip>
    #include <cmath>
    #include <string>
    
    using namespace std;
    
    void init(vector<double> &array1, vector<double> &array2, vector<double> &array3, 
              int &top, double &count, double &size);
    void print(vector<double> &array1, vector<double> &array2, vector<double> &array3,
    	      int &top, double &count, double &size);
    void waitForUser(); //Function for stopping the program in some part
    void move(int from, int to, vector<double> &array1, vector<double> &array2, 
              vector<double> &array3);
    void Newtop(vector<double> &array1, vector<double> &array2, 
              vector<double> &array3, int &top);
    void hanoi( int a, int b, int c, int amount, vector<double> &array1, vector<double> &array2, 
                vector<double> &array3, int &top, double &count, double &size);
    
    
    
    int main(int argc, char *argv[]){
    
       	double size = 3;
        double count;
    	int top, from, to, a=1, b=2, c=3, amount=size;
    	vector<double> array1;
        vector<double> array2;
    	vector<double> array3;
    	
    
    	
    	init(array1, array2, array3, top, count, size);
    	print(array1, array2, array3, top, count, size);
       // move(from, to, array1, array2, array3);
       // Newtop(array1, array2, array3, top);
    	hanoi(a, b, c, amount, array1, array2, array3, top, count, size);
    	
    	
    	system("pause");
    	return 0;
    	
    }
    void init(vector<double> &array1, vector<double> &array2, vector<double> &array3, 
              int &top, double &count, double &size){
        for( double r=size-1; r >= 0; r-- ){  
            array1.push_back(r+1);   
        } 
        for( double r=size-1; r >= 0; r-- ){  
            array2.push_back(0);   
        }  
        for( double r=size-1; r >= 0; r-- ){  
            array3.push_back(0);  
        }
        for( double r=size-1; r >= 0; r-- ){  
            array2.pop_back();  
        }
        for( double r=size-1; r >= 0; r-- ){  
            array3.pop_back();  
        }   
            
        top = array1.size();
        count = 0;
    
    }
    
    void print(vector<double> &array1, vector<double> &array2, vector<double> &array3,
    	      int &top, double &count, double &size){
      cout << "**** HANOI TOWER **** " << endl;
      cout << "N=" << size << "          STEP "  << count << "\n" << endl;  
       for( int r = top-1; r >= 0 ; r-- ){
          if( array1.size() != 0 ){
            if ( (array1[r] >= 1) && (array1[r] <= size ) ) 
             cout << "  " << array1[r] << "      " ;
            else
             cout << "         "; 
          }  
          else
             cout << "         "; 
          if( array2.size() != 0 ){
            if ( (array2[r] >= 1 ) && (array2[r] <= size) ) 
             cout << array2[r] << "      " ;
            else
             cout << "       "; 
          }
          else
            cout << "       ";  
          if( array3.size() != 0 ){
            if ( (array3[r] >= 1 ) && (array3[r] <= size) ) 
             cout << array3[r] ;
          }   
          cout << endl;    
       }      
       cout << "=====  =====  =====  " << endl;
       cout << "  A      B      C    " << endl;
       if ( count == (pow( 2, size ) - 1) )
          cout << "PRESS <ENTER> TO FINISH" << endl;
       else   
          cout << "PRESS <ENTER>" << endl;
       cout << endl;
       waitForUser();
       //system("clear");
      
    }
    void waitForUser() {
      // waits for the user to hit <ENTER>
      string dummy;
      getline(cin, dummy);
    }
    void move(int from, int to, vector<double> &array1, vector<double> &array2, 
              vector<double> &array3){
    
    	if ( (from == 1) && (to == 2) ){
            array2.push_back( array1[array1.size()-1] );
            array1[array1.size()-1] = 10;
    	    array1.pop_back();
    	}
        else if ( (from == 1) && (to == 3) ){
    	    array3.push_back( array1[array1.size()-1] );
    	    array1[array1.size()-1] = 10;
            array1.pop_back();
        }
        else if ( (from == 2) && (to == 3) ){
    		array3.push_back( array2[array2.size()-1] );
    	    array2[array2.size()-1] = 10;
            array2.pop_back();
    	}
        else if ( (from == 3) && (to == 1) ){
    		array1.push_back( array3[array3.size()-1] );
    	    array3[array3.size()-1] = 10;
            array3.pop_back();
    	}
        else if ( (from == 3) && (to == 2) ){
    		array2.push_back( array3[array3.size()-1] );
    	    array3[array3.size()-1] = 10;
            array3.pop_back();
    	}
        else if ( (from == 2) && (to == 1) ){
    		array1.push_back( array2[array2.size()-1] );
    	    array2[array2.size()-1] = 10;
            array2.pop_back();
    	}
    }
    void Newtop(vector<double> &array1, vector<double> &array2, 
              vector<double> &array3, int &top){
       if ( array1.size() > array2.size() )
          top = array1.size();
       else
          top = array2.size();   
       if ( array3.size() > array2.size() )
          top = array3.size();
       else
          top = array2.size();
       if ( array3.size() > array1.size() )
          top = array3.size();
       else
          top = array1.size();      
    }
    void hanoi( int a, int b, int c, int amount, vector<double> &array1, vector<double> &array2, 
              vector<double> &array3, int &top, double &count, double &size){
       if ( amount > 0 ){
          hanoi( a, c, b , amount-1, array1, array2, array3, top, count, size);
          move(a,c, array1, array2, array3);
          count++;
      //    move(a,c, array1, array2, array3);
          Newtop(array1, array2, array3, top);
          print(array1, array2, array3, top, count, size); 
            
          hanoi( b, a, c, amount-1, array1, array2, array3, top, count, size);
        
        }
    }

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    I dont get it. what is that you want us to do.. I had built the tower of hanoi using borland graphics.. the user had to change discs from one poele to another in least number of moves.. do u want to see it.. I used three stacks to keep track..

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    I am just doing a project for a class of c++. This project is made without grafics...Just Run it and see how it works. Depending of the number the user puts when running the program is the quantity of "disks" (shown as numbers) that the game will be. The game is only played by the computer (by the user seeing the game played pressing enter) . In this case the Program is not terminated because It will be finally be run on linux/unix system..running the game with a paramater that will be the number of "disks" between 1 and 9. I will add it on Linux.

    Please help me with the problem I have on the presentation...!!! Thanks....

  4. #4
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    you can use recursion:

    Code:
    void solve(char source, char temp, char dest, int n)
    {
    	if (n != 0)
    	{
    		solve(source,dest,temp,n-1);
    		cout << "move disk " << n << " from peg "
    			 << source << " to peg " << dest << endl;
    		solve(temp,source,dest,n-1);
    	}
    }
    
    void main()
    {
    	int n;
    	cout << "# of disks [1-8]: ";
    	cin >> n;
    	cout << endl;
    	solve('A','B','C',n);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Tower of Hanoi
    By dmkanz07 in forum C Programming
    Replies: 13
    Last Post: 03-29-2007, 12:37 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM