Question
I need help fixing my C++ function to take all the conditions into consideration for my program. My program's responibility is to finish the implementation
I need help fixing my C++ function to take all the conditions into consideration for my program. My program's responibility is to finish the implementation of an iterative solution to the Towers of Hanoi puzzle. Specifically, I need to implement the puzzle initialization and the move operation utilizing the stacks provided. I can't rename existing functions, nor add any additional functions nor member variables! The program will read in a text file with containing a single integer: the number of disks for the puzzle. 2. The program will output the steps required to solve the puzzle for the specified number of disks. 3. The program will utilize the Hanoi class. 4. The program will utilize the std::stack library to complete the move functionality. 5. The only changes will be to the Hanoi.cpp file and will only be within the constructor and the MakeMove function
main: (can't change anything in the main)
#include
#include
#include
#include
#include "hanoi.h"
int main(int argc, char** argv)
{
if (argc != 3)
{
std::cout << "Usage: assignment04 [input file] [output file]" << std::endl;
return 0;
}
std::ifstream fin(argv[1]);
std::ofstream fout(argv[2]);
unsigned int n = 0;
if (!(fin >> n))
{
std::cerr << "Invalid input file!" << std::endl;
}
else
{
Hanoi problem(n);
problem.Solve(fout);
}
return 0;
}
hanoi.cpp (Only thing that needs tweaking or adding)
#include "hanoi.h" using namespace std; Hanoi::Hanoi(unsigned int n) : m_Disks(n) { m_Disks=n; std:stack<unsigned int> s; for(int i=0;i//m_Pegs[1]=std::stack (); // m_Pegs[2]=std::stack (); // TODO: set then initial state for the problem } 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! std::stack<unsigned int> s1=m_Pegs.at(peg1); unsigned int hold=s1.top(); hold; s1.pop(); std::stack<unsigned int> s2=m_Pegs.at(peg2); s2.push(hold); m_Pegs[peg1]=s1; m_Pegs[peg2]=s2; PrintMove(os, hold, peg1, peg2); }
hanoi.h
#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);
}
}
}
};
#endif // __HANOI_H__
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