summaryrefslogtreecommitdiff
path: root/agent.prolog
blob: e48049dd60e499447ab3b0fc2b3391243d4fc7d1 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

:- module(agent, [init, look/0, move/1, take/1, shoot/1]).


:- consult('parser.prolog').
:- consult('map.prolog').




% initialisation

init :-
    initPos,
    initPits,
    initWumpus,
    initGold,
    initBats,
    roomList(Rooms),
    filter(Rooms, hazardAt, Walkable),
    isConnected(Walkable) *-> !; init.


initPos :-
    retractall(moves(_)),
    asserta(moves(0)),
    retractall(here(_)),
    roomList(Rooms),
    filter(Rooms, somethingAt, Emptyrooms),
    random_member(X,Emptyrooms),
    asserta(here(X)).


initPits :-
    retractall(pitAt(_)),
    insert(5,pitAt).


initWumpus :-
    retractall(wumpusAt(_)),
    roomList(Rooms),
    filter(Rooms, somethingAt, Emptyrooms),
    random_member(X,Emptyrooms),
    asserta(wumpusAt(X)).


initGold :-
    retractall(goldAt(_)),
    retractall(haveGold(_)),
    insert(3,goldAt),
    asserta(haveGold(0)).


initBats :-
    retractall(batsAt(_)),
    insert(2,batsAt).


insert(N,Thing) :-
    N > 0,
    Nx is N - 1,
    insert(Nx,Thing),
    roomList(Rooms),
    filter(Rooms, somethingAt, Emptyrooms),
    random_member(X,Emptyrooms),
    P =.. [Thing,X],
    asserta(P).
insert(0,_) :- true.




% command functions

look :-
    here(Location),
    writeSenses(Location),
    writeExits(Location).


move(Direction) :-
    here(Location),
    connects(Location, New, Direction),
    retract(here(Location)),
    asserta(here(New)),
    join(['You move to the ',Direction,'.\n\n'],W),
    write(W), 
    checkHazards,
    incrementMoves,
    look, !.


take(gold) :-
    here(Location),
    goldAt(Location),
    retract(goldAt(Location)),
    haveGold(G),
    Gx is G + 1,
    retract(haveGold(G)),
    asserta(haveGold(Gx)),
    write('You find some gold. Lucky you.\n\n'),
    incrementMoves, !.


take(_) :-
    write('You cannot take that.\n\n').


shoot(Direction) :-
    here(Location),
    connects(Location, Target, Direction),
    wumpusAt(Target),
    write('You hear an unearthly scream.\n\n'),
    retract(wumpusAt(Target)),
    incrementMoves,
    win.


shoot(Direction) :-
    here(Location),
    connects(Location, Target, Direction),
    not(wumpusAt(Target)),
    write('Thunk. Missed.\n\n'),
    incrementMoves,
    moveWumpus.




% letting the player know what's going on

writeSenses(Location) :-
    glitter(Location),
    breeze(Location),
    bats(Location),
    stench(Location).


glitter(Location) :-
    current_predicate(goldAt/1),
    goldAt(Location),
    write('You see a glitter along the sandy floor of the cave.\n').
glitter(_) :- true.


breeze(Location) :-
    current_predicate(pitAt/1),
    connects(Location, ConnectedRoom, _),
    pitAt(ConnectedRoom),
    write('A cold breeze blows through the room, making you shiver slightly.\n').
breeze(_) :- true.


bats(Location) :-
    current_predicate(batsAt/1),
    connects(Location, ConnectedRoom, _),
    batsAt(ConnectedRoom),
    write('All available surfaces are covered in guano. How unsanitary.\n').
bats(_) :- true.


stench(Location) :-
    current_predicate(wumpusAt/1),
    connects(Location, ConnectedRoom, _),
    wumpusAt(ConnectedRoom),
    write('An overpowering stench fills your nose.\n').
stench(_) :- true.


writeExits(Location) :-
    findall(X, connects(Location,_,X), Exits),
    intercalate(Exits, ', ', O),
    join(['There are exits to the ',O,'.\n'],W),
    write(W).




% modifying things and checking the ramifications

moveWumpus :-
    wumpusAt(Old),
    findall(X, connects(Old,X,_), PossibleNews),
    random_member(New, PossibleNews),
    retract(wumpusAt(Old)),
    asserta(wumpusAt(New)),
    checkWumpus.


checkHazards :-
    checkWumpus,
    checkBats,
    checkPit.


checkWumpus :-
    current_predicate(wumpusAt/1),
    here(Location),
    wumpusAt(Location),
    lose(eaten).
checkWumpus :- true.


checkBats :-
    current_predicate(batsAt/1),
    here(Location),
    batsAt(Location),
    write('A giant bat swoops down, picks you up, and deposits you elsewhere in the cave.\n\n'),
    roomList(Rooms),
    random_member(NewLocation, Rooms),
    retract(here(Location)),
    asserta(here(NewLocation)),
    checkHazards.
checkBats :- true.


checkPit :-
    current_predicate(pitAt/1),
    here(Location),
    pitAt(Location),
    lose(pit).
checkPit :- true.




% winning, losing, and otherwise

incrementMoves :-
    moves(N),
    retractall(moves(_)),
    Nx is N + 1,
    asserta(moves(Nx)).


win :-
    write('*** YOU WIN ***\n'),
    moves(M),
    haveGold(G),
    join(['\nWin accomplished in ',M,' moves with ',G,' gold found\n'],W),
    write(W),
    halt(0).


lose(eaten) :-
    write('You have been eaten by the wumpus.\n'),
    write('*** GAME OVER ***\n'),
    halt(0).


lose(pit) :-
    write('You have fallen into a bottomless pit.\n'),
    write('*** GAME OVER ***\n'),
    halt(0).




% miscellaneous clauses

somethingAt(X) :- current_predicate(here/1), here(X).
somethingAt(X) :- current_predicate(goldAt/1), goldAt(X).
somethingAt(X) :- hazardAt(X).


hazardAt(X) :- current_predicate(wumpusAt/1), wumpusAt(X).
hazardAt(X) :- current_predicate(pitAt/1), pitAt(X).
hazardAt(X) :- current_predicate(batsAt/1), batsAt(X).


intercalate([X|Y], Spacer, Result) :-
    intercalate(Y, Spacer, Tail),
    atom_concat(X, Spacer, Head),
    atom_concat(Head, Tail, Result).
intercalate([X], _, Result) :- Result = X.


filter(List, Predicate, Result) :-
    Test =.. [Predicate,X],
    findall(X, Test, No),
    subtract(List, No, Result).


head(X,[Y]) :- X = Y.
head(X,[Y|_]) :- X = Y.


tail(X,[]) :- X = [].
tail(X,[_]) :- X = [].
tail(X,[_|Y]) :- X = Y.


isConnected([]) :- true.
isConnected(X) :-
    head(Y,X),
    tail(Z,X),
    connectTail([Y],Z).

connectTail(_,[]) :- true.
connectTail([],_) :- false.
connectTail(Open,G) :-
    head(X,Open),
    tail(Y,Open),
    findall(N, connects(X,N,_), Ns),
    subtract(G,Ns,Gx),
    subtract(G,Gx,Nx),
    append(Y,Nx,Yx),
    connectTail(Yx,Gx).