tags:

views:

1128

answers:

1

Hi , 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.

+3  A: 

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:

  1. Move everything on top of the bottom disc to the swap (which is the recursive step)
  2. Move the bottom disc to the goal
  3. Move everything else from the swap to the goal

Because it's bottom-up, you have to give the list starting with the bottom-most disc.

Pesto
Thanx A Lot , I'm Using the PIE (Prolog Inference Engine) and still its not showing the answers correctly i will try to download the SWI but is there a way to keep on using the PIE and get the same results
Yes. You have the trace on. If you turn it off, you should get the correct results. Click on the Trace icon (next to Pause, it looks like the eject button on a DVD player), or go to Engine->Trace Calls. And I know SWI-Prolog may not be as friendly, but it's much more powerful.
Pesto