Commits

Seth Hobson committed 4127279

Added printMoves and writeList rules to farmer.pl

Comments (0)

Files changed (1)

 %% author: Seth Hobson
 %%
 
-opposite(e,w).
-opposite(w,e).
+opposite(e, w).
+opposite(w, e).
 
-unsafe([F,W,W,_]) :- opposite(F,W).
-unsafe([F,_,W,W]) :- opposite(F,W).
+unsafe([F, W, W, _]) :- opposite(F, W).
+unsafe([F, _, W, W]) :- opposite(F, W).
 
-safe([F,W,G,C]) :- \+(unsafe([F,W,G,C])).
+safe([F, W, G, C]) :- \+(unsafe([F, W, G, C])).
 
-move([F1,F1,G,C], [F2,F2,G,C]) :- opposite(F1,F2), safe([F2,F2,G,C]).
-move([F1,W,F1,C], [F2,W,F2,C]) :- opposite(F1,F2), safe([F2,W,F2,C]).
-move([F1,W,G,F1], [F2,W,G,F2]) :- opposite(F1,F2), safe([F2,W,G,F2]).
-move([F1,W,G,C], [F2,W,G,C]) :- opposite(F1,F2), safe([F2,W,G,C]).
+move([F1, F1, G, C], [F2, F2, G, C]) :- opposite(F1, F2), safe([F2, F2, G, C]).
+move([F1, W, F1, C], [F2, W, F2, C]) :- opposite(F1, F2), safe([F2, W, F2, C]).
+move([F1, W, G, F1], [F2, W, G, F2]) :- opposite(F1, F2), safe([F2, W, G, F2]).
+move([F1, W, G, C], [F2, W, G, C]) :- opposite(F1, F2), safe([F2, W, G, C]).
 
-solution([w,w,w,w],[]).
-solution(X,[Y|Z]) :- move(X,Y), solution(Y,Z).
+solution([w, w, w, w],[]).
+solution(X, [Y|Z]) :- move(X, Y), solution(Y, Z).
 
-puzzle(P) :- length(X, Y), Y < 15, solution([e, e, e, e], X), append([[e, e, e, e]], X, P), printSolution(P).
+puzzle(P) :- length(X, Y), Y < 15, solution([e, e, e, e], X), append([[e, e, e, e]], X, P). %printSolution(P).
 
 printSolution([X|Y]) :- write('Start. '), writeLocation(X), printMoves([X|Y]), write('Solved. \n').
 
 writeLocation(X) :- getList(X, e, E), getList(X, w, W), write('East: '), writeList(E), write('West: '), writeList(W), write('\n').
 
-getList()
+% TODO: implement getList rules
+%getList([], List, List).
+%getList([H|T1], List, [H|T2]) :
 
 writeList([]).
 writeList([H|T]) :- write(H), writeList(T).
+
+printMoves([]).
+printMoves([_]).
+printMoves([X, Y|Z]) :- X = [A, B, C, D], Y = [E, B, C, D], opposite(A, E), write('Farmer takes self. '), writeLocation(Y), printMoves([Y|Z]).
+printMoves([X, Y|Z]) :- X = [A, A, C, D], Y = [E, E, C, D], opposite(A, E), write('Farmer takes wolf. '), writeLocation(Y), printMoves([Y|Z]).
+printMoves([X, Y|Z]) :- X = [A, B, A, D], Y = [E, B, E, D], opposite(A, E), write('Farmer takes goat. '), writeLocation(Y), printMoves([Y|Z]).
+printMoves([X, Y|Z]) :- X = [A, B, C, A], Y = [E, B, C, E], opposite(A, E), write('Farmer takes cabbage. '), writeLocation(Y), printMoves([Y|Z]).