Commits

Seth Hobson  committed 0ffdad6

Finished farmer.pl logic, working on printing

  • Participants
  • Parent commits 1fe3cec

Comments (0)

Files changed (1)

File Prolog/farmer.pl

+%%
+%% farmer.pl
+%% author: Seth Hobson
+%%
 
-opposite(E,W).
-opposite(W,E).
+opposite(e,w).
+opposite(w,e).
 
-solution([W,W,W,W],[]).
-unsafe([F,X,X,_]) :- opposite(F,X).
-unsafe([F,_,X,X]) :- opposite(F,X).
+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([X,X,G,C], W, [Y,Y,G,C]) :- opposite(X,Y).
-move([X,W,X,C], G, [Y,W,Y,C]) :- opposite(X,Y).
-move([X,W,G,X], C, [Y,W,G,Y]) :- opposite(X,Y).
-move([X,W,G,C], Z, [Y,W,G,C]) :- opposite(X,Y).
+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]).
 
-puzzle(P) :- length(X,Y), Y < 15, solution([E,E,E,E],X), append(([[E,E,E,E]],X,P), printSolution(P).
+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).
 
+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()
+
+writeList([]).
+writeList([H|T]) :- write(H), writeList(T).