forked from mmalita/PrologPuzzles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoat.pl
68 lines (56 loc) · 2.07 KB
/
goat.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* File: goat.pl
Title: The goat problem
A farmer has to cross a river with a wolf, a goat and a cabbage.
He has a boat, but in the boat he can take just one thing.
He cannot let the goat alone with the wolf or the goat with the cabbage.
It’s obvious why. What is the solution?
We describe the problem as Nodes in a graph and the solution means to find a path from the initial node to the final node.
state(s) = node in graph
s(farmer(Bank),goat(Bank),cabbage(Bank),wolf(Bank)).
Bank can be East (e) or West (w).
**********************************************/
:- encoding(utf8).
:- use_module(library(lists)).
start :-
initial(S),
path(S,[],Sol),
reverse(Sol,Res),
write_list(Res).
%% at the beginning All are on the same bank
initial(s(farmer(e),goat(e),cabbage(e),wolf(e))).
%% at the end they have all to be on the other bank
final(s(farmer(w),goat(w),cabbage(w),wolf(w))).
path(Node,Path,[Node|Path]) :- final(Node).
path(Node,Path,Sol) :-
move(Node,N1),
\+bad(N1),
\+member(N1,Path),
path(N1,[Node|Path],Sol).
%% opposite bank East is opposite to west Bank. opp/2
opp(e,w).
opp(w,e).
/*
A bad state (s) is a state to be avoided. In this case:
Goat cannot be left alone with wolf or cabbage
The bank where the farmer is (T) has to be opposite
Where the goat is together with the wolf or the cabbage
T, G, C , W are variables for the banks (e or w)
**************************************************************/
bad(s(farmer(T),goat(G),cabbage(C),wolf(W))) :-
(G=W, opp(T,G)); (G=C, opp(T,G)).
%% Farmer takes goat
move(s(farmer(T), goat(T), cabbage(C), wolf(W)),
s(farmer(T1),goat(T1),cabbage(C), wolf(W))) :- opp(T,T1).
%% Farmer takes cabbage
move(s(farmer(T), goat(G),cabbage(T), wolf(W)),
s(farmer(T1),goat(G),cabbage(T1),wolf(W))) :- opp(T,T1).
%% Farmer takes wolf
move(s(farmer(T), goat(G),cabbage(C),wolf(T)),
s(farmer(T1),goat(G),cabbage(C),wolf(T1))) :- opp(T,T1).
%% Farmer goes alone
move(s(farmer(T), goat(G),cabbage(C),wolf(W)),
s(farmer(T1),goat(G),cabbage(C),wolf(W))) :- opp(T,T1).
write_list(L) :-
forall(member(X,L),
(write(X), nl)
).