-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnani_search.pl
executable file
·388 lines (317 loc) · 6.86 KB
/
nani_search.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env swipl
:- dynamic
here/1,
location/2,
have/1,
turned_off/1,
move_count/1.
:- initialization(main, main).
main:-
nani_search.
nani_search:-
write('Your persona as the adventurer is that of a three year'),nl,
write('old. The Nani is your security blanket. It is getting'),nl,
write('late and you''re tired, but you can''t go to sleep'),nl,
write('without your Nani. Your mission is to find the Nani.'),nl, nl,
write('You control the game by using simple English commands'),nl,
write('expressing the action you wish to take. You can go to'),nl,
write('other rooms, look at your surroundings, look in things'),nl,
write('take things, drop things, eat things, inventory the'),nl,
write('things you have, and turn things on and off.'),nl, nl,
write('Hit any key to continue.'),get0(_),
write('Type "quit" if you give up.'),nl, nl, write('Enjoy the hunt.'),nl,
look,
command_loop.
% Initial assertions
room(kitchen).
room(cellar).
room(office).
room(hall).
room(bedroom).
room('dining room').
location(computer, office).
location(desk, office).
location(flashlight, desk).
location(envelope, desk).
location(stamp, envelope).
location(key, envelope).
location(apple, kitchen).
location(broccoli, kitchen).
location(crackers, kitchen).
location('washing machine', cellar).
location(nani, 'washing machine').
door(kitchen, cellar).
door(kitchen, office).
door(office, hall).
door(hall,'dining room').
door(bedroom, hall).
door('dining room', kitchen).
connect(X, Y):-
door(X, Y).
connect(X, Y):-
door(Y, X).
edible(apple).
edible(crackers).
tastes_yucky(brocolli).
turned_off(flashlight).
here(kitchen).
move_count(0).
% read a line of words from the user
read_list(L) :-
write('> '),
read_line(CL),
wordlist(L,CL,[]), !.
read_line(L) :-
get0(C),
buildlist(C,L).
buildlist(10,[]) :- !.
buildlist(C,[C|X]) :-
get0(C2),
buildlist(C2,X).
wordlist([X|Y]) --> word(X), whitespace, wordlist(Y).
wordlist([X]) --> whitespace, wordlist(X).
wordlist([X]) --> word(X).
wordlist([X]) --> word(X), whitespace.
word(W) --> charlist(X), {name(W,X)}.
charlist([X|Y]) --> chr(X), charlist(Y).
charlist([X]) --> chr(X).
chr(X) --> [X],{ X>=48 }.
whitespace --> whsp, whitespace.
whitespace --> whsp.
whsp --> [X], { X<48 }.
% NLP
det([the|X]- X).
det([a|X]-X).
det([an|X]-X).
verb(look, [look|X]-X).
verb(look, [look,around|X]-X).
verb(inventory, [inventory|X]-X).
verb(end, [end|X]-X).
verb(end, [quit|X]-X).
verb(end, [good,bye|X]-X).
verb(place, goto, [go,to|X]-X).
verb(place, goto, [go|X]-X).
verb(place, goto, [move,to|X]-X).
verb(place, goto, [X|Y]-[X|Y]):-room(X).
verb(place, goto, ['dining room'|Y]-['dining room'|Y]).
verb(thing, take, [take|X]-X).
verb(thing, put, [drop|X]-X).
verb(thing, put, [put|X]-X).
verb(thing, turn_on, [turn,on|X]-X).
verb(thing, turn_off, [turn,off|X]-X).
verb(thing, look_in, [look,in|X]-X).
verb(thing, look_in, [look,inside|X]-X).
noun(place, Place, [Place|X]-X):- room(Place).
noun(place, 'dining room', [dining,room|X]-X).
noun(thing, Thing, [Thing|X]-X):- location(Thing, _).
noun(thing, Thing, [Thing|X]-X):- have(Thing).
noun(thing, 'washing machine', [washing,machine|X]-X).
object(Type, Object, S1-S3):-
det(S1-S2),
noun(Type, Object, S2-S3).
object(Type, Object, S1-S2):-
noun(Type, Object, S1-S2).
get_command(C) :-
read_list(L),
command(CL,L),
C =.. CL,
!.
get_command(_) :-
write('I do not understand'), nl, fail.
command([V,O], InList) :-
verb(Object_Type, V, InList-S1),
object(Object_Type, O, S1-[]).
command([V], InList):-
verb(V, InList-[]).
% UI
command_loop:-
repeat,
get_command(X),
puzzle(X),
do(X),nl,
increment_move,
end_condition(X).
increment_move:-
move_count(X),
X1 is X + 1,
retract(move_count(X)),
assert(move_count(X1)).
end_condition(_):-
have(nani),
write('Congratulations').
end_condition(end):-
true.
end_condition(_):-
move_count(X),
X > 25,
write('You were too noisy. Your parents woke up and put you to bed without Nani :('),
!.
do(goto(X)):-
goto(X),
!.
do(go(X)):-
go(X),
!.
do(inventory):-
inventory,
!.
do(look):-
look,
!.
do(look_in(X)):-
look_in(X),
!.
do(take(X)):-
take(X),
!.
do(put(X)):-
put(X),
!.
do(end):-
true,
!.
do(turn_on(_)):-
turn_on,
!.
do(turn_off(_)):-
turn_off,
!.
do(_):-
write('Invalid command').
% Internal Commands
list_things(Place):-
location(X, Place),
tab(2),
write(X),
nl,
fail.
list_things(_):-
true, !.
:- op(35, fx, list_things).
list_connections(Place):-
connect(X, Place),
tab(2),
write(X),
nl,
fail.
list_connections(_):-
true, !.
:- op(35, fx, list_connections).
look:-
here(Place),
write('You are in the '), write(Place),nl,
write('You can see:'), nl,
list_things(Place),
write('You can go to'), nl,
list_connections(Place).
look_in(Place):-
list_things(Place).
:- op(35, fx, look_in).
goto(Place):-
can_go(Place),
move(Place),
look.
:- op(35, fx, goto).
go(Place):-
goto(Place).
:- op(35, fx, go).
can_go(Place):-
here(X),
connect(X, Place),
!.
can_go(_):-
write('You can not get there from here'),nl,
fail.
:- op(35, fx, can_go).
move(Place):-
retract(here(_)),
asserta(here(Place)).
:- op(35, fx, move).
take(Thing):-
can_take(Thing),
take_object(Thing),
!.
:- op(35, fx, take).
can_take(Thing):-
here(Place),
is_contained(Thing, Place).
can_take(Thing):-
write('There is no '),
write(Thing),
write(' here'),
nl, fail.
put(Thing):-
have(Thing),
retract(have(Thing)),
here(Place),
assert(location(Thing, Place)),
write('All good'), nl,
true.
:- op(35, fx, put).
take_object(Thing):-
retract(location(Thing, _)),
asserta(have(Thing)),
write('taken'), nl,
true.
inventory:-
have(_),
write('You have following objects: '),nl,
list_inventory,
!.
inventory:-
write('You have nothing on ya'),
true,
!.
list_inventory:-
have(X),
tab(2),
write(X),
nl,
fail.
list_inventory:-
true, !.
turn_on:-
turned_off(flashlight),
retract(turned_off(flashlight)),
write('Flashlish was turned on'),
true, !.
turn_on:-
write('Flashlish is already turned on'),
true.
turn_off:-
turned_off(flashlight),
write('Flashlish is already turned off'),
true, !.
turn_off:-
assert(turned_off(flashlight)),
write('Flashlish was turned off'),
true.
is_in(Thing, Place):-
location(Thing, Place).
:- op(35, yfx, is_in).
is_contained(Thing, Place):-
location(Thing, Place).
is_contained(Thing, Place):-
location(X, Place),
is_contained(Thing, X).
puzzle(go(cellar)):-
!,
puzzle(goto(cellar)).
puzzle(go(bedroom)):-
!,
puzzle(goto(bedroom)).
puzzle(goto(cellar)):-
have(flashlight),
not(turned_off(flashlight)),
!.
puzzle(goto(cellar)):-
write("Afraid of the dark"),nl,
!,
fail.
puzzle(goto(bedroom)):-
write("You can't go to bedroom. You're risking to wake up mommy and daddy."),nl,
!,
fail.
puzzle(_):-
true.
:- op(35, yfx, is_contained).