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!";
}

Dani AI

Generated

A few targeted fixes will make this playable. Right now destination and temporary are constructed with numDisks elements, so they start non-empty and you compensate with pop_back(). That breaks the invariant that a tower is just a stack. Instead, start those towers empty and always push/pop at the same end. Your print code (front()+i) does not iterate the lists; it reuses the same element and will read from empty towers. Finally, compute remaining moves without pow() and enforce legality before moving.

Here is a compact pattern you can drop in (arrays or vectors are simpler than lists for a stack-like game):

// back is the top of each tower
vector<vector<int>> t(3);
for (int d = numDisks; d >= 1; --d) t[0].push_back(d);

auto legal_move = [&](int s, int d) {
    if (t[s].empty()) return false;
    int x = t[s].back();
    if (!t[d].empty() && x > t[d].back()) return false;
    t[s].pop_back(); t[d].push_back(x);
    return true;
};

uint64_t maxMoves = (numDisks < 63) ? ((1ULL << numDisks) - 1) : UINT64_MAX;
uint64_t used = 0;

Display towers from bottom to top or top to bottom; either is fine as long as it is consistent. Simple and safe:

auto print_towers = [&]{
    for (int p = 0; p < 3; ++p) {
        cout << "ABC"[p] << ": ";
        for (int d : t[p]) cout << d << ' ';
        cout << '\n';
    }
    cout << "Moves left: " << (maxMoves - used) << "\n\n";
};

Game loop sketch: read a/b/c to indices 0..2, reject src==dst, call legal_move(src,dst) and increment used only on success, stop when t[1].size()==numDisks || t[2].size()==numDisks (win) or used==maxMoves (lose). As hinted, asking about one concrete issue at a time (e.g., move legality or rendering) will get you faster, more focused help.

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.