Question

In: Computer Science

Implement an iterator that produces the moves for the Towers of Hanoi puzzle described in Worked...

Implement an iterator that produces the moves for the Towers of Hanoi puzzle described in Worked Example 11.2. Provide functions has_more_moves and next_move. The next_move function should yield a string describing the next move. For example, the following code prints all moves needed to move five disks from peg 1 to peg 3:

DiskMover mover(5, 1, 3);
while (mover.has_more_moves())
{
   cout << mover.next_move() << endl;
}

Hint: A disk mover that moves a single disk from one peg to another simply has a next_move function that returns a string

Move disk from peg source to target

A disk mover with more than one disk to move must work harder. It needs another DiskMover to help it move the first d – 1 disks. Then next_move asks that disk mover for its next move until it is done. Then the next_move function issues a command to move the dth disk. Finally, it constructs another disk mover that generates the remaining moves.

It helps to keep track of the state of the disk mover:

•BEFORE_LARGEST: A helper mover moves the smaller pile to the other peg.

•LARGEST: Move the largest disk from the source to the destination.

•AFTER_LARGEST: The helper mover moves the smaller pile from the other peg to the target.

•DONE: All moves are done.

Solutions

Expert Solution

HELLO ADDING CODE IN C++ FOR YOUR REQUIREMENT

PLEASE GO THROUGH IT ONCE
THANK YOU

#include <bits/stdc++.h> 
#include <string>
using namespace std; 
  
string mover(int n,int from_rod,int to_rod)
{
    return  "Move disk " + std::to_string(n) + " from rod " + std::to_string(from_rod) + " to rod " + std::to_string(to_rod);
}

void towerOfHanoi(int n, int from_rod,int to_rod,int aux_rod)  
{  
    
    if (n == 1)  
    {  
        cout << "Move disk 1 from rod " << from_rod <<  
                            " to rod " << to_rod<<endl;  
        return;  
    }  
    towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);  
                                
    cout << mover(n,from_rod,to_rod)<<endl;
    towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);  
}  
 

// Driver code 
int main()  
{  
    int n = 5; // Number of disks  
    towerOfHanoi(n, 1, 3, 2); // 1, 3 and 2 are names of rods  
    return 0;  
} 

OUTPUT


Related Solutions

There is a famous puzzle called the Towers of Hanoi that consists of three pegs and...
There is a famous puzzle called the Towers of Hanoi that consists of three pegs and n circular disks, all of different sizes. The disks start on the leftmost peg, with the largest disk on the bottom, the second largest on top of it, and so on, up to the smallest disk on top. The goal is to move the disks so that they are stacked in this same order on the rightmost peg. However, you are allowed to move...
Describe an algorithm to solve the variant of the Towers of Hanoi in as few moves...
Describe an algorithm to solve the variant of the Towers of Hanoi in as few moves as possible. Prove that your algorithm is correct. Initially, all the n disks are on peg 1, and you need to move the disks to peg 2. You are not allowed to put a bigger disk on top of a smaller disk. 1. Suppose you are forbidden to move any disk directly between peg 1 and peg 2, and every move must involve (the...
How many moves are required to solve the Towers of Hanoi problem when we have 3...
How many moves are required to solve the Towers of Hanoi problem when we have 3 disks? Group of answer choices 5 6 8 4 7 After the following statements execute, what item is at the front of the queue? QueueInterface<String> zooDelivery = new LinkedQueue<>(); zooDelivery.enqueue("lion"); zooDelivery.enqueue("tiger"); zooDelivery.enqueue("cheetah"); String next = zooDelivery.dequeue(); next = zooDelivery.dequeue(); zooDelivery.enqueue("jaguar"); zooDelivery.enqueue("cat"); Group of answer choices "lion" "tiger" "cheetah" "jaguar" "cat" When a recursive method does not include a base case, calling the method results...
Write a java program to solve Towers of Hanoi with the condition that there are "m"...
Write a java program to solve Towers of Hanoi with the condition that there are "m" number of rods and "n" number of disks. Where m >= 3 and n >=1.
10. The Tower of Hanoi is a puzzle consisting of a board with three dowels and...
10. The Tower of Hanoi is a puzzle consisting of a board with three dowels and a collection of n disks of n different radii. The disks have holes drilled through their centers so they can fit on the dowels on the board. Initially, all the disks are on the first dowel arranged in order of their sizes, with the largest one being at the bottom, and the smallest one on the top. The object is to move all the...
There is a famous puzzle called the Tower of Hanoi. Code the recursive implementation of it...
There is a famous puzzle called the Tower of Hanoi. Code the recursive implementation of it with Python.
Consider the following variants of the Towers of Hanoi. For each of variant, describe an algorithm...
Consider the following variants of the Towers of Hanoi. For each of variant, describe an algorithm to solve it in as few moves as possible. Prove that your algorithm is correct. Initially, all the n disks are on peg 1, and you need to move the disks to peg 2. In all the following variants, you are not allowed to put a bigger disk on top of a smaller disk. Consider the disappearing Tower of Hanoi puzzle where the largest...
towers of hanoi c++ program using stacks and singly linked lists.
towers of hanoi c++ program using stacks and singly linked lists.
Program a solver for the Towers of Hanoi problem presented below. Submit the following to canvas:...
Program a solver for the Towers of Hanoi problem presented below. Submit the following to canvas: Hanoi.java, Driver.java (contains your main method). This is an individual project. Students may discuss solutions to the problem but may not share code with one another. I will discuss this project during our next lecture. Rules: a) There are three Pillars (Pillar1, Pillar2, Pillar3). b) There are N number of disks of increasing size (disk size is indicated by an integer). c) At the...
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner...
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner (using Stack) in C++(using data structure). Note: you have to solve for N number of disks, 3 Towers (Stacks). Do not use recursion. For better understanding play the game at least once. Link:https://www.mathsisfun.com/games/towerofhanoi.html
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT