Computer Science Canada Drawing the Tower of Hanoi |
Author: | whoareyou [ Mon Apr 02, 2012 9:22 pm ] | ||
Post subject: | Drawing the Tower of Hanoi | ||
So as of now, I have a class that will display the moves when solving the Tower of Hanoi.
I want to take it to the next level and actually visually display the actual disks moving from peg to peg. I'm just wondering ... what variables/arrays should I use and what should I keep track of? Also, if there is an array that tells you how many disks are on a peg, how would you keep track of the disk (ie. the size since the disks are stacked from longest at the bottom to shortest at the top) that is on that peg? Because if there is only 1 disk on a peg, is it OK to assume that it is the smallest disk? |
Author: | DemonWasp [ Mon Apr 02, 2012 10:16 pm ] |
Post subject: | RE:Drawing the Tower of Hanoi |
Well, you should keep track of the current location of each disk (or the current disks on a peg) in arrays. You know the initial positions, and you know the moves made, so it should be easy to implement moving (just do the move every time you say you're going to). None of your current variables will tell you this. If there is only one disk on a peg, it can be any disk (hypothetically speaking). In your solution, it should only ever be the smallest disk (alone very frequently) or the largest disk (only alone immediately after you uncover it until you move it to its final position and re-cover it). |
Author: | whoareyou [ Tue Apr 03, 2012 4:13 pm ] | ||
Post subject: | Re: Drawing the Tower of Hanoi | ||
Ok so I created 3 new methods,: one to draw the pegs, one to draw the disks, and one to determine the height from the bottom that the peg should be draw from.
How exactly would I implement these methods so that it will actually change the position when a move is made and where do I put the command to draw in the actual method that moves (ie. after which recursive step)? I'm new at recursion :$. |
Author: | Zren [ Tue Apr 03, 2012 4:44 pm ] | ||
Post subject: | RE:Drawing the Tower of Hanoi | ||
I *strongly* recommend doing this in a CLI (Command Line Interface aka the console) fashion first. Once you have the mechanics down, then you can focus on eye candy.
> Also, if there is an array that tells you how many disks are on a peg The data structure you want is a List. As each Peg will contain an ordered collection of elements (disks). Most of all, it differs from the native Arrays by it's capability to have a variable size. Which will be required as there number of disks on a peg is not constant. |