-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWumpus World.pl
125 lines (95 loc) · 4.17 KB
/
Wumpus World.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
:- dynamic ([visited/1,
breeze/1,
stench/1,
glitter/1,
moved/2,
wumpus_location/1,
pit_location/1,
gold_location/1,
agent_location/1,
timer/1,
score/1,
wumpus_final_location/1]).
adjacent([X,Y],L) :- Xr is X+1, L=[Xr,Y].
adjacent([X,Y],L) :- Xl is X-1, L=[Xl,Y].
adjacent([X,Y],L) :- Yt is Y+1, L=[X,Yt].
adjacent([X,Y],L) :- Yb is Y-1, L=[X,Yb].
%----------------------------------------------------------------------------
border([X,Y]) :- X<1; X>4; Y<1; Y>4.
%----------------------------------------------------------------------------
makestatement([X,Y]) :-
forall((pit_location(PL),adjacent([X,Y],PL)), (format('there is a breeze in ~p~n',[[X,Y]]),assert(breeze([X,Y])))),
forall((wumpus_location(L),adjacent([X,Y],L)), (format('there is a stench in ~p~n',[[X,Y]]),assert(stench([X,Y])))),
forall((gold_location(G),([X,Y] == G)),(assert(glitter([X,Y])),score(S), N is S + 500 , format('I have found GOLD, Score is now ~p~n',[N]), retractall(score(_)),retractall(glitter(_)),retractall(gold_location(_))., assert(score(N)))).
%----------------------------------------------------------------------------
pit([X,Y]) :- forall(adjacent([X,Y],L), (breeze(L);border(L))).
pit([X,Y]) :- adjacent([X,Y],L), visited(L), breeze(L), forall(adjacent(L,L2),(L2 == [X,Y] ; psafe(L2) ; border(L2))).
%----------------------------------------------------------------------------
wumpus([X,Y]) :- forall(adjacent([X,Y],L), (stench(L);border(L))), retractall(wumpus_final_location(_)),assert(wumpus_final_location([X,Y])).
wumpus([X,Y]) :- adjacent([X,Y],L), visited(L), stench(L), forall(adjacent(L,L2),(L2 == [X,Y];wsafe(L2); border(L2))),
retractall(wumpus_final_location(_)),assert(wumpus_final_location([X,Y])).
gold([X,Y]) :- glitter([X,Y]).
psafe([X,Y]) :- adjacent([X,Y],L), visited(L), \+ breeze(L).
wsafe([X,Y]) :- adjacent([X,Y], L), visited(L), \+ stench(L).
fail_agent([X,Y]) :- pit([X,Y]); wumpus([X,Y]).
maybe([X,Y]) :- \+ visited([X,Y]), \+ fail_agent([X,Y]), ( adjacent([X,Y],L), ( breeze(L);stench(L))).
safe([X,Y]) :- visited([X,Y]).
safe([X,Y]) :- psafe([X,Y]), wsafe([X,Y]).
good([X,Y]) :- safe([X,Y]), \+visited([X,Y]).
existgood(A) :- visited(V), adjacent(V,A), good(A), \+ visited(A), \+ border(A).
existmaybe(A) :- visited(V), adjacent(V,A), maybe(A), \+ visited(A), \+ border(A).
failure(X):- wumpus_location(W), pit_location(P), (X=W;X=P), format('Eaten!') ,halt.
exist(X):- existgood(X);existmaybe(X).
start:-
init,
agent_location(AL),
\+acte(AL),
wumpus_final_location(Z),
(Z=[-1,-1])-> format('The agent failed to find the Wumpus~nFAILED !~n'); ( wumpus_final_location(Z), format('The wumpus is located in ~p! I am shooting my bullet!~n',[Z])),
score(S),
timer(T),
format('Score: ~p~n',[S]),
format('timer: ~p~n',[T]),
format('WON!').
acte(X):-
retractall(agent_location(_)),
assert(agent_location(X)),
update_score(-1),
update_timer(1),
format('I am in ~p~n',[X]),
%failure(X),
assert(visited(X)),
makestatement(X),
exist(L),
get_next(N,L,X),
wumpus_final_location(Z),
Z = [-1,-1],
acte(N).
get_next([X,Y],[X1,Y1],[X2,Y2]):-
(adjacent([X1,Y1],[X2,Y2])) -> ([X,Y] = [X1,Y1]);(adjacent([X1,Y1],L),visited(L)) ->([X,Y]=L).
update_score(X):-
score(S),
Z is S+X,
retractall(score(_)),
assert(score(Z)).
update_timer(X):-
timer(S),
Z is S+X,
retractall(timer(_)),
assert(timer(Z)).
init:-
retractall(timer(_)),
assert(timer(0)),
retractall(score(_)),
assert(score(30)),
retractall(gold_location(_)),
assert(gold_location([3,3])),
retractall(wumpus_location(_)),
assert(wumpus_location([4,4])),
retractall(pit_location(_)),
assert(pit_location([1,4])),
assert(pit_location([3,1])),
retractall(agent_location(_)),
assert(agent_location([1,1])),
retractall(wumpus_final_location(_)),
assert(wumpus_final_location([-1,-1])).