Write a Prolog program to solve Tower of Hanoi Problem

Aim: Write a Prolog program to solve Tower of Hanoi Problem.


Prolog Program Code:


domains
    loc=right;middle;left
predicates
    toh(integer)
    move(integer,loc,loc,loc)
    print(loc,loc)
clauses
    toh(N):-
    move(N,left,middle,right).
    move(1,A,_,C):-
    print(A,C),!.
    move(N,A,B,C):-
    N1=N-1,move(N1,A,C,B),
    print(A,C),move(N1,B,A,C).
    print(Loc1,Loc2):-n1,
    write("move a disk from",Loc1,"to",Loc2).
        

Output:


Post a Comment

0 Comments