Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prolog Programming: You got a set of six discs of different sizes all initially

ID: 3778569 • Letter: P

Question

Prolog Programming:

You got a set of six discs of different sizes all initially inserted in post A, as you can see in the figure below. Your goal is to move all the disks to post C, by taking into account the following conditions: You can only move one disk at a time You can never put a bigger disk on top of a smaller disk. Write a Prolog program that solves this problem. The program should show all the steps required for any given solution, and all possible solutions. Again, to solve this problem you can use the same technique we learned in class to solve the cannibals and missionaries problem.

Explanation / Answer

Here is the code for you:

moves(6,'PegA', 'PegC', 'PegB').

Database:

moves(1,X,Y,_):-
write('Move disc from '),
write(X),
write(' to '),
write(Y),
nl.

%Left, Right, Center
moves(N,L,R,C):-
M is N-1,
moves(M,L,C,R),
moves(1,L,R,_),
moves(M,C,R,L).