# programming_languages / Prolog / farmer.pl

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31``` ```%% %% farmer.pl %% author: Seth Hobson %% opposite(e,w). opposite(w,e). unsafe([F,W,W,_]) :- opposite(F,W). unsafe([F,_,W,W]) :- opposite(F,W). 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]). 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). ```