2
3:- dynamic i_am_at/1, at/2, holding/1, buy_only_at/2, alive/1, money/1. 4:- retractall(at(_, _)), retractall(i_am_at(_)), retractall(alive(_)),
5 retractall(buy_only_at(_, _)), retractall(alive(_)), retractall((money(_)).
6
8
9i_am_at(logcabin).
10money(0).
11
13
14path(treasurechest, e, tunnel).
15path(tunnel, w, treasurechest).
16path(tunnel, s, crystalball).
17path(crystalball, n, tunnel).
18path(tunnel, n, smalldoor).
19path(smalldoor, s, tunnel) :- at(badge, in_hand).
20path(smalldoor, s, tunnel) :-
21 write('Oh, the door seems to be loocked, there is '), nl,
22 write('a guardian angle, could you find a badge and give'), nl,
23 write('it to the angle, he will open the door for you.'), nl, fail.
24path(smalldoor, w, rockcave).
25path(rockcave, e, smalldoor).
26path(smalldoor, e, logcabin).
27path(logcabin, w, smalldoor).
28path(logcabin, n, store).
29path(store, s, logcabin).
30path(logcabin, e, bridge) :- at(horse, in_hand).
31path(logcabin, e, bridge) :-
32 write('Oh, you can not across the bridge without horse.'), nl, fail.
33path(bridge, w, logcabin).
34path(store, e, forest).
35path(forest, w, store).
36path(tunnel, e, entrance_wetland).
37path(entrance_wetland, w, tunnel).
38path(entrance_wetland, s, wetland).
39path(wetland, n, entrance_wetland).
40path(bridge, e, river).
41path(river, w, bridge).
42path(river, n, volcano).
43path(volcano, s, river).
44path(river, e, devil_monster).
45path(devil_monster, w, river).
46
49
50at(diamond, treasurechest).
51at(ruby, rockcave).
52at(magic_wand, crystalball).
53at(badge, forest).
55buy_only_at(grass, store).
56buy_only_at(vitamin, store).
57
58
60alive(devil_monster).
61
62
64
65take(X) :-
66 at(X, in_hand),
67 write('You''re already holding it!'),
68 nl, !.
69
70take(X) :-
71 i_am_at(Place),
72 at(X, Place),
73 retract(at(X, Place)),
74 assert(at(X, in_hand)),
75 write('OK.'),
76 nl, !.
77
78take(_) :-
79 write('I don''t see it here.'),
80 nl.
81
82
84
85drop(X) :-
86 at(X, in_hand),
87 i_am_at(Place),
88 retract(at(X, in_hand)),
89 assert(at(X, Place)),
90 write('OK.'),
91 nl, !.
92
93drop(_) :-
94 write('You aren''t holding it!'),
95 nl.
96
98
99sell(X) :-
100 i_am_at(store),
101 at(X, in_hand),
102 (at(diamond, in_hand) ; at(ruby, in_hand)),
103 retract(at(X, in_hand)),
104 retract(money(M)),
105 N is M + 100,
106 assert(money(N)),
107 write('Your balance is: '),
108 write(N), nl, !.
109sell(X) :-
110 i_am_at(store),
111 at(X, in_hand),
112 write('You can not sell this item at store!'), nl, !.
113sell(_) :-
114 i_am_at(market),
115 write('You don''t have it!'), nl, !.
116sell(_) :-
117 write('You''re not at the store!'),
118 nl.
119
121
122buy(X) :-
123 i_am_at(store),
124 buy_only_at(X, store),
125 money(M), M >= 100,
126 retract(buy_only_at(X, store)),
127 assert(at(X, in_hand)),
128 retract(money(M)),
129 N is M - 100,
130 assert(money(N)),
131 write('Your balance is : '),
132 write(N), nl, !.
133buy(X) :-
134 i_am_at(store),
135 buy_only_at(X, store),
136 write('You don''t have enough money!'), nl, !.
137buy(_) :-
138 i_am_at(store),
139 write('This item is not available in the store!'), nl, !.
140buy(_) :-
141 write('You''re not at the store!'),
142 nl.
143
144
146
147n :- go(n).
148
149s :- go(s).
150
151e :- go(e).
152
153w :- go(w).
154
156display :-
157 at(X, in_hand),
158 write(X), nl, fail.
159
161
162go(Direction) :-
163 i_am_at(Here),
164 path(Here, Direction, There),
165 retract(i_am_at(Here)),
166 assert(i_am_at(There)),
167 !, look.
168
169go(_) :-
170 write('You can''t go that way.').
171
172
174
175look :-
176 i_am_at(Place),
177 describe(Place),
178 nl,
179 notice_objects_at(Place),
180 nl.
181
182
185
186notice_objects_at(Place) :-
187 at(X, Place),
188 write('There is a '), write(X), write(' here.'), nl,
189 fail.
190
191notice_objects_at(_).
192
194
195kill :-
196 i_am_at(wetland),
197 write('There is a spider here, you died!'), nl,
198 !, die.
199kill :-
200 i_am_at(devil_monster),
201 at(magic_wand, in_hand),
202 retract(alive(devil_monster)),
203 write('You hack repeatedly at the devil_monster''s back. Slimy ichor'), nl,
204 write('gushes out of the devil_monster''s back, and gets all over you.'), nl,
205 write('I think you have killed it, despite the continued twitching.'), nl,
206 write('You successfully rescue the princess, congratulations!'), nl,
207 finish, !.
208kill :-
209 write('I see nothing inimical here.'), nl.
210kill :-
211 i_am_at(devil_monster),
212 write('Run! You can not win the devil_monster without the magic_wand.'), nl.
213
215
216die :-
217 finish.
218
223
224finish :-
225 nl,
226 write('The game is over. Please enter the "halt." command.'),
227 nl.
228
229
231
232instructions :-
233 nl,
234 write('Enter commands using standard Prolog syntax.'), nl,
235 write('Available commands are:'), nl,
236 write('start. -- to start the game.'), nl,
237 write('n. s. e. w. -- to go in that direction.'), nl,
238 write('take(Object). -- to pick up an object.'), nl,
239 write('drop(Object). -- to put down an object.'), nl,
240 write('sell(Object). -- to sell an item at the store.'), nl,
241 write('buy(Object). -- to buy an item at the store.'), nl,
242 write('kill -- to attack an enemy.'), nl,
243 write('look. -- to look around you again.'), nl,
244 write('display. -- to display the item you are holding.'), nl,
245 write('instructions. -- to see this message again.'), nl,
246 write('halt. -- to end the game and quit.'), nl,
247 nl.
248
249
251
252start :-
253 instructions,
254 look.
255
256
259
260describe(treasurechest) :-
261 at(diamond, in_hand),
262 write('You are now in a treasure chest.'), nl,
263 write('You have got the diamond, the chest is empty now.'), nl,
264 write('To the east is the tunnel.'), nl, !.
265
266describe(treasurechest) :-
267 write('You are now in a treasure chest.'), nl,
268 write('Please take the diamond.'), nl,
269 write('To the east is the tunnel.'), nl.
270
271describe(crystalball) :-
272 at(magic_wand, in_hand),
273 write('You are in a crystal ball.'), nl,
274 write('You have got the magic_wand, the crystalball is empty now.'), nl,
275 write('To the north is the locked tunnel.'), nl, !.
276
277describe(crystalball) :-
278 write('You are in a crystal ball.'), nl,
279 write('Please take the magic_wand.'), nl,
280 write('To the north is the locked tunnel.'), nl.
281
282describe(tunnel) :-
283 write('You are in a tunnel.'), nl,
284 write('You give your badge to the guardian angle of the tunnel, you can enter the tunnel now'), nl,
285 write('To the west is the treasure chest.'), nl,
286 write('To the east is the entrance of wetland.'), nl,
287 write('To the south is the crystal ball.'), nl,
288 write('To the north is a unlocked small door.'), nl.
289
290describe(smalldoor) :-
291 write('You are in a small unlocked door.'), nl,
292 write('To the west is the rock cave.'), nl,
293 write('To the east is the log cabin.'), nl,
294 write('To the south is the locked tunnel.'), nl.
295
296describe(rockcave) :-
297 at(ruby, in_hand),
298 write('You are in a rock cave.'), nl,
299 write('You have got the ruby and the rock cave is empty now'), nl,
300 write('To the east is a small unlocked door'), nl, !.
301
302describe(rockcave) :-
303 write('You are in a rock cave please take the ruby.'), nl,
304 write('To the east is a small unlocked door'), nl.
305
306describe(logcabin) :-
307 write('You are in a log cabin.'), nl,
308 write('To the west is a unlocked small door.'), nl,
309 write('To the east is the bridge which is the road to the LOST LAND.'), nl,
310 write('To the north is the store.'), nl.
311
312describe(store) :-
313 write('You are at a store.'), nl,
314 write('Ruby worth 100 golden coins, diamond worth 100 golden coins.'), nl,
315 write('grass costs 100 golden coins, vitamin costs 100 golden coins.'), nl,
316 write('Your balance is: '), money(X), write(X), write(' golden coins'), nl,
317 write('Please sell things to earn money or just buy something.'), nl,
318 write('To the south is the log cabin, to the east is the forest.'), nl.
319
320describe(forest) :-
321 at(badge, in_hand),
322 write('You are in the forest.'), nl,
323 write('You have got the badge and forest is empty now'), nl,
324 write('To the west is the store'), nl, !.
325
326describe(forest) :-
327 write('You are in forest and please take the badge.'), nl,
328 write('To the west is the store.'), nl.
329
330describe(bridge) :-
331 write('To the west is the log cabin.'), nl,
332 write('To the east is the river.'), nl.
333
334describe(entrance_wetland) :-
335 at(grass, in_hand),
336 at(vitamin, in_hand),
337 write('You are at entrance of wetlan. You have got the horse. It''s empty now'), nl,
338 write('To the west is the locked tunnel.'), nl,
339 write('To the south is the wetland. It''s dangerous! '), nl,
340 assert(at(horse, in_hand)), !.
341
342describe(entrance_wetland) :-
343 at(horse, in_hand),
344 write('You are at entrance of wetlan. You notice the horse is hidden in this place.'), nl,
345 write('You use the mixed vitamin grass to allure the horse then get it.'), nl,
346 write('To the west is the locked tunnel.'), nl,
347 write('To the south is the wetland. It''s dangerous! '), nl, !.
348
349describe(entrance_wetland) :-
350 at(grass, in_hand),
351 write('You are at entrance of wetlan. Someting hidden here! Try to find it! The grass is poinsonous'), nl,
352 write('clue: vitamin'), nl,
353 write('To the west is the locked tunnel.'), nl,
354 write('To the south is the wetland. It''s dangerous! '), nl, !.
355
356describe(entrance_wetland) :-
357 write('You are at entrance of wetlan. Someting hidden here! Try to find it!'), nl,
358 write('clue: vitamin and grass.'), nl,
359 write('To the west is the locked tunnel.'), nl,
360 write('To the south is the wetland. It''s dangerous! '), nl.
361
362describe(wetland) :-
363 write('You are at the wetlan. It''s dangerous! This is a spider room! Kill or leave?'), nl,
364 write('To the west is the locked tunnel.'), nl,
365 write('To the north is the entrance of wetland.'), nl.
366
367describe(river) :-
368 write('You are at river now.'), nl,
369 write('To the north is the volcano, it''s dangerous.'), nl,
370 write('To the west is the bridge.'), nl,
371 write('To the east is the devil_monster.'), nl.
372
373describe(volcano) :-
374 write('You are at volcano now. You are unable to survive at volcano, the valcano '), nl,
375 write('is eruppting! You died!'), nl,
376 die.
377
378describe(devil_monster) :-
379 write('You meet the devil_monster, this is the final boss! Kill it or leave? If want to kill it , just type kill.'), nl,
380 write('To the west is the river.'), nl