[RESOLVED] Towers of Hanoi Game
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: [RESOLVED] Towers of Hanoi Game

  1. #1
    Join Date
    Sep 2012
    Posts
    10

    [RESOLVED] Towers of Hanoi Game

    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. 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



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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Towers of Hanoi Game

    Quote Originally Posted by Lethargic View Post
    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
    OK, so what is your question?

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center