• Post Reply Bookmark Topic Watch Topic
  • New Topic

help me in Towers of hanoi  RSS feed

 
diaa baraka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am requried to implement Towers of Hanoi puzzle. After each movement, show the placement of the discs. For example, for n=3, your output should be like the following:
1
2
3
-- -- --
A B C
Step 1:
2
3 1
-- -- --
A B C
Step 2:
3 2 1
-- -- --
A B C
Step 3:
1
3 2
-- -- --
A B C
And so on.



i know its algorithm and i have already implemented it but i am confused about the shape of output
this is my code without the statment of displaying tho output

public class main {

public static void main(String[] args) {
tower(3,'a', 'b', 'c');
}
public static void tower(int count ,char source ,char destionation,char spare){

if(count==1){
//output statement
}
else {
tower(count-1, source, spare,destionation);


tower(1, source, destionation, spare);

tower(count-1, spare, destionation, source);

}
}
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your last thread Kemel asked you to UseCodeTags (<- click) when posting your code. Please read the link and learn how to do so for your future posts.

Also, if your problem is with the output, why have you not shown the code you have written to display the output so we can advise on where you have gone wrong?
 
diaa baraka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my teacher is who told me how he wants the output to be?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO,
*Towers of hanoi* -- > wrong example, why? the answer is to teach stack . very funny ....
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
diaa baraka wrote:my teacher is who told me how he wants the output to be?

So what exactly is your question? In your original post you stated that you are "confused about the shape of output". We cannot see the output, nor you provided more information about what you/your teacher expect it to be.

And again, please UseCodeTags.
 
diaa baraka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't know how to make the output display like that
please read my code and you will understand
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try anything so far?
 
diaa baraka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes but i couldn't reach the same
i can make it like this output
(Move top disk from pole a to pole b)
i think the required output requires String formatting but i am a beginner and still have a bit experience so please help me
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
diaa baraka wrote: so please help me

People here ARE trying to help you. What they will NOT do is provide a full blown solution. We expect you to try something, show us what you try, and then ask questions. People will guide you, suggest to you things to try, or point out errors you may have made...but even if a complete solution is given, a moderator will most likely delete it.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
diaa baraka wrote:Yes but i couldn't reach the same
i can make it like this output
(Move top disk from pole a to pole b)
i think the required output requires String formatting but i am a beginner and still have a bit experience so please help me


Unfortunately, it is *not* about formatting -- it can't be done at all because you are not tracking the current state. At each move, you know what to move, but you don't know the locations of the disks that won't be moved.

You need to track the state of the pegs and disks. Then instead of "Move top disk from pole a to pole b" output, you need code a method that actually does the work to get to the new state. From there, you will be able to print the step -- and yes, at that point, it will involve formatting...

Henry

PS... I like your teacher's variation on this assignment. The Tower of Hanoi problem is such a common assignment, that solutions are all over the internet. This one requires a bit more effort than just using Google.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!