2
3:- dynamic i_am_at/1, at/2, holding/2, alive/1,energy/1, wearing/2. 4:- retractall(at(_, _)), retractall(i_am_at(_)), retractall(alive(_)). 5
7i_am_at(home).
8alive(barney).
9energy(100).
10
11
13
14path(home, d, mcLaren).
15path(mcLaren, u, home).
16
17path(closet, w, home).
18path(chest, n, home).
19path(home,s,chest).
20
21path(home, e, closet) :- holding(key,in_hand).
22path(home, e, closet) :-
23 write('The door appears to be locked.'), nl,
24 fail.
25
26path(mcLaren, e, restaurant).
27path(restaurant, w, mcLaren).
28
29path(restaurant, s, barney).
30path(barney, n, restaurant).
31
32path(closet, e, drawer):-holding(suit,in_hand).
33path(closet, e, drawer):-
34 write('How do you pick a tie without a suit?'),nl,
35 fail.
36path(drawer,w,closet).
37path(robins_house_enterance, w, restaurant).
38
39path(robins_house_enterance, s, robins_house).
40
41path(restaurant, e, robins_house_enterance):-
42 holding(blue_french_horn,in_hand),
43 wearing(suit,tie),
44 holding(ring,in_hand).
45
46path(restaurant, e, robins_house_enterance):-
47 holding(blue_french_horn,in_hand),
48 wearing(tie,suit),
49 holding(ring,in_hand).
50
51path(restaurant, e, robins_house_enterance):-
52 holding(blue_french_horn,in_hand),
53 wearing(suit,tie),
54 write('What are you going to propose her without a ring!?'), nl,
55 write('That''s a NO!'), nl,
56 fail.
57
58path(restaurant, e, robins_house_enterance):-
59 holding(blue_french_horn,in_hand),
60 wearing(tie,suit),
61 write('What are you going to propose her without a ring!?'), nl,
62 write('That''s a NO!'), nl,
63 fail.
64
65path(restaurant, e, robins_house_enterance):-
66 holding(ring,in_hand),
67 wearing(suit,tie),
68 write('Thats not you Ted, there is something missing?'), nl,
69 write('How about the blue french horn!'), nl,
70 fail.
71
72path(restaurant, e, robins_house_enterance):-
73 holding(ring,in_hand),
74 wearing(tie,suit),
75 write('Thats not you Ted, there is something missing?'), nl,
76 write('How about the blue french horn!'), nl,
77 fail.
78
79
80path(restaurant, e, robins_house_enterance):-
81 holding(ring,in_hand),
82 holding(blue_french_horn,in_hand),
83 write('Where is the suit Mr.? Go get properly dressed'), nl,
84 !,fail.
85
86path(restaurant, e, robins_house_enterance):-
87 write('Yes there is something called true love! But is that how you want to do it?'), nl,
88 write('Gosh! Check!Do you have a ring? Are you wearing a suit with a matching tie? '),nl,
89 write('What about the blue french horn? '), nl,
90 fail.
91
92at(energy_drink, mcLaren).
93at(blue_french_horn, restaurant).
94at(suit,closet).
95at(key, chest).
96at(ring, barney).
97at(tie, drawer).
98
99
101
102check_energy(X):-
103 X\=0.
104
105energy_left:-
106 energy(X),
107 write('Energy left is: '),nl,
108 write(X),nl.
109
110take(X) :-
111 holding(X,in_hand),
112 write('You''re already holding it!'),
113 !, nl.
114
115take(X) :-
116 i_am_at(Place),
117 at(X, Place),
118 retract(at(X, Place)),
119 assert(holding(X,in_hand)),
120 write('OK.'),
121 !, nl.
122
123take(_) :-
124 write('I don''t see it here.'),
125 nl.
126
127
129
130drop(X) :-
131 holding(X,in_hand),
132 i_am_at(Place),
133 retract(holding(X,in_hand)),
134 assert(at(X, Place)),
135 write('OK.'),
136 !, nl.
137
138drop(_) :-
139 write('You aren''t holding it!'),
140 nl.
141
142
144
145n :- go(n).
146
147s :- go(s).
148
149e :- go(e).
150
151w :- go(w).
152
153u :- go(u).
154
155d :- go(d).
156
157
159
160go(Direction) :-
161 i_am_at(Here),
162 path(Here, Direction, There),
163 retract(i_am_at(Here)),
164 assert(i_am_at(There)),
165 energy(Initial),
166 check_energy(Initial),!,
167 Current is Initial-10,
168 retract(energy(Initial)),
169 assert(energy(Current)),
170 energy_left,
171 nl,
172 !, look.
173
174go(_) :-
175 write('You can''t go that way.').
176
177
179
180look :-
181 i_am_at(Place),
182 describe(Place),
183 nl,
184 notice_objects_at(Place),
185 nl.
186
187
190
191notice_objects_at(Place) :-
192 at(X, Place),
193 write('There is a '), write(X), write(' here.'), nl,
194 fail.
195
196
197notice_objects_at(_).
198
199notice_inventory(X):-
200 holding(F,X),
201 write(F),nl,fail.
202
203
204notice_inventory(_).
205
206
208
209die :-
210 !, finish.
215
216finish :-
217 nl,
218 write('The game is over. Please enter the halt. command.'),
219 nl, !.
220
221hit :-
222 i_am_at(barney),
223 holding(blue_french_horn,in_hand),
224 retract(alive(barney)),
225 write('You hit barney with the blue french horn....'), nl,
226 write('he seems unconcious.'), nl,
227 write('OMG!lets get out of the restaurant from the north.'),
228 nl, !.
229
230hit :-
231 i_am_at(barney),
232 write('Barney is stronger than you! Punching him won''t help!'), nl.
233hit :-
234 write('I see nothing to hit here.'), nl.
235
236
237wear(X,Y) :-
238 holding(X,in_hand),
239 holding(Y,in_hand),
240 assert(wearing(X,Y)),!,
241 retract(holding(X,in_hand)),
242 retract(holding(Y,in_hand)),
243 write('great that just fits perfect!'),nl.
244
245
246wear(X,Y) :-
247 wearing(X,Y),
248 write('You''re already wearing it!'),
249 !,nl.
250
251wear(_,_):-
252 write('You don''t have both your tie and suit! Get them together!'),nl.
253
254inventory:-
255 write('You have the following items with you!'),nl,
256 notice_inventory(in_hand),nl.
257
258i:-
259 write('You have the following items with you!'),nl,
260 notice_inventory(in_hand),nl.
261drink:-
262 write('You are using your energy drink'),nl,
263 energy(Initial),
264 check_energy(Initial),!,
265 Current is Initial+50,
266 retract(energy(Initial)),
267 assert(energy(Current)),nl,
268 retract(holding(energy_drink,in_hand)),nl,
269 energy_left,nl.
270
272
273instructions :-
274 nl,
275 write('Enter commands using standard Prolog syntax.'), nl,
276 write('Available commands are:'), nl,
277 write('start. -- to start the game.'), nl,
278 write('n. s. e. w. u. d. -- to go in that direction.'), nl,
279 write('take(Object). -- to pick up an object.'), nl,
280 write('drop(Object). -- to put down an object.'), nl,
281 write('look. -- to look around you again.'), nl,
282 write('inventory. -- check inventory.'),nl,
283 write('energy_left. -- check energy left.'),nl,
284 write('drink. -- use energy_drink.'),nl,
285 write('i. -- check inventory.'),nl,
286 write('wear(Object,Object).-- to wear clothes.'),nl,
287 write('hit. -- to attack an enemy.'), nl,
288 write('instructions. -- to see this message again.'), nl,
289 write('halt. -- to end the game and quit.'), nl,
290 nl.
291
292
294
295start :-
296 instructions,
297 look.
298
299describe(_):-
300 energy(Initial),
301 \+check_energy(Initial),
302 write('Sorry no energy left! You lose!'),nl,
303 write('Today is not the day you propose Robin!'),nl,
304 finish,!.
305
306
309describe(mcLaren) :-
310 write('You are at McLarens!'),nl,
311 write('There is a restaurant on the east.'),nl,
312 write('Your home is above.'),nl.
313
314describe(robins_house) :-
315 write('Congratulations!! You have got Robin a blue french horn and the ring! She is all impressed and wants to mary you!'), nl,
316 write('You won the game.'),nl,
317 finish, !.
318
319describe(chest):-
320 write('You are at the chest!'),nl,
321 write('There is the home enterance to the north.'),nl.
322
323describe(home) :-
324 write('You are in your home. Its a wonderful day outside!'),nl,
325 write('Lets ask out Robin today! Did that make you a little nervous?'),nl,
326 write('Let the blue french horn do its trick! Lets get it from the restaurant!'), nl,
327 write('Lets suit up, get the blue frnch horn from the restaurant, a ring and hit Robins house.'), nl,
328 write('McLarens, your favorite hangout place, is below your house.'),nl,
329 write('There is a closet to your east and something glittery to your south.'),nl.
330
331describe(closet) :-
332 holding(key,in_hand),
333 write('Heres a closet with all your clothes!How about waearing a suit.'),nl,
334 write('Oh! no can''t as yet!Need a matching tie! There is a drawer to the east!'), nl,
335 write('The home enterance is to the west.'),nl.
336describe(closet):-
337 write('You are at the closet!'),nl,
338 write('The home enterance is to your west.'),nl.
339
340describe(drawer) :-
341 write('You are at the drawer.'),nl,
342 write('The closet is to your west.'),nl.
343
344describe(robins_house_enterance) :-
345 holding(blue_french_horn, in_hand),
346 holding(ring,in_hand),
347 wearing(_,_),
348 write('The moment of truth! You have got everything?'), nl,
349 write('There.. her room is on the south.'),nl.
350
351describe(restaurant) :-
352 alive(barney),
353 holding(ring,in_hand),
354 write('Barney saw you with the ring he was going to give Robin!!!'), nl,
355 write(' it is over now! '), nl,
356 die.
357
358describe(restaurant) :-
359 alive(barney),
360 write('You are at the restaurant! '), nl,
361 write('Mclarens is to your west.'), nl,
362 write('There is barney here on your south! '), nl,
363 write('Gosh! He is going to talk you out of it!'),nl.
364
365describe(restaurant) :-
366 write('You are at the restaurant! '), nl,
367 write('Mclarens is to your west.'), nl,
368 write('Robin''s house is to your east.'),nl.
369
370describe(barney) :-
371 alive(barney),
372 write('Heres Barney!'),nl,
373 write(' He is going to propose Robin first? You are a good guy Ted but you ought to do something about it! '),nl,
374 write('Knock him down. Well you got to do what you got to do!'),nl,
375 write('The restaurant is on your north.'),nl.
376
377describe(barney) :-
378 write('Barney is on the floor unconcious.'),nl