3
4:- dynamic at/2, i_am_at/1, my_fuel/1. 5:- retractall(at(_, _)), retractall(i_am_at(_)), retractall(fuel(_)). 6
8
9i_am_at(mercury).
10
12fuel(200).
13
15checkfuel(Fuel) :- Fuel > 0.
16
17
19
21path(mercury, e, polestar).
22path(polestar, w, mercury).
23
25path(mercury, w, venus).
26path(venus, e, mercury).
27
28
30path(mercury, s, sun) :- at(lifejacket, in_hand).
31path(mercury, s, sun) :-
32 write('Going to sun without life jacket? Are you crazy?'), nl,
33 !, fail.
34path(sun, n, mercury).
35
36
38path(mercury, n, earth) :- at(fire, in_hand).
39path(mercury, n, earth) :- write('The dragon will not allow you without fire in hand.'), nl,
40 !, fail.
41path(earth, s, mercury).
42
43
46path(venus, s, milkyway) :- at(bullets, in_hand).
47path(venus, s, milkyway) :-
48 write('The gun is not filled with enough bullets.'), nl,
49 !, fail.
50path(milkyway, n, venus).
51
52
55path(mercury, s, sun) :- at(lifejacket, in_hand).
56path(mercury, s, sun) :-
57 write('Going to sun without life jacket? Are you crazy?'), nl,
58 !, fail.
59
60
63path(venus, w, box) :- at(starcount, in_hand).
64path(venus, w, box) :-
65 write('You do not have the star count yet.'), nl,
66 fail.
67path(box, e, venus).
68
71
72at(lifejacket, box).
73at(fire, sun).
74at(bullets, polestar).
75at(starcount, milkyway).
76
77
78
80
81take(X) :-
82 at(X, in_hand),
83 write('You''re already holding it!'),
84 nl, !.
85
86take(X) :-
87 i_am_at(Place),
88 at(X, Place),
89 retract(at(X, Place)),
90 assert(at(X, in_hand)),
91 write('OK.'),
92 nl, !.
93
94take(_) :-
95 write('I don''t see it here.'),
96 nl.
97
98
100
101drop(X) :-
102 at(X, in_hand),
103 i_am_at(Place),
104 retract(at(X, in_hand)),
105 assert(at(X, Place)),
106 write('OK.'),
107 nl, !.
108
109drop(_) :-
110 write('You aren''t holding it!'),
111 nl.
112
113
115
116n :- go(n).
117
118s :- go(s).
119
120e :- go(e).
121
122w :- go(w).
123
124
125
127
128go(Direction) :-
129 i_am_at(Here),
130 path(Here, Direction, There),
131 retract(i_am_at(Here)),
132 assert(i_am_at(There)),
133 fuel(Fuel),
134 checkfuel(Fuel),!,
135 NewFuel is Fuel - 10,nl,
136 write('Fuel left is '),
137 write(NewFuel),nl,
138 retract(fuel(Fuel)),
139 assert(fuel(NewFuel)),
140 look, !.
141
142go(_) :-
143 write('You can''t go that way.').
144
145
147
148look :-
149 i_am_at(Place),
150 describe(Place),
151 nl,
152 notice_objects_at(Place),
153 nl.
154
155
158
159notice_objects_at(Place) :-
160 at(X, Place),
161 write('There is a '), write(X), write(' here.'), nl,
162 fail.
163
164notice_objects_at(_).
165
166
167
168finish :-
169 nl,
170 write('The game is over. Please enter the halt. command.'),
171 nl, !.
172
173
174i :-
175 write('We found these items in your inventory: '), nl,
176 notice_objects_at(in_hand),nl,
177 nl.
178
179
181
182instructions :-
183 nl,
184 write('Enter commands using standard Prolog syntax.'), nl,
185 write('Available commands are:'), nl,
186 write('start. -- to start the game.'), nl,
187 write('n. s. e. w. -- to go in that direction.'), nl,
188 write('take(Object). -- to pick up an object.'), nl,
189 write('drop(Object). -- to put down an object.'), nl,
190 write('look. -- to look around you again.'), nl,
191 write('instructions. -- to see this message again.'), nl,
192 write('halt. -- to end the game and quit.'), nl,
193 nl.
194
196
197start :-
198 instructions,
199 look.
200
201
204
205describe(_) :-
206 fuel(Fuel),
207 \+ checkfuel(Fuel)
208 ,!,
209 write('Sorry ! Fuel is up'), nl,
210 write('and you lost the game.'), nl,
211 finish, !.
212
213describe(earth) :-
214 at(fire, in_hand),
215 write('Congratulations!! You have entered earth'), nl,
216 write('and won the game.'), nl,
217 finish, !.
218
219describe(mercury) :-
220 write('You are at mercury and have a spaceship which will take you along your journey.'),nl,
221 write('Be careful, you cannot travel once the fuel in the spaceship is over !!! '),nl,
222 write('To the north is earth in which you have to enter and be cautious, '), nl,
223 write('the dragon wont let you in without the fire'),nl,
224 write('To the south is sun from where you have to pick up fire and cannot '), nl,
225 write('enter without a life jacket. To the east is pole star which has'), nl,
226 write('bullets to fight the shooting stars on the way to milky way from venus'), nl,
227 write('which is south of venus. Go to the west of venus and there is locked box'), nl,
228 write('Star count of the stars at the milky way is the code key to open the box.'), nl.
229
230describe(venus) :-
231 write('You have to pick up lifejacket in the box which is towards west from here.'), nl,
232 write('The key of the box is count of stars at the milky way.'), nl,
233 write('You need enough bullets in the gun to fight on the way to milky way'), nl,
234 write('which is to the south.'), nl.
235
236describe(milkyway) :-
237 write('Take the count of stars from milkyway'), nl,
238 write('and then head back to venus!'), nl.
239
240describe(sun) :-
241 write('Procure fire from the sun, and now head to the mercury'), nl,
242 write('and earth from there, offer fire to the dragon.'), nl.
243
244describe(polestar) :-
245 write('You reached the polestar. Load your gun with bullets'), nl,
246 write('to fight shooting stars on the milky way, take bullets here and'), nl,
247 write('rush to west.'), nl.
248
249describe(box) :-
250 write('Take life jacket from here'), nl,
251 write('and rush to the sun to take fire.'), nl