Hanoi in Prolog

Resume

Hanoi is a very popular game in acient years. Here’s an implentation of Hanoi with prolog.


Code

hanoi(N) :- move(N,left,center,right).

move(0,_,_,_) :- !.
move(N,A,B,C) :-
    M is N-1,
    move(M,A,C,B),
    inform(A,B),
    move(M,C,B,A).

inform(X,Y) :- write('Move from '), write(X), write(' to '), write(Y), write('.'), nl.

Result

?- hanoi(3).
Move from left to center.
Move from left to right.
Move from center to right.
Move from left to center.
Move from right to left.
Move from right to center.
Move from left to center.
true.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章