every one know the famous hanoi prolog
and you can find it HERE
and its great but when i write this query move(3,left,right,center).
its not showing these results
Move top disk from left to right
Move top disk from left to center
Move top disk from right to center
Move top disk from left to right
Move top disk from center to left
Move top disk from center to right
Move top disk from left to right
what i got is
Trace: >> RETURN: nl()
Trace: >> RETURN: hanoi(1,left,right,center)
Trace: >> RETURN: hanoi(2,center,right,left)
Trace: >> RETURN: hanoi(3,left,right,center)
True
1 Solution
so how i can let it print the results in a better way , and is it possible to name the disks so the program will name them to me as to show results as the following "move disk A from the left to the right"
sorry if I ask a lot but god I love PROLOG.
First of all, what Prolog are you using to get those results? That looks like you have a debugger enabled or something. I strongly recommend SWI-Prolog, which gives the correct results.
As to naming the discs, yes you can do it. Consider something like this:
move([Disc],X,Y,_) :-
write('Move disk '),
write(Disc),
write(' from '),
write(X),
write(' to '),
write(Y),
nl.
move([Bottom|Rest],Start,Swap,Goal) :-
move(Rest,Start,Swap,Goal),
move([Bottom],Start,Goal,_),
move(Rest,Swap,Goal,Start).
When you run move([biggest,middle,smallest], left, center, right).
you will get results like this:
Move disk smallest from left to center
Move disk middle from left to right
Move disk smallest from center to right
Move disk biggest from left to right
Move disk smallest from center to right
Move disk middle from center to left
Move disk smallest from right to left
The trick here is that this is a bottom-up algorithm. Essentially it boils down to:
Because it's bottom-up, you have to give the list starting with the bottom-most disc.