Towers Of Hanoi

This is a discussion on Towers Of Hanoi within the C++ Programming forums, part of the General Programming Boards category; I'm looking to create a version of the url=http://en.wikipedia.org/wiki/Tower_of_Hanoi]towers of hanoi[/url] problem using C++. If your unfamiliar with the game, ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    40

    Towers Of Hanoi

    I'm looking to create a version of the url=http://en.wikipedia.org/wiki/Tower_of_Hanoi]towers of hanoi[/url] problem using C++. If your unfamiliar with the game, it involves moving hoops from one peg to another, without placing larger hoops on top of smaller hoops.

    I am not looking for a program that will solve it automatically.

    But I am having difficulty on deciding the best way to handle the logic behind the problem. I am going to do it with 3 hoops at first. But I need to decide how to represent these hoops being on each peg.

    I was thinking of using 3 instances of a class something like this:

    Code:
    class Peg{
    bool hoopA, hoopB, hoopC;
    } Peg1, Peg2, Peg3;
    But thought that this could lead to far too many conditional statements for all the possible moves.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Data structures are typically dictated by the algorithm. Do you know how you plan on solving the problem? As Wikipedia will tell you, there are a few ways of analyzing it.

    One of the more intuitive ways of starting would be to create a stack for each peg.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    It will be for the user to solve. Stacks appear to be exactly what I'm looking for. Thanks for the help

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Yes use three stacks. You can only move a hoop if the source stack is non-empty, and the destination stack is either empty or the top hoop on it has a larger diameter than the hoop from the source stack.
    The stacks themselves need only store ints, where each int is the hoop diameter.
    Hope that helps.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. towers of Hanoi variation
    By Aisthesis in forum C++ Programming
    Replies: 1
    Last Post: 06-14-2010, 10:44 AM
  2. Iterative Towers of Hanoi
    By Mister C in forum C++ Programming
    Replies: 10
    Last Post: 04-11-2009, 01:11 AM
  3. towers of hanoi - what is wrong with this code?
    By kanesoban in forum C Programming
    Replies: 4
    Last Post: 09-17-2007, 01:20 PM
  4. Towers of Hanoi (need help)
    By Loudan in forum C++ Programming
    Replies: 3
    Last Post: 01-30-2006, 09:17 PM
  5. Towers of Hanoi
    By janehung in forum C Programming
    Replies: 12
    Last Post: 01-07-2002, 05:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21