Results 1 to 3 of 3
  1. #1
    xdrazkalnytex is offline Member
    Join Date
    Mar 2012
    Posts
    34
    Rep Power
    0

    Default Can someone help me finish this program? TowerOfHanoi

    // TowersOfHanoi.java

    import java.awt.Color;

    public class TowersOfHanoi
    {
    // Draw the poles with the disks correctly placed on them according to the
    // rules of the game. Index of smallest disk 1 and the index of the largest disk is
    // disks.length - 1. Each value in the array indicates the pole to which the
    // disk belongs; 0 is the left pole, 1 the middle pole, and 2 the right pole.
    private static void draw(int[] disks)
    {
    // Clear screen.
    StdDraw.clear();

    // Draw the floor.
    StdDraw.setPenColor(Color.BLACK);
    StdDraw.setPenRadius(0.04);

    // Draw the poles.
    StdDraw.line(0, 0, 160, 0);
    StdDraw.setPenRadius(0.01);
    StdDraw.line(30, 0, 30, 40);
    StdDraw.line(80, 0, 80, 40);
    StdDraw.line(130, 0, 130, 40);

    // Draw the disks starting with the largest one.
    StdDraw.setPenColor(Color.BLUE);
    StdDraw.setPenRadius(0.01);
    double y1 = 5.0, y2 = 5.0, y3 = 5.0; // y coordinate of the next disk on
    // each of the three poles
    for (int i = disks.length - 1; i > 0; i--)
    {
    // TBD: check which pole the disk i belongs, draw the disk with
    // appropriate size, and update the y coordinate for the next disk on
    // that pole.

    StdDraw.setPenRadius(0.10); // magic constant
    double size = 0.5 * i / disks.length-1;
    int d = disks[i];
    StdDraw.line(d-size/2, disks[d], d + size/2, disks[d]);
    ++disks[d];
    }

    // Pause for a second.
    StdDraw.show(1000);
    }

    // Solves the Towers of Hanoi problem recursively.
    private static void moves(int n, boolean left, int[] disks)
    {
    if (n == 0) {
    return;
    }
    moves(n - 1, !left, disks);
    if (left) {
    // TBD: modify disks appropriately.
    System.out.println(n + "Left");
    draw(disks);
    }
    else {
    // TBD: modify disks appropriately.
    System.out.println(n + "Right");
    draw(disks);
    }
    moves(n - 1, !left, disks);
    }

    // Entry point
    public static void main(String[] args)
    {
    int n = Integer.parseInt(args[0]); // Number of disks.
    int[] disks = new int[n + 1];
    StdDraw.setXscale(0.0, 160.0);
    StdDraw.setYscale(0.0, 160.0);
    draw(disks);
    moves(n, true, disks);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can someone help me finish this program? TowerOfHanoi

    Closing thread as it is nothing but a homework dump.

    Consider re-asking your question, but if you do, please ask a specific answerable question, not "here's my code, finish it for me", else the thread will be locked again.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can someone help me finish this program? TowerOfHanoi

    Edit: you've been admonished for dumping homework here before. Consider this a last warning -- if you do this again, you will be banned.

Similar Threads

  1. Please help me finish off this program
    By Danbendlin in forum New To Java
    Replies: 1
    Last Post: 03-28-2011, 01:27 AM
  2. Someone help me fix/finish this
    By Apertin in forum Java Applets
    Replies: 1
    Last Post: 10-15-2010, 03:28 PM
  3. how to finish this?
    By xpandaxlover in forum New To Java
    Replies: 2
    Last Post: 04-09-2010, 07:43 PM
  4. how to finish this?
    By xpandaxlover in forum New To Java
    Replies: 1
    Last Post: 04-09-2010, 07:42 PM
  5. Next, Finish Buttons !!!
    By pele in forum SWT / JFace
    Replies: 1
    Last Post: 07-14-2007, 05:22 PM

Posting Permissions

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