Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 12th, 2007, 07:06 PM
ChaNgeD ChaNgeD is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 15 ChaNgeD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);
	}
}

Reply With Quote
  #2  
Old February 16th, 2007, 08:24 AM
ChaNgeD ChaNgeD is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 15 ChaNgeD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 37 sec
Reputation Power: 0
I should have been a little more specific as to what i want. All the user does is enter a number from 1-10. The screenshot above shows the number 10 entered for example. so it basically shows the first step, 8th step, and final step of the number entered in this matter. First and last step are reverse of each other which i already have working i just need the middle step to work. So when they enter a number 1-10 it shows the 3 steps to solve the hanoi tower. So it solves automatically depending on wat number the user chooses. I cant get the right display based on the number they choose.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Hanoi Tower Help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT