3
4:- dynamic at/2, i_am_at/1, alive/1,count/1. 5:- retractall(at(_, _)), retractall(i_am_at(_)), retractall(alive(_)). 6
8
9i_am_at(meadow).
10
12
13count(0).
14
16
17path(meadow,n,market).
18path(meadow,e,'dark forest'):-
19 at(flashlight, in_hand),
20 at(batteries,in_hand),
21 write('You have the flashlight. You can walk in the dark forest.'),nl.
22path(meadow,e,'dark forest') :-
23 at(flashlight,in_hand),
24 \+ at(batteries, in_hand),
25 write('You do not have the batteries. You cannot use the flashlight.'),nl,
26 !,fail.
27path(meadow,e,'dark forest'):-
28 write('Go into the dark forest without a light? Are you crazy?'),nl,
29 !,fail.
30
31path(meadow,s,'wood village').
32
33path(market,s,meadow).
34
35path('wood village',n,meadow).
36path('wood village',e,'death valley') :-
37 alive(zombie),
38 nl,!.
39path('wood village',e,'death valley').
40
41path('dark forest',s,'death valley') :-
42 alive(zombie),
43 nl,!.
44path('dark forest',s,'death valley').
45path('dark forest',w,meadow).
46
47path('death valley',w,'wood village').
48path('death valley',n,'dark forest') :-
49 at(flashlight,in_hand),
50 at(batteries,in_hand),
51 write('You have the flashlight. You can walk in the dark forest.'),nl.
52path('death valley',n,'dark forest') :-
53 at(flashlight,in_hand),
54 \+ at(batteries, in_hand),
55 write('You do not have the batteries. You cannot use the flashlight.'),nl,
56 !,fail.
57path('death valley',n,'dark forest') :-
58 write('Go into the dark forest without a light? Are you crazy?'),nl,
59 !,fail.
60path('death valley',s,'secret chamber').
61
62path('secret chamber',n,'death valley').
63
64
65
68
69
70at(flashlight,market).
71at(boat,'wood village').
72at(batteries,'wood village').
73at(gun,meadow).
74at(spell,'dark forest').
75at(tent,'wood village').
76at('poison pill','dark forest').
77at(parachute,market).
78
79
81
82alive(zombie).
83
84
86
87take(X) :-
88 at(X, in_hand),
89 write('You''re already holding it!'),
90 nl, !.
91
92take(X) :-
93 i_am_at(Place),
94 at(X, Place),
95 count(3),
96 write('You can just take at most three objects. To take the'),nl,
97 write('object you want, you must drop something.'),nl,
98 nl,!.
99
100take(X) :-
101 i_am_at(Place),
102 at(X, Place),
103 retract(at(X, Place)),
104 assert(at(X, in_hand)),
105 retract(count(N)),
106 M is N+1,
107 assert(count(M)),
108 write('OK.'),
109 nl, !.
110
111take(_) :-
112 write('I don''t see it here.'),
113 nl.
114
115
117
118drop(X) :-
119 at(X, in_hand),
120 i_am_at(Place),
121 retract(at(X, in_hand)),
122 assert(at(X, Place)),
123 retract(count(N)),
124 M is N-1,
125 assert(count(M)),
126 write('OK.'),
127 nl, !.
128
129drop(_) :-
130 write('You aren''t holding it!'),
131 nl.
132
133
135
136n :- go(n).
137
138s :- go(s).
139
140e :- go(e).
141
142w :- go(w).
143
144
146
147go(Direction) :-
148 i_am_at(Here),
149 path(Here, Direction, 'death valley'),
150 alive(zombie),
151 at(gun,in_hand),
152 write('The death valley is full of zombies!'),nl,
153 retract(i_am_at(Here)),
154 assert(i_am_at('death valley')),
155 look,!.
156go(Direction) :-
157 i_am_at(Here),
158 path(Here, Direction, 'death valley'),
159 alive(zombie),
160 \+ at(gun,in_hand),
161 write('The death valley is full of zombies!'),nl,
162 write('You do not have weapons to kill the zombies.'),nl,
163 write('Your brain is eaten by the zombies!'),!,die.
164go(Direction) :-
165 i_am_at(Here),
166 path(Here, Direction, 'death valley'),
167 write('There is no zombies. You can walk in the death valley'),nl,
168 write('safely.'),
169 retract(i_am_at(Here)),
170 assert(i_am_at('death valley')),
171 nl,!.
172go(Direction) :-
173 i_am_at(Here),
174 path(Here,Direction,'secret chamber'),
175 alive(zombie),
176 write('The entrance of the secret chamber is blocked by the '),nl,
177 write('zombies. You cannot go to the secret chamber.'),nl,!,fail.
178go(Direction) :-
179 i_am_at(Here),
180 path(Here, Direction, 'secret chamber'),
181 \+ at(boat,in_hand),
182 write('You do not have the boat! The lake is full of ghosts.'),nl,
183 write('They drag you down to the bottom of the lake.'),nl,
184 !,die.
185go(Direction) :-
186 i_am_at(Here),
187 path(Here, Direction, 'secret chamber'),
188 at(boat,in_hand),
189 write('You have a boat and you can cross the river.'),nl,
190 retract(i_am_at(Here)),
191 assert(i_am_at('secret chamber')),
192 look, !.
193go(Direction) :-
194 i_am_at(Here),
195 path(Here, Direction, There),
196 retract(i_am_at(Here)),
197 assert(i_am_at(There)),
198 look, !.
199
200go(_) :-
201 write('You can''t go that way.').
202
203
205
206look :-
207 i_am_at(Place),
208 describe(Place),
209 nl,
210 notice_objects_at(Place),
211 nl.
212
213
216
217notice_objects_at(Place) :-
218 at(X, Place),
219 write('There is a '), write(X), write(' here.'), nl,
220 fail.
221
222notice_objects_at(_).
223
225
226i :-
227 at(Item, in_hand),
228 write('You have '),write(Item),write('.'),nl,fail.
229i.
230
231
232
234
235use(gun) :-
236 i_am_at('death valley'),
237 alive(zombie),
238 retract(alive(zombie)),
239 write('You have successfully killed all the zombies!'),nl,!.
240
241use(spell) :-
242 i_am_at('secret chamber'),
243 write('You open the door of the secret chamber! You finally'),nl,
244 write('see your lovely princess!'),nl,
245 !,finish.
246
247use(_) :-
248 write('Nothing changes.'),nl.
249
250
251
253
254die :-
255 !, finish.
256
257
262
263finish :-
264 nl,
265 write('The game is over. Please enter the halt. command.'),
266 nl, !.
267
268
270
271instructions :-
272 nl,
273 write('You are a prince. Your princess has been taken away to a'),nl,
274 write('secret chamber by your enemy. The journey is destined to be '),nl,
275 write('dangerous and threatening! My hero! Go to rescue your lovely'),nl,
276 write('princess! Take care!'),nl,
277 write('Reminder: You can just take at most three objects.'),nl,
278 write('Available commands are:'), nl,
279 write('start. -- to start the game.'), nl,
280 write('n. s. e. w. -- to go in that direction.'), nl,
281 write('take(Object). -- to pick up an object.'), nl,
282 write('drop(Object). -- to put down an object.'), nl,
283 write('i. -- to see the objects that you are'),nl,
284 write(' currently holding.'),nl,
285 write('use(Object). -- to use the object.'),nl,
286 write('look. -- to look around you again.'), nl,
287 write('instructions. -- to see this message again.'), nl,
288 write('halt. -- to end the game and quit.'), nl,
289 nl.
290
291
293
294start :-
295 instructions,
296 look.
297
298
301
302describe(meadow) :-
303 write('You are in a meadow.'),nl,
304 write('Go north, arrive at the market.'),nl,
305 write('Go east, arrive at the dark valley.'),nl,
306 write('Go south, arrive at the wood village.'),nl.
307describe(market) :-
308 write('You are in the market.'),nl,
309 write('Go south, arrive at the meadow.'),nl.
310describe('wood village') :-
311 write('You are in the wood village.'),nl,
312 write('Go north, arrive at the meadow.'),nl,
313 write('Go east, arrive at the death valley.'),nl.
314describe('death valley') :-
315 alive(zombie),
316 write('You are in the death valley. The zombies are coming!'),nl,
317 write('Use your wisdom to kill all the zombies. Or, you can '),nl,
318 write('leave in the west or north.'),nl,
319 write('Go north, arrive at the dark forest.'),nl,
320 write('Go west, arrive at the wood village.'),nl,
321 write('Go south, there is a ghost lake.'),nl,!.
322describe('death valley') :-
323 write('You are in the death valley. There are no zombies. '),nl,
324 write('You can be safe in the death valley.'),nl,
325 write('Go north, arrive at the dark forest.'),nl,
326 write('Go west, arrive at the wood village.'),nl,
327 write('Go south, there is a ghost lake.'),nl.
328describe('dark forest') :-
329 write('You are in the dark forest. There lives an elderly '),nl,
330 write('witch. Since you have saved her before, the witch gives'),nl,
331 write('you a spell and a poison pill. They may be useful in the'),nl,
332 write('coming journey.'),nl,
333 write('Go west, arrive at the meadow.'),nl,
334 write('Go south, arrive at the death valley.'),nl.
335describe('secret chamber') :-
336 write('You are in the entrance of the secret chamber. There is'),nl,
337 write('an oldstone gate in front of you. You need a key to open'),nl,
338 write('the gate of the secret chamber!'),nl,
339 write('Go north, arrive at the death valley.'),nl