
February 12th, 2007, 07:06 PM
|
|
Registered User
|
|
Join Date: Aug 2006
Posts: 15
Time spent in forums: 1 h 21 m 37 sec
Reputation Power: 0
|
|
|
Hanoi Tower Help
I was assigned to do a hanoi tower with the following exact outputs. When you run the program this is what it should look like:
This is my program so far. Right now it prints all the towers but the the math is wrong and does not look like the assigned format, and i cant figure it out.
Code:
import java.io.*;
import java.util.*;
public class HanoiTower
{
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
public static void main (String[] args)throws IOException
{
HanoiTower HoT = new HanoiTower();
HoT.Tower1();
}
private void Tower1()throws IOException
{
int UserNum;
ArrayList<String>Peg1 = new ArrayList();
ArrayList<String>Peg2 = new ArrayList();
ArrayList<String>Peg3 = new ArrayList();
String Sides = "", Spaces = "", empty = "", Input;
System.out.print("Please enter a disk#: ");
UserNum = Integer.parseInt(Input = keyboard.readLine().trim());
if(UserNum < 1 || UserNum > 10)
System.exit(0);
for(int a = 1; a <= UserNum; a++)
Spaces+=" ";
empty = (Spaces+"|"+Spaces);
for(int a = 1; a <= 10; a++)
{
Peg1.add(empty);
Peg2.add(empty);
Peg3.add(empty);
}
for(int a = 1; a <= UserNum; a++)
{
Spaces="";
for(int b = a; b <= a; b++)
Sides+="*";
for(int c = (UserNum-a); c > 0; c--)
Spaces+=" ";
Peg1.remove(UserNum - a);
Peg1.add(Spaces+Sides+a+Sides+Spaces);
}
/*Prints Tower1*/
System.out.println("\n\nInitial Start:");
for(int x = 0; x < 10; x++)
System.out.print(Peg1.get(x)+" "+Peg2.get(x)+" "+Peg3.get(x)+"\n");
System.out.println("\n\nStatus: Move #8");
/*Prints Tower2*/
Tower2((10-UserNum), empty, Peg1, Peg2, Peg3);
for(int x = 0; x < 10; x++)
System.out.print(Peg1.get(x)+" "+Peg2.get(x)+" "+Peg3.get(x)+"\n");
System.out.println("\n\nFinal Status:");
/*Prints Tower3*/
Tower3((10-UserNum), empty, Peg1, Peg2, Peg3);
for(int x = 0; x < 10; x++)
System.out.print(Peg1.get(x)+" "+Peg2.get(x)+" "+Peg3.get(x)+"\n");
}
/*Function for Tower2*/
public static void Tower2(int x, String Space, ArrayList Peg1, ArrayList Peg2, ArrayList Peg3)
{
Object Temp = Peg1.get(x);
Peg1.remove(x);
Peg1.add(x, Space);
Peg3.remove(9);
Peg3.add(0, Temp);
}
/*Function for Tower3*/
public static void Tower3(int x, String Space, ArrayList Peg1, ArrayList Peg2, ArrayList Peg3)
{
Object Temp = Peg1.get(x);
Peg1.remove(x);
Peg1.add(x, Space);
Peg3.remove(9);
Peg3.add(0, Temp);
}
}
|