So my program uses the list template as the towers and the values in them are disks. I used the push and pop methods with loops to display changing values due to user input.

Rules of the game:
1. All disks are stacked into an initial tower.
2. Disks range from smallest at the top to the biggest at the bottom.
3. Only 1 disk can be moved to another tower per move.
4. The disk you are moving to the next tower stacks onto possibly another disk, but the selected disk can't be bigger than the one in designated tower.
5. The game ends when the user successfully moves all the disks to another tower, in the range stated in rule #2.

Problems with my program:
* add while loop that counts the remaining moves for player
* fix displaying cout for the "lists"
* fix push and pop logic statements
* add if temp or dest tower size is = numDisk, you win
* else if moves = 0, you lost

#include <list>
#include <queue>
#include <iostream>
#include <string>
#include <math.h>
using namespace std;

template <typename T, size_t N>
inline
size_t arraysize( const T(&)[ N ] )
{
  return N;
}
int main()
{
    int numDisks;
    char selTower, nxTower;
    int count = 1;
    queue<int>display;

    cout << "o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o\n" <<
            "o o o o o o o o o TOWERS OF HANOI GAME  o o o o o o o o o o\n" <<
            "o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o\n\n";
    cout << "How many disks do you want to play with?\nSuggestions: 4 - Recommended, 5 - Intermediate ";
    cin >> numDisks;

    const int num = numDisks;
    int disks[num];

    for (int i = 0; i < numDisks; i++)
    {
        disks[i] = i+1;
        //cout << disks[i] << endl;
    }
    //objectNameOf_List.classMem(position to insert, amount, input number)
    list<int>initial (disks, disks+numDisks); //equiv. to (4, 1)
    list<int>destination(numDisks);
    list<int>temporary(numDisks);

    while (1) //2^n - 1
    {
        cout << "\nChoose a tower:\n\n" <<
                "   [a]         [b]         [c]\n"
                "Initial  Destination   Temporary\n"<<
                "    |          |           |\n" <<
                "    v          v           v\n\n";

        cout << "    |    " << "  " << "    |    " << "  " << "     |    " << endl;
        cout << "    |    " << "  " << "    |    " << "  " << "     |    " << endl;
    for(int i = 0; i < (int)numDisks; i++)
        cout << "    " <<  initial.front()+i << "          " << destination.front()+i << "           " << temporary.front()+i << endl;
        cout << "_________  _________   _________\n\n";

    cin >> selTower;

    cout << "\nSelect the tower for this top disk to move to. ";
    cin >> nxTower;

        if (selTower == 'a' && nxTower == 'b')
        {
            destination.push_front(initial.front());
            destination.pop_back();
            initial.pop_front();
        }
        else if (selTower == 'a' && nxTower == 'c')
        {
            temporary.push_front(initial.front());
            temporary.pop_back();
            initial.pop_front();
        }
        else if (selTower == 'b' && nxTower == 'a')
        {
            initial.push_front(destination.front());
            initial.pop_back();
            destination.pop_front();
        }
        else if (selTower == 'b' && nxTower == 'c')
        {
            temporary.push_front(destination.front());
            temporary.pop_back();
            destination.pop_front();
        }
        else if (selTower == 'c' && nxTower == 'a')
        {
            initial.push_front(temporary.front());
            initial.pop_back();
            temporary.pop_front();
        }
        else if (selTower == 'c' && nxTower == 'b')
        {
            destination.push_front(temporary.front());
            destination.pop_back();
            temporary.pop_front();
        }
    count++;
    }
    cout << "Ran out of moves. You lose!";
}

Recommended Answers

All 5 Replies

And you want us to fix your homework assignment? Sorry, but I have better things to do, like improve the browsing/web-apps performace for 50 million users...

Good point. Next person.

You know, you could ask a question or two that will lead us to understand what you are having trouble with and actually be able to lead you to a solution or two.

I guess it was a bad idea to ask for help here. To sign off on this thread, I'll give you all a quote from my project:

cout <<"You win! Too bad your intelligence can't stop the end of the universe.";

I guess it was a bad idea to ask for help here.

Actually, it wasn't. You just didn't ask one that could be answered.
I take it back. You never asked a question at all -- in any of your posts. So don't get huffy with us!

To sign off on this thread, I'll give you all a quote from my project:

And I'll finish off with a quote from the garment cleansing industry:

No tickee? No laundry!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.