summaryrefslogtreecommitdiff
path: root/src/agent.prolog
blob: 7b628777b24d493135aba88e17dd2420dfd81410 (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

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


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




% initialisation

init :-
    retractall(here(_)),
    retractall(pitAt(_)),
    retractall(wumpusAt(_)),
    retractall(goldAt(_)),
    retractall(batsAt(_)),
    initPos,
    initPits,
    initWumpus,
    initGold,
    initBats,
    roomList(Rooms),
    filter(Rooms, hazardAt, Walkable),
    isConnected(Walkable) *-> !; init.


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


initPits :- insert(4,pitAt).


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


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


initBats :- 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, !.


move(_) :-
    write('Unknown direction.\n\n').


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.


shoot(_) :-
    write('Unknown target.\n\n').




% 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\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\n'),
    write('*** GAME OVER ***\n'),
    halt(0).


lose(pit) :-
    write('You have fallen into a bottomless pit.\n\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).