Tower of Hanoi Program in C++

This program asks user for number of discs first. say n= 2 or 3 or any number. Then it will ask user to enter one option out of two.

1 If the user wants the solution of the Tower of Hanoi for disc he has entered.

or

2 If the user wants to play by himself and shuffle the discs between the pegs A B and C

And user will play till he moves all the discs from peg A to peg C and the peg B is auxiliary peg. The game continues till all the disc from peg A (Starting peg) to move peg C (Final peg).

CODE :

// TOWER OF HANOI

#include <iostream.h>
class hanoi{
public:
 int s[50],top,elem,i,count,count1;
 hanoi(){
 top=-1;
 count1=0;
 }
 void push(int e){
 ++top;
 s[top]=e;
 ++count1;
 }
 int pop(){
 int e;
 if(top==-1){
 cout<<"\npeg is empty\n";
 return 0;
 }
 else{
 e=s[top];
 --top;
 --count1;
 return e; 
 }
 }
 void display(){
 if(top==-1)
 cout<<"| "<<endl;
 else{
 cout<<"| ";
 for(i=0;i<=top;i++)
 cout<<s[i]<<"-";
 cout<<endl;
 }
 }
 void store(int n){
 count=n;
 for(i=0;i<n;i++){
 push(count);
 --count;
 }
 count1=n;
 }
};
void tower(int n,char frm,char aux,char to){
 if(n==1){
 cout<<"\tmov disc 1 from "<<frm<<" to "<<to<<endl;
 return;
 }
 else{
 tower(n-1,frm,to,aux);
 cout<<"\tmov disc "<<n<<" from "<<frm<<" to "<<to<<endl;
 tower(n-1,aux,frm,to);
 }
}
int main(){
 int n,op;
 cout<<"\n\t~~~~~~~~~TOWER OF HANOI~~~~~~~~~\n";
 cout<<"\nEnter no.of disc= ";
 cin>>n;
start:
 cout<<"\nPress 1 for solution OR 2 for playing\n";
 cin>>op;
 if(op>2 || op<1)
 goto start;
 else if(op==1)
 tower(n,'A','B','C');
 else{
 char fp,tp;
 int disc;
 bool flag1,flag2,flag3;
 hanoi a,b,c;
 a.store(n);
 cout<<"\nstarting peg is A\tFinishing peg is C\n";
 cout<<"\nA";a.display();
 cout<<"\nB";b.display();
 cout<<"\nC";c.display();
 while (c.count1!=n){
mid:
 cout<<"\nmov disc from peg= ";
 cin>>fp;
 cout<<"\nmov disc to peg= ";
 cin>>tp;
 flag1=flag2=flag3=false;
 switch(fp){
 case 'A':{disc=a.pop();
 flag1=true;
 if(disc==0)
 goto mid;
 break;
 }
 case 'B':{disc=b.pop();
 flag2=true;
 if(disc==0)
 goto mid;
 break;
 }
 case 'C':{disc=c.pop();
 flag3=true;
 if(disc==0)
 goto mid;
 break;
 }
 }
 switch(tp){
 case 'A':{if(disc>a.s[a.top] && a.top>-1){
 cout<<"\nInvalid move\n";
 if(flag1==true)
 a.push(disc);
 else if(flag2==true)
 b.push(disc);
 else if(flag3==true)
 c.push(disc);
 }
 else{
 a.push(disc); }
 break;
 }
 case 'B':{if(disc>b.s[b.top] && b.top>-1){
 cout<<"\nInvalid move\n";
 if(flag1==true)
 a.push(disc);
 else if(flag2==true)
 b.push(disc);
 else if(flag3==true)
 c.push(disc);
 }
 else{
 b.push(disc); }
 break;
 }
 case 'C':{if(disc>c.s[c.top] && c.top>-1){
 cout<<"\nInvalid move\n";
 if(flag1==true)
 a.push(disc);
 else if(flag2==true)
 b.push(disc);
 else if(flag3==true)
 c.push(disc);
 }
 else{
 c.push(disc); }
 break;
 }
 }
 cout<<"\nA";a.display();
 cout<<"\nB";b.display();
 cout<<"\nC";c.display();
 }
 if(c.count1==n)
 cout<<"\n\t\t~~~~~YOU WON!!~~~~~\n";
 }
 return 0;
}
Tower of Hanoi Sample output 1

Tower of Hanoi Sample output 1

Tower of Hanoi Sample Output 2

Tower of Hanoi Sample Output 2

Leave a comment