Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with tower of hanoi puzzle, I need help creating a switch statement that prompts users for the moves thy want and moves elements

Please help with tower of hanoi puzzle, I need help creating a switch statement that prompts users for the moves thy want and moves elements between stacks to solve the puzzle. Stack::Stack(){
top =-1;
for (int i =0; i <4; i++){
arr[i]=0;
}
}
Stack::~Stack(){}
bool Stack::isEmpty(){
if(top ==-1){
cout <<" is empty" << endl;
return true;
}
else{
cout <<" is not empty." << endl;
return false;
}
}
bool Stack::isFull(){
if(top ==3){
cout <<" is full." << endl;
return true;
}
else{
cout <<"is not full." << endl;
return false;
}
}
void Stack::push(int val){
if (isFull()){
cout << "Overflow" << endl;
} else {
top++;
arr[top]= val;
}
}
int Stack::pop(){
if (isEmpty()){
cout << "Stack underflow" << endl;
return 0;
} else {
int popValue = arr[top];
arr[top]=0;
top--;
return popValue;
}
}
int Stack::count(){
return (top +1);
}
int Stack::peek(int pos){
if (isEmpty()){
cout << "Stack underflow" << endl;
return 0;
} else {
return arr[pos];
}
}
void Stack::change(int pos, int val){
arr[pos]= val;
cout << "Value changed at position "<< pos << endl;
}
void Stack::display(){
cout << "All items in stack are:" << endl;
for (int i = top; i >=0; i--){
cout << arr[i]<< endl;
}
}
{
if (disks ==1){
int disk = source.pop();
destination.push(disk);
cout << "Move disk "<< disk <<" from Tower "<< &source <<" to Tower "<< &destination << endl;
return;
}
towerOfHanoi(disks -1, source, destination, auxiliary);
int disk = source.pop();
destination.push(disk);
cout << "Move disk "<< disk <<" from Tower "<< &source <<" to Tower "<< &destination << endl;
towerOfHanoi(disks -1, auxiliary, source, destination);
}
int main(){
int numDisks =3; // Change the number of disks here
Stack towerA, towerB, towerC;
char answer;
int option, choice;
cout << "Here are the options for sloving the Tower of Hanoi: "<< endl;
cout <<"1. Self solving program. "<< endl;
cout <<"2. Try and solve the puzzle yourself. "<< endl;
cin >> choice;
switch(choice){
case 1:
// Push disks onto the first tower (Tower A)
for (int i = numDisks; i >0; i--){
towerA.push(i);
}
cout << "Initial configuration:" << endl;
towerA.display(); // Display the initial configuration
// Solve Tower of Hanoi
towerOfHanoi(numDisks, towerA, towerB, towerC);
cout << "Final configuration:" << endl;
towerC.display(); // Display the final configuration in the destination tower
break;
case 2:
//Help here moving discs between towers as directed by the user
cout << "what move will you make? "<< endl;
cout <<"1. move disc from Tower A to Tower B"<< endl;
cout <<"2. move disc From Tower A to Tower C"<< endl;
cin >> choice;
switch(choice){
case 1:
break;
case 2:
break;
}
break;
}
while(option !=0);
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

How does national culture relate to organizational culture?

Answered: 1 week ago