-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtowerOfHonoi.cpp
More file actions
263 lines (248 loc) · 5.02 KB
/
towerOfHonoi.cpp
File metadata and controls
263 lines (248 loc) · 5.02 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include<iostream>
using namespace std;
class node
{
public:
node *next;
int data;
node()
{
next=NULL;
}
};
class ADT
{
public:
node *top;
int count=-1;
ADT()
{
top = NULL;
}
void push(int element)
{
if(isfull()==true)
{
return;
}
if(top==NULL)
{
node *new_node = new node;
new_node->data=element;
top = new_node;
new_node->next=NULL;
count++;
}
else
{
node *new_node = new node;
new_node->data = element;
new_node->next = top;
top = new_node;
count++;
}
}
bool isempty()
{
if(top==NULL)
{
return true;
}
return false;
}
void display()
{
node *current = top;
while(current!=NULL)
{
cout<<current->data<<endl;
current = current->next;
}
}
int pop()
{
int var;
if(top==NULL)
{
cout<<"stack is underflow"<<endl;
}
else
{
var = top->data;
node *p = top;
top = top->next;
delete p;
p=NULL;
count--;
}
return var;
}
int Top()
{
int var ;
if(top==NULL)
{
return -1;
}
var = top->data;
return var;
}
bool isfull()
{
if(count==4)
{
return true;
}
return false;
}
};
void diskmoves(ADT &l1 , ADT &l2)
{
if(l1.isempty()!=true && l2.isfull()!=true)
{
if(l2.isempty()==true)
{
l2.push(l1.Top());
l1.pop();
}
else
{
if(l1.Top()<l2.Top() && l1.Top()!=-1)
{
l2.push(l1.Top());
l1.pop();
}
}
}
else
{
return;
}
}
void display_if(ADT &l1 , ADT &l2 , ADT &l3)
{
cout<<endl;
cout<<"first cupboard:"<<endl;
l1.display();
cout<<endl;
cout<<"second cupboard:"<<endl;
l2.display();
cout<<endl;
cout<<"third cupboard:"<<endl;
l3.display();
cout<<endl;
}
bool game_solve(ADT &l1)
{
if(l1.isfull()==true)
{
return true;
}
return false;
}
int min_moves_to_win()
{
int count = 5;
int n = 1;
for(int i=0;i<count;i++)
{
n = n * 2;
}
n = n -1;
return n;
}
// check that
void initialize(ADT &l1,ADT &l2,ADT &l3)
{
for(int i=5;i>0;i--)
{
l1.push(i);
}
}
int main()
{
ADT l1;
int count1 = 0;
ADT l2,l3;
int choice = 0;
int mn = 0;
cout<<"-----------------------------"<<endl;
cout<<"||||| THE TOWER OF HENOI |||||"<<endl;
cout<<"-----------------------------"<<endl;
cout<<"1.playing game"<<endl;
cout<<"2.exit the game"<<endl;
cin>>choice;
if(choice==1)
{
initialize(l1,l2,l3);
while(true)
{
display_if(l1, l2, l3);
cout<<"1. move from 1 to 2"<<endl;
cout<<"2. move from 1 to 3"<<endl;
cout<<"3. move from 2 to 1"<<endl;
cout<<"4. move from 2 to 3"<<endl;
cout<<"5. move from 3 to 1"<<endl;
cout<<"6. move from 3 to 2"<<endl;
cin>>mn;
if(mn==1)
{
diskmoves(l1, l2);
count1++;
}
else if(mn==2)
{
diskmoves(l1, l3);
count1++;
}
else if(mn==3)
{
diskmoves(l2, l1);
count1++;
}
else if(mn==4)
{
diskmoves(l2, l3);
count1++;
}
else if(mn==5)
{
diskmoves(l3, l1);
count1++;
}
else if(mn==6)
{
diskmoves(l3, l2);
count1++;
}
else
{
cout<<"you enter the wrong number"<<endl;
}
if(game_solve(l3)==true)
{
cout<<"solve game successfully"<<endl;
display_if(l1, l2, l3);
int mov = min_moves_to_win();
cout<<endl;
cout<<"minimum moves to win:"<<mov<<endl;
if(count1==mov)
{
cout<<"your moves:"<<count1<<endl;
cout<<"you complete the game in minimum number of moves"<<endl;
}
else
{
cout<<"your moves:"<<count1<<endl;
cout<<"you cannot win the game in using minimum number of moves"<<endl;
}
exit(1);
}
}
}
else
{
return 0;
}
return 0;
}