# Thread: Can someone help me finish this program? TowerOfHanoi

1. Member
Join Date
Mar 2012
Posts
34
Rep Power
0

## 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.

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. ## Re: Can someone help me finish this program? TowerOfHanoi

Closing thread as it is nothing but a homework dump.

3. ## 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.

#### Posting Permissions

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