Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Drawing the Tower of Hanoi
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
whoareyou




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

code:

// The "TowersOfHanoi" class.
/*This program shows a recursive method used to solve the Towers of Hanoi problem.
It demonstrates the moving of a disk as a print statement.
It uses a global variable to record the number of moves needed*/
import java.awt.*;
import hsa.Console;
public class TowersOfHanoi
{
    static Console c;   // The output console
    static int count;   // global variable for keeping track of the number of moves
    public static void main (String[] args)
    {
        c = new Console ();
        final int DISKS = 3; // change this value to investigate any number of disks
        count = 1; // initialize the count to 1
        moveTower (DISKS, 1, 3);
    } // main method


    public static void moveTower (int height, int start, int finish)
    {
        if (height == 1)
        { // the is final task before exiting from the method
            // move the disk from start to finish
            c.println (count + ": " + start + " --> " + finish);
            // increase the count each time there is output to record a move
            count++;
        }
        else
        { // this is the recursive part of the method
            // determine the location of the intermediate needle
            int intermediate = 6 - (start + finish);
            // move all but 1 disk from start to intermediate
            moveTower (height - 1, start, intermediate);
            // move the bottom disk
            c.println (count + ": " + start + " --> " + finish);
            // increase the count each time there is output to record a move
            count++;
            // move the remaining disks from intermediate to finish using recursion
            moveTower (height - 1, intermediate, finish);
        }
    }
} // TowersOfHanoi class


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?
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: 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).
whoareyou




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

code:

    public static void drawPegs ()
    {
        c.drawLine (PEGS, 0, PEGS, c.maxy ());
        c.drawLine (3 * PEGS, 0, 3 * PEGS, c.maxy ());
        c.drawLine (5 * PEGS, 0, 5 * PEGS, c.maxy ());
    }


    public static void drawTowers ()

    {

        drawPegs ();

        diskLoc [1] = 3;
        diskLoc [2] = 5;

        for (int i = 0 ; i < diskLoc.length ; i++)
        {
            int width = (int) (baseWidth * diskSize [i]);
            int height = (int) (c.maxy () / DISKS);
            int x = diskLoc [i] * PEGS - width / 2;

            c.drawRect (x, c.maxy () - (detHeight (diskLoc [i]) + 1) * height, width, height);
        }

    }


    public static int detHeight (int peg)
    {
        switch (peg)
        {
            case 1:
                return PEG1;

            case 3:
                return PEG2;

            case 5:
                return PEG3;

            default:
                return -1;
        }
    }


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 :$.
Zren




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

code:

=== Move 0 ===
Peg 1: 54321
Peg 2: -----
Peg 3: -----
=== Move x ===
Peg 1: 53---
Peg 2: 421--
Peg 3: -----


> 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: