opp(e, w). opp(w, e). % places(farmer, fox, chicken, corn) % % in places(), the first parameter always says the farmer's position, % the second parameter always says the fox's position, % the third parameter always says the chicken's position, % the four parameter always says the corn's position, % variables like FxHc are just used to ensure that both players are in the same place unsafe(places(Fa, FxCh, FxCh, Co)) :- opp(Fa, FxCh). unsafe(places(Fa, Fx, ChCo, ChCo)) :- opp(Fa, ChCo). move(places(FaFx, FaFx, Ch, Co), places(X, X, Ch, Co), takefox) :- opp(X, FaFx), not(unsafe(places(X, X, Ch, Co))), write(takeFox(X, X, Ch, Co)), nl. move(places(FaCh, Fx, FaCh, Co), places(X, Fx, X, Co), takechicken) :- opp(X, FaCh), not(unsafe(places(X, Fx, X, Co))), write(takeChicken(X, Fx, X, Co)), nl. move(places(FaCo, Fx, Ch, FaCo), places(X, Fx, Ch, X), takecorn) :- opp(X, FaCo), not(unsafe(places(X, Fx, Ch, X))), write(takeCorn(X, Fx, Ch, X)), nl. move(places(Fa, Fx, Ch, Co), places(X, Fx, Ch, Co), goalone) :- opp(X, Fa), not(unsafe(places(X, Fx, Ch, Co))), write(goAlone(X, Fx, Ch, Co)), nl. move(places(Fa, Fx, Ch, Co), places(Fa, Fx, Ch, Co), _) :- write(backtrackFrom(Fa, Fx, Ch, Co)), nl, fail. % that last clause just ensures that before we reach a failure that makes % backtracking happen, we have a chance to report that. path(A, A, How) :- write(success), nl, reverse(How, Woh), write(Woh). path(A, B, How) :- move(A, C, Action), not(member(C, How)), path(C, B, [C, Action | How]). solve-it :- path(places(w, w, w, w), places(e, e, e, e), [places(w, w, w, w)]).