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);
}
}