Results 1 to 3 of 3
- 03-29-2012, 07:23 PM #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);
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);
}
}
-
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.
-
Similar Threads
-
Please help me finish off this program
By Danbendlin in forum New To JavaReplies: 1Last Post: 03-28-2011, 02:27 AM -
Someone help me fix/finish this
By Apertin in forum Java AppletsReplies: 1Last Post: 10-15-2010, 04:28 PM -
how to finish this?
By xpandaxlover in forum New To JavaReplies: 2Last Post: 04-09-2010, 08:43 PM -
how to finish this?
By xpandaxlover in forum New To JavaReplies: 1Last Post: 04-09-2010, 08:42 PM -
Next, Finish Buttons !!!
By pele in forum SWT / JFaceReplies: 1Last Post: 07-14-2007, 06:22 PM
Bookmarks