tower of hanoi using stack

hello there i am making a game tower of hanoi using stack class not build in but user define in this program i have to get inputs from user to move the disks but i am confused how to do it plz help heres the code

header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once
#ifndef STACK_H
#define STACK_H


class stack
{

    private:
    int MAX, TOP;
    public:
    int *A;
    public:
    stack();
    void init();
    int isempty();
    int isfull();
    void push(int );
    void pop(int);
};

#endif // STACK_H 


class cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "StdAfx.h"
#include "stack.h"
#include <iostream>

using namespace std;

stack::stack()
{
    MAX=10;
    TOP =0;
    A =new int(MAX);
}
void stack::init()
{
    TOP = 0;
}
int stack::isempty()
{
    return (!TOP);
}
int stack::isfull()
{
    return (TOP==(MAX-1));
}
void stack::push(int x)
{
    A[TOP++] = x;
    cout<<"   "<<x<<"\n";
}
void stack::pop(int x)
{
     x= A[--TOP];
}




main body

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// stacks.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include<conio.h>
#include "stack.h"
using namespace std;
//------------------------Globals--------------------------------------------------------
int count= 0; //Counter to display the number of moves.


//---------------------------------------------------------------------------------------
void move( stack ,stack );
void move2( stack ,stack ) ;
void move3( stack ,stack ) ;
void move4( stack ,stack ) ;
void move5( stack ,stack ) ;
void move6( stack ,stack ) ;
int main()
{


    stack source;// create 3 empty stacks
    stack target;
    stack aux;
    char ss;
    char ds;
	

    int N;// Number of discs.

     cout<<"\n**********************************************************\n";
 cout<<"This C++ program is to solve the towers of hanoi problem";
 cout<<"\n**********************************************************\n";
 cout<<"Enter the no. of disks ";

    cin>>N;//Input number of disks
    cout<<endl;
    while(N<0)//to make sure that the user didnot enter an invalid number
        {
            cout<<"The number you entered is invalid...Please reneter!"<<endl;
            cin>>N;
            cout<<endl;
        }

    for(int i=1; i<=N; i++)//Fill the source peg with the number of discs.
    {
        source.push(i);
    }
       cout<<"\nsource stack "<<endl;
       if(aux.isempty())
       {
           cout<<" \t\tintermediate stack "<<endl;
       }
       if(target.isempty())
       {
           cout<<" \t\t\t\t\ttarget stack "<<endl;
       }
while(count==0)
{
cout<<"\nenter source stack a, b or c"<<endl;
cin>>ss;
cout<<"\nenter destination stack a, b or c"<<endl;
cin>>ds;

if(ss=='a'&&ds=='c')
{

    move3(source , target);
    // element that will be removed from one stack and added to the other
	
    //source.pop(N-1);//pop from source

    //target.push(N);//and move to target
	 
	

}
else if(ss=='a'&&ds=='b')
{
    move(source, aux);
    //source.pop(N-1);//pop from source

    //aux.push(N);//and move to target
	

}
else if(ss=='b'&&ds=='c')
{
    move2(aux, target);

    //aux.pop(N-1);//pop from source

    //target.push(N);//and move to target
	
}
else if(ss=='b'&&ds=='a')
{
    move5(aux, source);

    //aux.pop(N-1);//pop from source

    //source.push(N);//and move to target
	 

}else if(ss=='c'&&ds=='b')
{
    move6(target, aux);

    //target.pop(N-1);//pop from source

    //aux.push(N);//and move to target
	 

}else if(ss=='c'&&ds=='a')
{
    move4(target, source);

    //target.pop(N-1);//pop from source

    //source.push(N);//and move to target
	


}
if(target.isfull()){
count=1;
}
else if(aux.isfull()){
count=2;}
//system("cls");
}
if(count==1){
cout<<"you  win";
}
else if(count==2){
cout<<"try again";
}
getch();
return 0;
}



void move( stack s,stack t) //This will move an element from first stack to the second stack
{
    int e; // element that will be removed from one stack and added to the other

    s.pop(e);//pop from source

    t.push(e);//and move to target

	cout<<e<<endl;

}
void move2( stack t,stack d) //This will move an element from first stack to the second stack
{
    int e; // element that will be removed from one stack and added to the other

    t.pop(e);//pop from source

    d.push(e);//and move to target
	cout<<e<<endl;

}
void move3( stack s,stack d) //This will move an element from first stack to the second stack
{
    int e; // element that will be removed from one stack and added to the other

    s.pop(e);//pop from source

    d.push(e);//and move to target
	cout<<e<<endl;
}
void move4( stack d,stack s) //This will move an element from first stack to the second stack
{
    int e; // element that will be removed from one stack and added to the other

    d.pop(e);//pop from source

    s.push(e);//and move to target
	cout<<e<<endl;
}
void move5( stack t,stack s) //This will move an element from first stack to the second stack
{
    int e; // element that will be removed from one stack and added to the other

    t.pop(e);//pop from source

    s.push(e);//and move to target
	cout<<e<<endl;
}
void move6( stack d,stack t) //This will move an element from first stack to the second stack
{
    int e; // element that will be removed from one stack and added to the other

    d.pop(e);//pop from source

    t.push(e);//and move to target
	cout<<e<<endl;
}


first: you need a recursive approach to solve that problem
second: void move( stack s,stack t) provides copies of the stack, i.e. your modification to s and t won't have any effect outside of the function move()

hint: when a disk can't be stacked due to a smaller disk you need to do the same move for that smaller disk just with exchanged target and source. After this you put the smaller disk back again (it's only about what's target and what's source)
I don't think it's a Hanoi Solver; I think it's a Hanoi game. The program just simulates the game, but the user has to solve it by inputting commands.

Regardless of the case, anything but the first point is valid either way.
@Gaminic
This C++ program is to solve the towers of hanoi problem


what i described was a single move done by the computer. I'd expect that'd be done anyway.

@mehreenh
All move2...6 doing the same like move(). Now why?
Last edited on
i know about the problem solver of hanoi but not how to make it play able by the user
if the user is supposed to do all the moves you just have to check whether the move is legal. if so do the move (pop and push is that simple that you don't need function for that) else tell the user he can't do that.

This
1
2
3
4
void stack::pop(int x)
{
     x= A[--TOP];
}
has no effect regarding x. if you'd return the popped value, like so
1
2
3
4
5
6
int stack::pop()
{
     int x= A[TOP];
     --TOP;
     return x;
}
you could write:
1
2
if(ss=='a'&&ds=='c')
     target.push(source.pop()); // maybe you want that move() function for validation/output 


You just have to care that everything is legal (e.g. source must not be empty)
thank u i've made it :)
Topic archived. No new replies allowed.