-1

I'm doing Towers of Hanoi for school, which will eventually take 7 discs and I cannot figure out how to use cout statements to display the moves that the discs make. For example, "Move disk 7 from peg1 to peg3." Any help on this would be much appreciated. The cout statements would be in the moveDisk() and Hanoi() functions.

#include <iostream>
#include <vector>
#include <string>
#include <cassert>

using namespace std;

struct pegType
{
    vector<int> diskStack;
    string pegName;
};

/***********************Function Prototypes************************/
void loadDisk(int totalDisks, vector<int>& startPeg);
void printPeg(vector<int> stack, string name);
void moveDisk(vector<int>& startPeg, vector<int>& goalPeg);
int hanoi(int totalDisks, vector<int>& startPeg, vector<int>& goalPeg, vector<int>& tempPeg);


int main()
{
    //Local Variables
    pegType peg1, peg2, peg3;
    peg1.pegName = "Peg1";
    peg2.pegName = "Peg2";
    peg3.pegName = "Peg3";

    const int NUM_DISKS(3);
    int totalMoves = 0;


    //Welcome Message
    cout << "Welcome to the Tower of Hanoi Simulator. This simulation will run with " << NUM_DISKS << " discs." << endl << endl;
    loadDisk(NUM_DISKS, peg1.diskStack);

    //Check the conditions of the pegs in the beginning
    cout << "The Starting Conditions of the three pegs: ";
    cout << endl; printPeg(peg1.diskStack, peg1.pegName);
    printPeg(peg2.diskStack, peg2.pegName);
    printPeg(peg3.diskStack, peg3.pegName);

    cout << endl << "Moves required to move " << NUM_DISKS << " discs from " << peg1.pegName << " to " << peg3.pegName << ": " << endl;
    totalMoves = hanoi(NUM_DISKS, peg1.diskStack, peg3.diskStack, peg2.diskStack);


    //Ending Conditions of the pegs and disks
    cout << endl << "The Ending Conditions of the three pegs: " << endl;
    printPeg(peg1.diskStack, peg1.pegName);
    printPeg(peg2.diskStack, peg2.pegName);
    printPeg(peg3.diskStack, peg3.pegName);

    cout << endl << "A stack of " << NUM_DISKS << " can be transfered in " << totalMoves << " moves.";
    cout << endl;
    system("pause");
    return 0;
}

/***********************Function Definitions*****************************/

//Load the discs into the vector
void loadDisk(int totalDisks, vector<int>& startPeg)
{
    for (int i = totalDisks; i > 0; i--)
    {
        startPeg.push_back(i);
    }
}

//Print the disk Numbers backwards (3, 2, 1) to the user
void printPeg(vector<int> stack, string name)
{
    if (stack.size() > 1)
    {
        cout << name << " has " << stack.size() << " discs: ";
        assert(stack.size() > 0);
        for (unsigned int i = 0; i < stack.size(); i++)
        {
            cout << stack[i] << " ";
        }
    }
    else
    {
        cout << name << " has " << stack.size() << " discs: ";
    }

    cout << endl;
}

//Moves the bottom disk to the goal
void moveDisk(vector<int>& startPeg, vector<int>& goalPeg)
{
    if (startPeg.size() > 0)
    {
        int temp = startPeg.back();
        startPeg.pop_back();
        goalPeg.push_back(temp);
    }
}

