3
4:- dynamic at/2, i_am_at/1, alive/1. 5:- retractall(at(_, _)), retractall(i_am_at(_)), retractall(alive(_)). 6
8
9i_am_at(home).
10
11
13
14path(witch, d, park).
15
16path(park, u, witch).
17path(park, w, park_entrance).
18
19path(road, u, thief).
20path(thief, d, road).
21path(park_entrance, e, park) :- at(energy_drink, in_hand).
22path(park_entrance, e, park) :-
23write('You are about to collapse! Find energy drink and drink it!!'), nl, !, fail.
24path(park_entrance, s, home).
25
26
27path(home, n, park_entrance) :- at(torch, in_hand), at(batteries, in_hand).
28path(home, n, park_entrance) :- at(torch, in_hand),
29write('Oops! Your torch is out of batteries. Find batteries to continue treasure hunt!'), nl, !, fail.
30path(home, n, park_entrance) :-
31 write('Going for treasure hunt without a torch is not possible.'), nl,
32 !, fail.
33path(home, s, school).
34
35path(school, n, home).
36path(school, w, road).
37
38path(road, e, school).
39
40path(classroom, w, school).
41path(witch, u, treasure_chest).
42path(treasure_chest, d, park).
43
44path(school, s, yard) :-
45write('Answer this to enter the yard.'), nl,
46write('What five letter word becomes shorter when you add two letters to it?'), nl,
47read(X),
48(X = 'short' -> write('You successfully entered the yard!'), nl, !;
49write('Oops! Wrong answer. You cannot enter the yard until you answer correctly.'), nl, !, fail).
50
51
52path(yard, n, school).
53
54path(classroom, e, closet).
55path(closet, w, classroom).
56
57path(school, e, classroom) :- at(classroom_key, in_hand).
58path(school, e, classroom) :-
59 write('The door appears to be locked.'), nl,
60 fail.
61
62
63
66
67at(treasure_chest, witch).
68at(chest_key, yard).
69at(knife, school).
70at(torch, school).
71at(energy_drink, closet).
72at(batteries, classroom).
73at(classroom_key, road).
74
75
77
78alive(witch).
79alive(thief).
80
81
83
84take(X) :-
85 at(X, in_hand),
86 write('You''re already holding it!'),
87 nl, !.
88
89take(X) :-
90 i_am_at(Place),
91 at(X, Place),
92 retract(at(X, Place)),
93 assert(at(X, in_hand)),
94 write('OK.'),
95 nl, !.
96
97take(_) :-
98 write('I don''t see it here.'),
99 nl.
100
102
103
104i :-
105at(_, in_hand),
106write('You have: '), nl,
107list_things.
108i :-
109write('You have nothing'), nl.
110
111list_things :-
112at(X, in_hand),
113tab(2), write(X), nl,
114fail.
115list_things.
116
117
119
120drop(X) :-
121 at(X, in_hand),
122 i_am_at(Place),
123 retract(at(X, in_hand)),
124 assert(at(X, Place)),
125 write('OK.'),
126 nl, !.
127
128drop(_) :-
129 write('You aren''t holding it!'),
130 nl.
131
132
134
135n :- go(n).
136
137s :- go(s).
138
139e :- go(e).
140
141w :- go(w).
142
143u :- go(u).
144
145d :- go(d).
146
147
149
150go(Direction) :-
151 i_am_at(Here),
152 path(Here, Direction, There),
153 retract(i_am_at(Here)),
154 assert(i_am_at(There)),
155 look, !.
156
157go(_) :-
158 write('You can''t go that way.').
159
160
162
163look :-
164 i_am_at(Place),
165 describe(Place),
166 nl,
167 notice_objects_at(Place),
168 nl.
169
170
173
174notice_objects_at(Place) :-
175 at(X, Place),
176 write('There is a '), write(X), write(' here.'), nl,
177 fail.
178
179notice_objects_at(_).
180
181
182
183
185
186kill :-
187i_am_at(road),
188at(knife, in_hand),
189retract(alive(thief)),
190write('You killed the thief! Now take the classroom key and continue your treasure hunt!'), nl,!.
191
192kill :-
193 i_am_at(road),
194 write('Oh! You have been killed by the thief.'), nl,
195 !, die.
196
197kill :-
198 i_am_at(park),
199 write('Oh! The witch has evil potions to kill you!'), nl.
200
201
202kill :-
203 i_am_at(witch),
204 at(knife, in_hand),
205 retract(alive(witch)),
206 write('You stabbed the witch in her stomach. The witch has'), nl,
207 write('died'), nl, !.
208
209kill :-
210 i_am_at(witch),
211 write('You can''t kill the witch by beating her with you hand.'), nl,
212 write('You need a knife for it.'), nl.
213
214kill :-
215 write('There is nothing to kill here.'), nl.
216
217
219
220die :-
221 !, finish.
222
223
228
229finish :-
230 nl,
231 write(' Game over. Enter halt command.'),
232 nl, !.
233
234
236
237instructions :-
238 nl,
239 write('Enter commands using standard Prolog syntax.'), nl,
240 write('Available commands are:'), nl,
241 write('start. -- to start the game.'), nl,
242 write('n. s. e. w. u. d. -- to go in north, south, east, west, up, down, direction.'), nl,
243 write('take(Object). -- to pick up an object.'), nl,
244 write('i -- to see the things you are currently holding.'), nl,
245 write('kill. -- to attack an enemy.'), nl,
246 write('kill. -- to attack an enemy.'), nl,
247 write('look. -- to look around you again.'), nl,
248 write('instructions. -- to see this message again.'), nl,
249 write('halt. -- to end the game and quit.'), nl,
250 nl.
251
252
254
255start :-
256 instructions,
257 look.
258
259
262
263describe(home) :-
264 at(treasure_chest, in_hand), at(chest_key, in_hand),
265 write('You used the chest key to open the treasure chest.'), nl,
266 write('Congratulations !! You have all the gold. You are rich now!'), nl,
267 write('You won the game.'), nl,
268 finish, !.
269
270
271 describe(home) :-
272 at(treasure_chest, in_hand),
273 write('You are at home but you need to find the chest key to open the treasure chest. Go back!!'), nl.
274
275describe(home) :-
276 write('You are at home. To north there is the park entrance.'), nl,
277 write('To the south is the haunted school. Your task'), nl,
278 write('is to get the treasure chest with all the gold back to your home.'), nl.
279
280
281
282describe(school) :-
283 write('You are in the haunted school. There is a'), nl,
284 write('classroom in the east,'), nl,
285 write('a yard in the south and a vast road in the west.'), nl.
286
287describe(road) :-
288 alive(thief),
289 at(knife, in_hand),
290 write('You are on the vast road. It''s dangerous here! There are'), nl,
291 write('thiefs waiting to kill you! Go back! Or kill them with your knife!'), nl.
292
293describe(classroom) :-
294 write('You are in the classroom. There is a secret closet somewhere here.'), nl.
295
296describe(closet) :-
297 write('You entered the secret closet.'), nl.
298
299describe(yard) :-
300 write('You are in the yard.'), nl.
301
302describe(park_entrance) :-
303 write('You are standing at the park entrance. To the east'), nl,
304 write('is the park, where you can begin your treasure hunt.'), nl.
305
306
307describe(park) :-
308 alive(witch),
309 at(treasure_chest, in_hand),
310 write('The witch sees you with the chest and attacks you with her evil potion!'), nl,
311 write(' ...it is over in seconds....'), nl,
312 die.
313
314describe(road) :-
315 alive(thief),
316 at(classroom_key, in_hand),
317 write('The thief sees you with the key and shoots you!'), nl,
318 write(' You are dead!'), nl,
319 die.
320
321
322describe(park) :-
323 alive(witch),
324 write('The witch is looking at you with evil looks! She has'), nl,
325 write('an evil potion to kill you. I would advise you to leave'), nl,
326 write('quietly....'), nl, !.
327
328describe(park) :-
329 write('There is a witch here, that is moaning.'), nl.
330
331describe(witch) :-
332 alive(witch),
333 write('You jumped on the witch and are about to kill the witch.'), nl.
334
335
336describe(witch) :-
337 write('You are on top of the dead witch!'), nl