towers of hanoi HELP !!!

Hi all, ive got a program to write, using simple Java.

it is the towers of hanoi, which is where u have 3 pegs with 3 disks on the left peg and you have to move them one by one to the right peg but you cant put a bigger disk onto a smaller disk. im sure u have heard of it !

now i have only had a few lessons in it so i have been told to use simple language, it doesnt have to be complicated.

all i have learnt about so far is: for loops, while loops, and do while loops. also If statements etc. aswel as variable setup.

can someone show me how to do it please because i am completly stuck, even though its simple to most of you which is why im hoping it wont take you long :)

id apreciate it !

thanks

[744 byte] By [Deridexa] at [2007-9-28 7:43:47]
# 1
Im sure you can find this on the web somewhere, try to search. Here is somewhere that will get you started. It has a psuedo-coded recursive solution: http://www.cut-the-knot.com/recurrence/hanoi.shtml
amwright1a at 2007-7-9 18:57:09 > top of Java-index,Archived Forums,Java Programming...
# 2
http://www.ability.org.uk/down/Tower.java http://www.google.com/search?q=towers+hanoi+java+-script+source
jsalonena at 2007-7-9 18:57:09 > top of Java-index,Archived Forums,Java Programming...
# 3
Do a search of this newsgroup with "hanoi towers" and you will find the answer.
blane2a at 2007-7-9 18:57:09 > top of Java-index,Archived Forums,Java Programming...
# 4
hm all those seem too complicated, i have not heard of most of the things in them,i mean real simple coding i need to use, it can realy just be an animation just showing the moves.
Deridexa at 2007-7-9 18:57:09 > top of Java-index,Archived Forums,Java Programming...
# 5
Try doing your own homework. That's the simpleset solution. I'm telling your teacher on you!
dustpana at 2007-7-9 18:57:09 > top of Java-index,Archived Forums,Java Programming...
# 6
:) thats just the problem, i cant :(im sorry if i sound cheeky, im just totaly stuck and somewhere like this is the only place to get some actual help.
Deridexa at 2007-7-9 18:57:09 > top of Java-index,Archived Forums,Java Programming...
# 7
atleast post the code you have...
smckee6452a at 2007-7-9 18:57:09 > top of Java-index,Archived Forums,Java Programming...
# 8

Well that's at least an honest response from you. If you really can't do the homework, you've two choices, as I see it - seek out some extracurricular resources (such as this forum) to help you (not to do it for you, just help) or drop the class.

If you opt for the first option, then the only way it'll work is if you try your assignments and come to the resource with specific questions, things like: "Why am I getting a null pointer exception whenever I try to make a move" or "Why does my code end prematurely, before a solution". *not* things like "It don't work".

Best of luck to you

Lee

tsitha at 2007-7-9 18:57:09 > top of Java-index,Archived Forums,Java Programming...
# 9

> :) thats just the problem, i cant :(

>

> im sorry if i sound cheeky, im just totaly stuck and

> somewhere like this is the only place to get some

> actual help.

>

Where are you stuck? You are more likely to get help if you:

1) show that you have already done some work on the subject

2) ask specific questions and give enough information on the problem you are having

3) don't ask people to write the code for you

4) show that you are interested in why things work as opposed to just wanting to know the answer

jsalonena at 2007-7-9 18:57:09 > top of Java-index,Archived Forums,Java Programming...
# 10
i havnt got any code realy, sorry.i emagin it can be done with like g.drawline and stuff to make the sticks, it just gota be simple, a stick version of sorts, just an animation
Deridexa at 2007-7-9 18:57:10 > top of Java-index,Archived Forums,Java Programming...
# 11

Thing is, graphics code adds another layer of complexity to your problem. You should just dump text out, I would think, decribing each move. That way you can concentrate on the logic of moving the pieces around, not having to worry about graphics. You can plug that in later, I would think.

Lee

tsitha at 2007-7-9 18:57:10 > top of Java-index,Archived Forums,Java Programming...
# 12

Usually Towers of Hanoi is used to represent recursive functions, so it's kind of strange that you're hearing to use looping structures to solve it. Unless maybe the point of the assignment is to unroll recursive functions into looping control structures -- is it?

Re-read the part of your text about recursion aka recursive functions.

If your text is missing that, then quit your class immediately because it's being taught by an idiot.

Also I agree with tsith -- write code to handle just the logic. Write a simple interface to it, one which uses only text input and output. That way you'll be able to keep it clear in your head and only have to solve one problem at a time.

paulcwa at 2007-7-9 18:57:10 > top of Java-index,Archived Forums,Java Programming...
# 13

ok, ive got the strting positions drawn up but id like to ask 2 questions please.

how do u make the applet refresh? is it just something like screen.refresh ?

also how do u make a program wait a few second on one screen, say like 2 seconds, could u just make it count to 1 million or something :)

o know it probably sounds silly just as ive said, im just beggining on Java

cheers

Deridexa at 2007-7-9 18:57:10 > top of Java-index,Archived Forums,Java Programming...
# 14

See now that's a nice specific question.

How do I slow down this god damn thing so I can see what's going on?

quick hack

try {Thread.sleep(2000);}catch(InterruptedException e){}

2000 is milliseconds, so that's 2 seconds.

Now mind you, that's horrible coding practice for anything but education in debugging :)

SpinozaQa at 2007-7-9 18:57:10 > top of Java-index,Archived Forums,Java Programming...
# 15

ok, thanks

do i just put this in on one line then:

try {Thread.sleep(2000);}catch(InterruptedException e){}

the whole thing?

does it just work on its own, or does it need key presses or something, i can show u what i have so far, its very simple so dont laugh hehe, this just displays the 3 pegs and disks:

import java.awt.*;

import javax.swing.*;

public class hanoi extends JApplet{

public void paint(Graphics g){

g.drawLine(20,20,20,90);

g.drawLine(70,20,70,90);

g.drawLine(120,20,120,90);

g.drawLine(10,85,30,85);

g.drawLine(13,80,27,80);

g.drawLine(16,75,24,75);

}

}

thx

Deridexa at 2007-7-18 19:31:51 > top of Java-index,Archived Forums,Java Programming...
# 16
I would say that you should store the positions of thedisc in an array and paint something dependant of valuesin this array.
janwna at 2007-7-18 19:31:51 > top of Java-index,Archived Forums,Java Programming...
# 17

hmm not used arrays much so im not sure of the coding.

for this line:

try {Thread.sleep(2000);}catch(InterruptedException e){}

do i need to put the 'try' and 'catch' in it or was that just saying try this: {Thread.sleep(2000);}

is this all one thing that all needs to go in?

Deridexa at 2007-7-18 19:31:51 > top of Java-index,Archived Forums,Java Programming...
# 18

> hmm not used arrays much so im not sure of the

> coding.

>

> for this line:

>

> try {Thread.sleep(2000);}catch(InterruptedException

> e){}

>

> do i need to put the 'try' and 'catch' in it or was

> that just saying try this: {Thread.sleep(2000);}

>

> is this all one thing that all needs to go in?

Oh, for God's sake, read a book or one of the thousands of java tutorials on this site and all over the internet.

try and catch are parts of the language. If you had spent a half hour reading a tutorial, you'd know this.

paulcwa at 2007-7-18 19:31:51 > top of Java-index,Archived Forums,Java Programming...
# 19
sorry
Deridexa at 2007-7-18 19:31:51 > top of Java-index,Archived Forums,Java Programming...