# Can someone help me finish this program? TowerOfHanoi

• 03-29-2012, 06:23 PM
xdrazkalnytex
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);

// Draw the poles.
StdDraw.line(0, 0, 160, 0);
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);
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);
}
}
• 03-29-2012, 06:26 PM
Fubarable
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.
• 03-29-2012, 06:27 PM
Fubarable
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.