Thread: Towers Of Hanoi

  1. #1
    Registered User
    Join Date
    Dec 2009

    Towers Of Hanoi

    I'm looking to create a version of the url=]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:

    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
    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
    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
    New Zealand
    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, 10:17 PM
  5. Towers of Hanoi
    By janehung in forum C Programming
    Replies: 12
    Last Post: 01-07-2002, 06:40 AM