2
3:- dynamic i_am_at/1, at/2, holding/1, path/3. 4:- retractall(at(_, _)), retractall(i_am_at(_)), retractall(alive(_)), retractall(holding(_)),
5 retractall(path(_,_,_)). 6
8dark(warehouse).
9dark(armor_house).
10dark(hidden_room).
11dark(kitchen).
12dark(chamber).
13
14bright(shop).
15bright(hall).
16bright(bedroom).
17bright(corridor_w).
18bright(corridor_e).
19bright(corridor_m).
20bright(bridge).
21
23path(corridor_w, e, corridor_m).
24path(corridor_m, e, corridor_e).
25path(corridor_m, w, corridor_w).
26path(corridor_e, w, corridor_m).
27
28path(corridor_w, s, warehouse).
29path(corridor_m, s, bedroom).
30path(corridor_e, s, armor_house).
31
32path(bedroom, n, corridor_m).
33path(armor_house, n, corridor_e).
34
35path(corridor_m, n, bridge).
37path(bridge, s, corridor_m).
38
39path(hall, n, entrance).
40path(hall, s, bridge).
41path(hall, w, chamber).
42path(hall, e, kitchen).
43
45path(entrance, w, shop).
46path(entrance, s, hall).
47
48path(shop, e, entrance).
49path(chamber, e, hall).
50path(kitchen, w, hall).
51
52path(hidden_room, s, kitchen).
53
54
56describe(warehouse) :- write('A warehouse, dirty and mess.').
57 describe(garbage) :- write('Some garbage lying on the ground.').
58 describe(ironwire) :- write('Some iron wire hidden in garbage.').
59 describe(battery) :- write('A flashlight battery.').
60 describe(door) :- write('A locked door, on the north side of the room.').
61 describe(unlocked_door) :- write('A unlocked door, n the north side of the room.').
62
63describe(corridor_w) :- write('A long corridor, it''s its west side.').
64describe(corridor_m) :- write('A long corridor, it''s its middle side.').
65describe(corridor_e) :- write('A long corridor, it''s its east side.').
66
67describe(bedroom) :- write('A luxury bedroom.').
68 describe(shelf) :- write('A shelf, with something on it.').
69 describe(candles(20)) :- write('Some candles, can be used for lighting.').
70 describe(redkey) :- write('A red key.').
71 describe(bed) :- write('A large bed made by red wood.').
72 describe(sheet) :- write('Bed sheet, made by wool.').
73 74 describe(burning_sheet) :- write('A burning... Run away!!!').
75 describe(fireplace) :- write('Fireplace on the wall, still burning.').
76 77 describe(snuffed_fireplace) :- write('The fireplace is snuffed.').
78 describe(firewood) :- write('Not elegent, but can be used as wood sticks').
79
80describe(armor_house) :- write('A place to store armors.').
81 describe(armor_suit) :- write('A status wearing armor suit.').
82 describe(largebox) :- write('Empty box.').
83 describe(bucket) :- write('Can be filled with water.').
84 describe(bag) :- write('A big bag, seems to be empty.').
85 86 describe(fullbucket) :- write('Bucket filled with water').
87
88describe(bridge) :- write('A bridge connects two parts of the castle.').
89 describe(bridgeguard) :- write('A guard at another end, stop anyone from passing').
90 describe(river) :- write('A river runs across the castle.').
91
92describe(shop) :- write('You may buy something here.').
93 94 95 describe(sale_sword) :- write('Great weapon., Price: 200').
96 describe(sale_gamehint) :- write('You can buy a game hint here, Price: 200').
97 describe(sale_candles) :- write('You can buy some candle here, Price: 200 for 10').
98
99describe(entrance) :- write('The entrance of the castle.').
100 describe(gateguard) :- write('Of course there is a guard at the entrance.').
101 describe(stone) :- write('This castle is build 200 years ago, it says.').
102
103describe(hidden_room) :- write('Congratulations, you get to the hidden room.').
104 describe(box) :- write('A box, but need password to open.').
105 describe(wall) :- write('If you are enough familiar with this castle, '), nl,
106 write('You would know the password. Just one letter.'), nl,
107 write('Use input(X) to input the letter, if it is uppercase'), nl,
108 write('Please input the corresponding lowercase.').
109 describe(openbox) :- write('It is open.').
110 describe(treasure) :- write('You made it.').
111
112describe(chamber) :- write('Office region. A large desk in the middle.').
113 describe(bookshelf) :- write('A tall bookshelf.').
114 describe(archbook) :- write('A book about British building style.').
115 describe(novel) :- write('Mastering C++ in 21 days.').
116 describe(note) :- write('I love the design of the castle, just as I love English.').
117 describe(desk) :- write('An office desk.').
118 describe(locked_drawer) :- write('Locked drawer.').
119 120 describe(drawer) :- write('A drawer, with some files in it.').
121 describe(money(100)) :- write('Some money.').
122 describe(mappart1) :- write('Some part of the map of the castle.').
123 describe(cabinet) :- write('A cabinet.').
124 describe(money(110)) :- write('Some money(2).').
125 describe(photo) :- write('A large photo hanging on the wall.').
126 describe(tied_greenkey) :- write('A green key. But it''s tied by a rope fixed on the wall.').
127 128 describe(greenkey) :- write('A green key.').
129
130describe(hall) :- write('What a great open area.').
131 describe(painting) :- write('Looks like a masterpiece.').
132 133 describe(cutpainting) :- write('Cut Painting. Something is hidden behind.').
134 describe(securitybox) :- write('A security box is embeded in the wall.').
135 136 describe(opened_securitybox) :- write('A opened security box.').
137 describe(mappart2) :- write('Another part of the map.').
138 describe(money(120)) :- write('Some money.').
139
140describe(kitchen) :- write('Lots of food.').
141 describe(goldplate) :- write('Maybe it can be used as money.').
142 describe(scissors) :- write('Ordinary thing in kitchen.').
143 describe(refrigerator) :- write('A large refrigerator in the kitchen').
144 describe(ice) :- write('Lots of ice...').
145 describe(worldmap) :- write('A map of world, hanging on the north wall.').
146 describe(flashlight_nopower) :- write('A flashlight, but run out of power').
147
148 describe(bagofice) :- write('Really heavy, can be used as a hammer.').
149
150describe(flashlight) :- write('A flashlight, candles are no longer useful.').
151describe(sword) :- write('A great weapon.').
152
154takeable(battery).
155takeable(ironwire).
156takeable(candles(_)).
157takeable(redkey).
158takeable(firewood).
159takeable(armor_suit).
160takeable(bag).
161takeable(bucket).
162takeable(fullbucket).
163takeable(sword).
164takeable(gamehint).
165takeable(treasure).
166takeable(archbook).
167takeable(novel).
168takeable(note).
169takeable(money(_)).
170takeable(greenkey).
171takeable(mappart1).
172takeable(mappart2).
173takeable(goldplate).
174takeable(scissors).
175takeable(ice).
176takeable(flashlight_nopower).
177takeable(sale_sword).
178takeable(sale_gamehint).
179takeable(sale_candles).
180
182describe_item(mappart1) :- nl,
183 write('+------------+---- ---+-------------+'), nl,
184 write('| shop |'), nl,
185 write('+------------+----- ----+-- --+'), nl,
186 write('| chamber kitchen |'), nl,
187 write('+------------+---- --+'), nl,
188 write(' | bridge | '), nl,
189 write('+------------+---- -----+-------------+'), nl,
190 write('| corridor_w co _e |'), nl,
191 write('+----- ----+---- ---- ----- '), nl,
192 write('| warehouse | |'), nl,
193 write('+------------+------------+-------------+'), !.
194
195describe_item(mappart2) :- nl,
196 write('+------------+---- ---+-------------+'), nl,
197 write('| en_room |'), nl,
198 write('+------------+----- ----+-------------+'), nl,
199 write('| r hall |'), nl,
200 write('+------------+---- -----+-------------+'), nl,
201 write(' | bridge | '), nl,
202 write('+------------+---- -----+-------------+'), nl,
203 write('| c _w corri m corridor_e |'), nl,
204 write('+----- ----+---- -----+----- -----+'), nl,
205 write('| | bedroom | armor_house |'), nl,
206 write('+------------+------------+-------------+'), !.
207
208
209describe_item(map) :- nl,
210 write('+------------+---- ---+-------------+'), nl,
211 write('| shop entrance | hidden_room |'), nl,
212 write('+------------+----- ----+-------------+'), nl,
213 write('| chamber hall kitchen |'), nl,
214 write('+------------+---- -----+-------------+'), nl,
215 write(' | bridge | '), nl,
216 write('+------------+---- -----+-------------+'), nl,
217 write('| corridor_w corridor_m corridor_e |'), nl,
218 write('+----- ----+---- -----+----- -----+'), nl,
219 write('| warehouse | bedroom | armor_house |'), nl,
220 write('+------------+------------+-------------+'), !.
221
222describe_item(candles(X)) :- write('You have '), write(X), write(' candles left.'), !.
223describe_item(money(X)) :- write('You have '), write(X), write(' dollars left.'), !.
224
225describe_item(gamehint) :- write('Have you noticed that a bag of ice can break the world map in the kitchen?').
226
227describe_item(Obj) :-
228 describe(Obj).
229
231dark_visible(warehouse).
232dark_visible(garbage).
233dark_visible(ironwire).
235dark_visible(door).
236dark_visible(unlocked_door).
237dark_visible(corridor_w).
238dark_visible(corridor_m).
239dark_visible(corridor_e).
240dark_visible(bedroom).
241dark_visible(shelf).
242dark_visible(candles(_)).
243dark_visible(redkey).
244dark_visible(bed).
245dark_visible(sheet).
246dark_visible(burning_sheet).
247dark_visible(fireplace).
248dark_visible(snuffed_fireplace).
249dark_visible(firewood).
250dark_visible(armor_house).
251dark_visible(bridge).
252dark_visible(bridgeguard).
253dark_visible(river).
254dark_visible(shop).
256dark_visible(sale_sword).
257dark_visible(sale_gamehint).
258dark_visible(sale_candles).
259dark_visible(entrance).
260dark_visible(money(_)).
261dark_visible(gateguard).
262dark_visible(stone).
263dark_visible(chamber).
264dark_visible(hall).
265dark_visible(painting).
266dark_visible(cutpainting).
267dark_visible(securitybox).
268dark_visible(openedsecuritybox).
269dark_visible(mappart2).
270dark_visible(kitchen).
271
272
274at(garbage, warehouse).
275 at(battery, garbage).
276 at(ironwire, garbage).
277at(door, warehouse).
279
280at(shelf, bedroom).
281 at(candles(20), shelf).
282 at(redkey, shelf).
283at(bed, bedroom).
284 at(sheet, bed).
285at(fireplace, bedroom).
287 at(firewood, snuffed_fireplace).
288
289at(armor_suit, armor_house).
290at(largebox, armor_house).
291at(bucket, armor_house).
292at(bag, armor_house).
293
294at(bridgeguard, bridge).
295at(river, bridge).
296
297at(sale_sword, shop).
298at(sale_gamehint, shop).
299at(sale_candles, shop).
300
301at(gateguard, entrance).
302at(stone, entrance).
303
304at(box, hidden_room).
305at(wall, hidden_room).
307at(treasure, openbox).
308
309at(bookshelf, chamber).
310 at(archbook, bookshelf).
311 at(novel, bookshelf).
312 at(note, bookshelf).
313at(desk, chamber).
314 at(locked_drawer, desk).
315 316 at(money(100), drawer).
317 at(mappart1, drawer).
318at(cabinet, chamber).
319 at(money(110), cabinet).
320at(photo, chamber).
321 at(tied_greenkey, photo).
322 323
324at(painting, hall).
326 at(securitybox, cutpainting).
327 328 at(mappart2, opened_securitybox).
329 at(money(120), opened_securitybox).
330
331at(goldplate, kitchen).
332at(scissors, kitchen).
333at(refrigerator, kitchen).
334 at(ice, refrigerator).
335at(flashlight_nopower, kitchen).
336at(worldmap, kitchen).
337
338
339open_bridge :-
340 retract(at(bridgeguard, bridge)), 341 assert(path(bridge, n, hall)).
342
343
346use(ironwire, door) :-
347 holding(ironwire), at(door, warehouse),
348 retract(at(door, warehouse)),
349 assert(at(unlocked_door, warehouse)),
350 assert(path(warehouse, n, corridor_w)),
351 write('The door is unlocked now.'), nl, !.
352
353
355use(candles(X), sheet) :-
356 holding(candles(X)), X > 0, at(sheet, bed),
357 retract(at(sheet, bed)),
358 assert(at(burning_sheet, bed)),
359 open_bridge,
360 write('You burned the sheet on bed, guards will come soon.'), nl, !.
361
363use(fullbucket, fireplace) :-
364 holding(fullbucket), at(fireplace, bedroom),
365 retract(at(fireplace, bedroom)),
366 retract(holding(fullbucket)),
367 assert(holding(bucket)),
368 assert(at(snuffed_fireplace, bedroom)),
369 write('The fireplace is snuffed.'), nl, !.
370
372use(bucket, river) :-
373 holding(bucket),
374 retract(holding(bucket)),
375 assert(holding(fullbucket)),
376 write('Filled the bucket with water.'), nl, !.
377
379use(firewood, bridgeguard) :-
380 holding(firewood), at(bridgeguard, bridge),
381 retract(holding(firewood)),
382 open_bridge,
383 write('Lucky! The bridge guard falls into the river.'), nl, !.
384
386use(sword, gateguard) :-
387 holding(sword), at(gateguard, entrance),
388 retract(at(gateguard, entrance)),
389 assert(path(entrance, n, outside)),
390 write('You defeated the gate guard. The entrance is open now.'), nl, !.
391
393use(redkey, locked_drawer) :-
394 holding(redkey), at(locked_drawer, desk),
395 retract(holding(redkey)),
396 retract(at(locked_drawer, desk)),
397 assert(at(drawer, desk)),
398 write('The drawer is unlocked now.'), nl, !.
399
401use(scissors, tied_greenkey) :-
402 holding(scissors), at(tied_greenkey, photo),
403 retract(at(tied_greenkey, photo)),
404 assert(at(greenkey, photo)),
405 write('The green key is cut off from the role now.'), nl, !.
406
408use(scissors, painting) :-
409 holding(scissors), at(painting, hall),
410 retract(at(painting, hall)),
411 assert(at(cutpainting, hall)),
412 write('You split the painting into two pieces.'), nl, !.
413
415use(greenkey, securitybox) :-
416 holding(greenkey), at(securitybox, cutpainting),
417 retract(at(securitybox, cutpainting)),
418 assert(at(opened_securitybox, cutpainting)),
419 write('The security box is unlocked.'), nl, !.
420
422use(mappart1, mappart2) :-
423 holding(mappart1), holding(mappart2),
424 retract(holding(mappart1)), retract(holding(mappart2)),
425 assert(holding(map)),
426 write('You get the whole map of the castle.'), nl, !.
427
429use(ice, bag) :-
430 holding(ice), holding(bag),
431 retract(holding(ice)), retract(holding(bag)),
432 assert(holding(bagofice)),
433 write('You put all ice into the bag.'), nl, !.
434
436use(bagofice, worldmap) :-
437 holding(bagofice), at(worldmap, kitchen),
438 retract(at(worldmap, kitchen)),
439 retract(holding(bagofice)),
440 assert(path(kitchen, n, hidden_room)),
441 write('You throw the bag of ice to the wall...'), nl,
442 write('A huge hole appears on the wall, what''s in it?'), nl, !.
443
445use(battery, flashlight_nopower) :-
446 holding(battery), holding(flashlight_nopower),
447 retract(holding(battery)), retract(holding(flashlight_nopower)),
448 assert(holding(flashlight)),
449 write('Get battery on. The flashlight works!'), nl, !.
450
451use(_, _) :-
452 write('Nothing happens.'), nl.
453
455i_am_at(warehouse).
456holding(candles(0)).
457holding(money(0)).
458
460
461within(Object, Location) :- at(Object, Location).
462within(Object, Location) :- at(Object, Place), within(Place, Location).
463
467take(sale_sword) :-
468 i_am_at(shop), at(sale_sword, shop),
469 holding(money(X)), X < 200,
470 write('Not enough money.'), !, nl.
471
472take(sale_sword) :-
473 i_am_at(shop), at(sale_sword, shop),
474 holding(money(X)), X >= 200,
475 retract(holding(money(X))), NewX is X - 200,
476 assert(holding(money(NewX))),
477 assert(holding(sword)),
478 retract(at(sale_sword, shop)),
479 !, nl.
480
481take(sale_gamehint) :-
482 i_am_at(shop), at(sale_gamehint, shop),
483 holding(money(X)), X < 200,
484 write('Not enough money.'), !, nl.
485
486take(sale_gamehint) :-
487 i_am_at(shop), at(sale_gamehint, shop),
488 holding(money(X)), X > 200,
489 retract(holding(money(X))), NewX is X - 200,
490 assert(holding(money(NewX))),
491 assert(holding(gamehint)),
492 retract(at(sale_gamehint, shop)),
493 !, nl.
494
495take(sale_candles) :-
496 i_am_at(shop), at(sale_candles, shop),
497 holding(money(X)), X < 200,
498 write('Not enough money.'), !, nl.
499
500take(sale_candles) :-
501 i_am_at(shop), at(sale_candles, shop),
502 holding(money(X)), X > 200,
503 retract(holding(money(X))), NewX is X - 200,
504 assert(holding(money(NewX))),
505 holding(candles(Y)),
506 retract(holding(candles(Y))), NewY is Y + 10,
507 assert(holding(candles(NewY))),
508 !, nl.
509
511take(candles(X)) :-
512 i_am_at(Location), within(candles(X), Location), 513 retractall(at(candles(X), _)),
514 holding(candles(Num)),
515 retract(holding(candles(Num))),
516 NewNum is Num + X,
517 assert(holding(candles(NewNum))),
518 write('Get it.'),
519 !, nl.
520
521take(money(X)) :-
522 i_am_at(Location), within(money(X), Location), 523 retractall(at(money(X), _)),
524 holding(money(Num)),
525 retract(holding(money(Num))),
526 NewNum is Num + X,
527 assert(holding(money(NewNum))),
528 write('Get it.'),
529 !, nl.
530
531take(goldplate) :-
532 i_am_at(Location), within(goldplate, Location),
533 retractall(at(goldplate, _)),
534 holding(money(Num)),
535 retract(holding(money(Num))),
536 NewNum is Num + 200,
537 assert(holding(money(NewNum))),
538 write('Get it.'),
539 !, nl.
540
541
542take(X) :-
543 holding(X),
544 write('You''re already holding it!'),
545 !, nl.
546
547take(X) :-
548 i_am_at(Location), within(X, Location), 549 takeable(X), 550 retractall(at(X, _)),
551 assert(holding(X)),
552 write('Get it.'),
553 !, nl.
554
555take(X) :-
556 i_am_at(Location), within(X, Location), 557 \+takeable(X), 558 write('Sorry, you cannot take it.'),
559 !, nl.
560
561take(_) :-
562 write('I do not see it here.'),
563 nl.
564
566n :- go(n).
567s :- go(s).
568e :- go(e).
569w :- go(w).
570
572subtract_candles :-
573 holding(candles(X)), X > 0,
574 retract(holding(candles(X))), NewX is X -1,
575 assert(holding(candles(NewX))).
576subtract_candles.
577
579go(n) :-
580 i_am_at(entrance),
581 path(entrance, n, outside),
582 write('Sucessfully escaped. You win!'), nl,
583 win_judge, nl, !.
584
585go(w) :-
586 i_am_at(entrance),
587 path(entrance, w, shop),
588 \+ holding(armor_suit),
589 write('You must wear an armor suit. Otherwise, the shop keeper would know you are escaping.'), nl,
590 !.
591
592go(Direction) :-
593 i_am_at(Here),
594 path(Here, Direction, There),
595 retract(i_am_at(Here)),
596 assert(i_am_at(There)),
597 write('Go to '), write(There), nl,
598 subtract_candles,
599 !.
600go(_) :-
601 write('You can''t go that way.').
602
604input(i) :-
605 retract(at(box, hidden_room)),
606 assert(at(openbox, hidden_room)),
607 write('Correct.'), !.
608
609input(_) :-
610 write('Incorrect.'), !.
611
613look :-
614 i_am_at(Place),
615 describe_ex(Place),
616 nl,
617 check, !.
618
620lightable :- (
621 (holding(candles(X)), X > 0);
622 holding(flashlight)
623 ), !.
624
625check :-
626 i_am_at(Location),
627 check(Location).
628
629check(Place) :- 630 \+ lightable,
631 at(X, Place), dark_visible(X),
632 write(X), write(' -- '), describe(X), nl,
633 fail.
634
635check(Place) :-
636 lightable,
637 at(X, Place),
638 write(X), write(' -- '), describe(X), nl,
639 fail.
640
641check(_).
642
644status :-
645 i_am_at(Place),
646 write('You are at '), write(Place), nl,
647 write('Your items:'), nl,
648 holding(Obj),
649 write(Obj), write(' -- '), describe_item(Obj), nl,
650 fail.
651
652status.
653
654
656win_judge :- holding(treasure),
657 write('You got the treasure, perfect!'),
658 finish, !.
659
660win_judge :-
661 write('There is some hidden treasure inside the castle.'), nl,
662 write('Can you find it?'),
663 finish, !.
664
666die :-
667 finish.
668
670describe_ex(Someplace) :- write('You are at '), write(Someplace), nl,
671 describle_darkness(Someplace),
672 describe(Someplace).
673
674describle_darkness(Someplace) :- dark(Someplace),
675 write('It is really dark in this room.'), nl.
676describle_darkness(Someplace) :- \+ dark(Someplace),
677 write('It is bright in this room.'), nl.
678
679
680
685finish :-
686 nl,
687 write('The game is over. Please enter the "halt." command.'),
688 nl.
689
691instructions :-
692 nl,
693 write('Welcome to Escaping The CASTLE game.'), nl,
694 write('You got trapped in a castle. Trying to escape.'), nl,
695 write('Your goal is to find out items that helps you to escape.'), nl,
696 write('Get some treasure back if you are lucky enough.'), nl,nl,
697 write('These are operations you could take :'), nl,
698 write('start. -- to start the game.'), nl,
699 write('n. s. e. w. -- to go in that direction.'), nl,
700 write('take(Object). -- to pick up an object.'), nl,
701 write('look. -- to look at the room.'), nl,
702 write('check(Place). -- to check what are over there.'), nl,
703 write('inventory. -- to list all your belongs.'), nl,
704 write('use(ObjA, ObjB). -- all actions, including combine things, unlock doors...'), nl,
705 write('halt. -- to end the game and quit.'), nl,nl,
706 write('Remember:'), nl,
707 write('If a room is too dark, you cannot see something.'), nl,
708 write('Candles will be running out when moving.'), nl,
709 write('Guards would let you pass unless you defeat them.'), nl,
710 nl.
711
712introduction :-
713 write('When I wake up, I found my self in a warehouse.'), nl,
714 write('The only thing I could remember is that I went to an old castle.'), nl,
715 write('But got trapped and lost conciousness.'), nl,
716 write('Where am I? It is really dark around.'), nl,
717 write('I can only see a ray of light shines through the closed door.'), nl,
718 nl.
719
720inventory :- status.
722
723start :-
724 instructions,
725 nl,
726 introduction