Question
In this assignment, you will finish the implementation of an iterative solution to the Towers of Hanoi puzzle. Specifically, you will implement the puzzle initialization
In this assignment, you will finish the implementation of an iterative solution to the Towers of Hanoi puzzle. Specifically, you will implement the puzzle initialization and the move operation utilizing the stacks provided. Do not rename existing functions, nor add any additional functions nor member variables!
REQUIREMENTS
The program will read in a text file with containing a single integer: the number of disks for the puzzle.
The program will output the steps required to solve the puzzle for the specified number of disks.
The program will utilize the Hanoi class.
The program will utilize the std::stack library to complete the move functionality.
The only changes will be to the Hanoi.cpp file and will only be within the constructor and the
MakeMove function.
i need help with my move function i try somethings and still dont know what to do
#include "hanoi.h"
void Hanoi::MakeMove(std::ostream& os, unsigned int peg1, unsigned int peg2)
{
// TODO: move the disks between pegs appropriately
// NOTE: Make sure to call PrintMove for each disk movement!
}
Below is the .h file to use when implementing the two above functions
#ifndef __HANOI_H__
#define __HANOI_H__
#include
#include
#include
#include
#include
class Hanoi
{
private:
unsigned int m_Disks;
std::array
void PrintMove(std::ostream& os, unsigned int disk, unsigned int from, unsigned int to)
{
os << "Moving Disk " << disk << " from peg " << from << " to peg " << to << std::endl;
}
void MakeMove(std::ostream& os, unsigned int from, unsigned int to);
public:
Hanoi(unsigned int n);
void Solve(std::ostream& os)
{
unsigned int number_of_moves = std::pow(2, m_Disks) - 1;
unsigned int src = 0;
unsigned int aux = 1;
unsigned int dst = 2;
// if the number of disks are even, swap the aux and dest pegs
if (m_Disks % 2 == 0)
{
std::swap(aux, dst);
}
for (int i = 1; i <= number_of_moves; ++i)
{
if (i % 3 == 1)
{
MakeMove(os, src, dst);
}
else if (i % 3 == 2)
{
MakeMove(os, src, aux);
}
else
{
MakeMove(os, aux, dst);
}
}
}
};
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started