//A recursive function that handles the disk transfer
//Displays the moves made
int hanoi(int totalDisks, vector<int>& startPeg, vector<int>& goalPeg, vector<int>& tempPeg)
{
    int count = 0;

    if (totalDisks > 0)
    {
        count = hanoi(totalDisks - 1, startPeg, goalPeg, tempPeg);
        moveDisk(startPeg, goalPeg);
        count++;
        count += hanoi(totalDisks - 1, tempPeg, goalPeg, startPeg);
    }
    return count;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
G.Egelstad
  • 13
  • 5

1 Answers1

0

Use struct Peg as parameters (instead of std::vector) in functions Hanoii() and MoveDisk() so that the function can correctly know the source and destination names for printing.

struct pegType
{
    vector<int> diskStack;
    string pegName;
};

/***********************Function Prototypes************************/
void loadDisk(int totalDisks, vector<int>& startPeg);
void printPeg(vector<int> stack, string name);
void moveDisk(pegType& source, pegType& dest);
int hanoi(int totalDisks, pegType& source, pegType& dest, pegType& aux);


int main()
{
    //Local Variables
    pegType peg1, peg2, peg3;
    peg1.pegName = "Peg1";
    peg2.pegName = "Peg2";
    peg3.pegName = "Peg3";

    const int NUM_DISKS(3);
    int totalMoves = 0;


    //Welcome Message
    cout << "Welcome to the Tower of Hanoi Simulator. This simulation will run with " << NUM_DISKS << " discs." << endl << endl;
    loadDisk(NUM_DISKS, peg1.diskStack);

    //Check the conditions of the pegs in the beginning
    cout << "The Starting Conditions of the three pegs: ";
    cout << endl; printPeg(peg1.diskStack, peg1.pegName);
    printPeg(peg2.diskStack, peg2.pegName);
    printPeg(peg3.diskStack, peg3.pegName);

    cout << endl << "Moves required to move " << NUM_DISKS << " discs from " << peg1.pegName << " to " << peg3.pegName << ": " << endl;
    totalMoves = hanoi(NUM_DISKS, peg1, peg3, peg2);


    //Ending Conditions of the pegs and disks
    cout << endl << "The Ending Conditions of the three pegs: " << endl;
    printPeg(peg1.diskStack, peg1.pegName);
    printPeg(peg2.diskStack, peg2.pegName);
    printPeg(peg3.diskStack, peg3.pegName);

    cout << endl << "A stack of " << NUM_DISKS << " can be transfered in " << totalMoves << " moves.";
    cout << endl;
    system("pause");
    return 0;
}

/***********************Function Definitions*****************************/

//Load the discs into the vector
void loadDisk(int totalDisks, vector<int>& startPeg)
{
    for (int i = totalDisks; i > 0; i--)
    {
        startPeg.push_back(i);
    }
}

//Print the disk Numbers backwards (3, 2, 1) to the user
void printPeg(vector<int> stack, string name)
{
    if (stack.size() > 1)
    {
        cout << name << " has " << stack.size() << " discs: ";
        assert(stack.size() > 0);
        for (unsigned int i = 0; i < stack.size(); i++)
        {
            cout << stack[i] << " ";
        }
    }
    else
    {
        cout << name << " has " << stack.size() << " discs: ";
    }

    cout << endl;
}

//Moves the bottom disk to the goal
void moveDisk(pegType& source, pegType& dest)
{
    if (source.diskStack.size() > 0)
    {       
        int temp = source.diskStack.back();
        source.diskStack.pop_back();
        dest.diskStack.push_back(temp);
        std::cout << "Moving disk# " << temp << " from " << source.pegName << " to " << dest.pegName << '\n';
    }
}

//A recursive function that handles the disk transfer
//Displays the moves made
int hanoi(int totalDisks, pegType& source, pegType& dest, pegType& aux)
{
    int count = 0;

    if (totalDisks > 0)
    {
        count = hanoi(totalDisks - 1, source, aux, dest);
        moveDisk(source, dest);
        count++;
        count += hanoi(totalDisks - 1, aux, dest, source);
    }
    return count;
}

Output:

enter image description here

seccpur
  • 4,996
  • 2
  • 13
  • 21
  • I don't think that's right. To move 3 discs, it would take seven moves, as shown at the bottom of the output screen. Also, it says "Moving disk# 1 from Peg1 to Peg3" three times. – G.Egelstad Sep 30 '18 at 03:27
  • Every disk move will call the MoveDisk function. That's why I told to fix your routine. It doesn't tell about moving to Peg2. My answer is only about printing the moves only. – seccpur Sep 30 '18 at 03:38
  • Fixed Hanoii() and MoveDisk() . The recursive call should vary the source and destination between Peg 2 and Peg3. So I named it the parameter as source , dest and aux in Hnaoii() and Mocedisk(). That solves the issue. – seccpur Sep 30 '18 at 03:52
  • Awesome! Thank you so much! I've been working on this since the beginning of the morning and have made it more complicated then need be. Again, big thanks! – G.Egelstad Sep 30 '18 at 03:55