forked from bennaaym/adventure-game-prolog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnowledgeBase.pl
669 lines (489 loc) · 17.8 KB
/
KnowledgeBase.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
:-dynamic currentLocation/1, inventory/1,hasObject/2,charSpeech/2,
bossNarrative/2,finished/1.
/* defines the connection between the different locations */
connected("forest of giants",[location(east,maze),location(south,dungeon)]).
connected(dungeon,[location(north,"forest of giants"),location(south,"final boss room"),location(east,forest)]).
connected(forest,[location(west,dungeon),location(south,temple),location(east,village)]).
connected(temple,[location(north,forest)]).
connected(maze,[location(south,village),location(west,"forest of giants"),location(east,lake)]).
connected(village,[location(north,maze),location(south,"mage place"),location(east,"mountain of despair"),location(west,forest)]).
connected("mage place",[location(north,village),location(east,"castle of drangleic"),location(south,underworld)]).
connected(underworld,[location(north,"mage place")]).
connected(lake,[location(west,maze)]).
connected("mountain of despair",[location(west,village)]).
connected("castle of drangleic",[location(west,"mage place")]).
/* defines the connection between the main locations and sublocations */
hasPlace(temple,["treasure room"]).
hasPlace(village,[house,guild]).
hasPlace(underworld,["reaper room"]).
hasPlace(lake,["water hole"]).
hasPlace("mountain of despair",[hut]).
hasPlace("castle of drangleic",[cave]).
/*defines the connections between the main locations and the characters */
hasChar(forest,"wise man").
hasChar("mage place",mage).
/*defines the connections between the places and objects*/
hasObject("forest of giants",["phoenix egg","key of earth"]).
hasObject(maze,["flower of life"]).
hasObject(house,["egyptian sword"]).
hasObject("treasure room",["egyptian treasure"]).
hasObject(guild,[warriors]).
hasObject(hut,["sword of souls","sun symbol"]).
hasObject("castle of drangleic",[armor,"moon symbol"]).
hasObject("reaper room",["flames of regret"]).
hasObject("water hole",["key of water","sword of ice and fire"]).
hasObject(cave,["key of wind"]).
hasObject(underworld,["key of fire"]).
/* defines requirements to enter a specific location */
require("forest of giants",[warriors]):-!.
require("final boss room",["key of fire","key of water","key of earth","key of wind",armor,"sword of ice and fire"]):-!.
require(temple,["egyptian sword"]):-!.
require(maze,["lantern of truth"]):-!.
require("castle of drangleic",["mage permission"]):-!.
require(underworld,["mage permission","sword of souls"]):-!.
require(lake,["sun symbol","moon symbol","magic stick","flames of regret"]):-!.
require(_,[]).
/* Bosses narrative */
bossNarrative(giant,"with the help of the other warriors, you were able to defeat the giant creature that lives in this forest").
bossNarrative(cerberus,"Cerberus attacked you from no where ... , you were able to kill him using the power of sword of souls").
bossNarrative(aquaDragon,"the monster protecting this place attacked you. the monster protecting this place attacked you. you used the power of the magic stick,moon symbol and sun symbol to evaporate the lake").
bossNarrative("egyptian creature","thanks to the **Egyptian sword** you were able to defeat Egyptian boss that protects this temple.").
writeNarrative(_boss):-
nl,
bossNarrative(_boss,_narrative),
write_ln(_narrative),
retract(bossNarrative(_boss,_narrative)),
assert(bossNarrative(_boss,"")).
/* describe environment */
description(dungeon):-
nl,
write("it's dark a cold fire, there is no one here ").
description("forest of giants"):-
nl,
write("the place is full of giants status and bodies, it's not a place you would survive it alone"),
nl,
writeNarrative(giant).
description(forest):-
nl,
write("it is nice and quiet but be aware of it's not always as it looks like").
description(temple):-
nl,
write("you're inside an Egyptian temple"),nl,
writeNarrative("egyptian creature").
description("treasure room"):-
nl,
write("there is a treasure in the middle of the room").
description(village):-
nl,
write("a casual village with few citizens").
description(maze):-
nl,
write("you can't find your way without the lantern of truth").
description("mage place"):-
write("a quiet place with a lot of magic power around").
description(underworld):-
nl,
write("you can see souls and hear their screams of regret"),
writeNarrative(cerberus).
description(lake):-
nl,
write("you can see a huge lake in front of you"),nl,
writeNarrative(aquaDragon).
description("mountain of despair"):-
nl,
write("welcome to mountain of despair you can see death bodies and skillets be carefull or you end like them").
description("castle of drangleic"):-
nl,
write("a good place to get an armor...").
description(house):-
nl,
write("pretty old house from the midlle ages").
description(guild):-
nl,
write("this is a place where warriors come to train, you might find someone to accompany you").
description(hut):-
nl,
write("such a miserable place, negative energy is everywhere, don't stay here long or ...").
description(cave):-
nl,
write("a pretty cold cave, you can hear the drop of water a powerfull wind is coming from the inside").
description("water hole"):-
write("you can't see much").
description("reaper room"):-
write("flames are every where around ...").
description("final boss room"):-
nl,
write("the boss attacked you but your armor is solid enough to protect you"),
nl,
write("using the sword of ice and fire you killed the final boss"),
nl,
write("a secret door opened you can see the one piece of this world"),
nl,
write("it is the treasure that can give you all what you need"),
nl,
write("power,glory and fortune"),
nl,
retract(finished(no)),
asserta(finished(yes)),
write("***********************end**********************").
/* define characters requirements and gifts */
charRequire("wise man",["egyptian treasure"]).
charRequire(mage,["flower of life","phoenix egg"]).
charGive("wise man",["lantern of truth"]).
charGive(mage,["mage permission","magic stick"]).
charSpeech("wise man","Hello warrior, you're at the beginning of a long journey. I have a mission for you, bring me the egyptian treasure inside the old temple and I'll give you the **lantern of truth**.you will need it for the rest of the journey").
charSpeech(mage,"we can talk after you bring me the ** phoenix egg , flower of life **.try to check the maze and the forest of giants if you didn't yet").
/* defines interactions with game characters */
talkTo(_char):-
currentLocation(_current),
hasChar(_current,_char),
charSpeech(_char,_speech),
write_ln(_speech).
talkTo(_):-
write_ln("who are you talking too !!!").
give(_char,_objects):-
currentLocation(_current),
hasChar(_current,_char),
charRequire(_char,_requirement),
subset(_objects,_requirement),
inventory(_collected),
subset(_objects,_collected),
subtract(_collected,_objects,_newInventory),
charGive(_char,_gives),
append(_newInventory,_gives,_newInventory2),
retract(inventory(_collected)),
asserta(inventory(_newInventory2)),
write("Great job warrior, I give you"),
write_ln(_gives),
write_ln("check your inventory"),
charSpeech(_char,_speech),
retract(charSpeech(_char,_speech)),
asserta(charSpeech(_char,"sorry warrior, I can't offer you more objects")),
!.
give(_,_):-
write_ln("warrior!! what are you trying to do").
/* define movement */
/*initial position when the game start*/
currentLocation(dungeon).
/* entering and exiting closed locations */
enter(_place):-
currentLocation(_current),
hasPlace(_current,_places),
member(_place,_places),
retract(currentLocation(_current)),
asserta(currentLocation(_place)),
look,!.
enter(_):-
currentLocation(_current),
write("the is no such place in the "),
write_ln(_current).
exit:-
currentLocation(_place),
hasPlace(_current,_places),
member(_place,_places),
retract(currentLocation(_place)),
assert(currentLocation(_current)),
write("you have exit from the "),
write_ln(_place).
/* moving from a main location to another */
north:- move(north).
south:-move(south).
east:-move(east).
west:- move(west).
move(_direction):-
currentLocation(_current),
connected(_current,_locations),
member(location(_direction,_destination),_locations),
haveRequirement(_destination),
retract(currentLocation(_current)),
assert(currentLocation(_destination)),
look,
!.
move(_):-
write_ln("closed path !").
/* defines contraints to access some locations */
haveRequirement(_destination):-
inventory(_collected),
require(_destination,_requirements),
subset(_requirements,_collected),!.
haveRequirement(_destination):-
require(_destination,_requirements),
write_ln("warrior, you aren't ready for this yet ..."),
write_ln("you need :"),
listRequirements(_requirements),
fail.
/* explore envirement */
look:-
currentLocation(_current),
write("you're in the "),write(_current),
description(_current),
nl,
connected(_current,_locations),
getDirections(_locations),
nl,
listPlaces(_current),
listChars(_current),
listObjects(_current),
!.
/* in case of a closed location */
look:-
currentLocation(_current),
listPlaces(_current),
listChars(_current),
listObjects(_current),
!.
getDirection(location(_direction,_)):-
write("there is a path to :"),
write(_direction),
nl,!.
getDirections([]).
getDirections([_location|_rest]):-
getDirection(_location),
getDirections(_rest).
listChars(_current):-
hasChar(_current,_char),
write("you can see :"),
write_ln(_char),
fail.
listChars(_).
listPlaces(_current):-
hasPlace(_current,_places),
listPlaces(_places),
!.
listPlaces([]).
listPlaces([_place|_rest]):-
write("you can see :"),
write_ln(_place),
listPlaces(_rest).
listPlaces(_).
listObjects(_current):-
hasObject(_current,_objects),
listObjects(_objects),
!.
listObjects([]).
listObjects([_object|_rest]):-
write("you can see :"),
write_ln(_object),
listObjects(_rest).
listObjects(_).
listRequirements([]).
listRequirements([H|Tail]):-
tab(2),
write("* "),
write_ln(H),
listRequirements(Tail).
/* interact with other objects */
inventory([]).
inventory:-
inventory([]),
write_ln("your inventory is empty try to collect some objects first :) "),
!.
inventory:-
inventory(_collected),
listInventory(_collected).
listInventory([]):-!.
listInventory([_object1|Tail]):-
tab(2),
write("- "),
write_ln(_object1),
listInventory(Tail).
pick(_object):-
currentLocation(_current),
hasObject(_current,_objects),
member(_object,_objects),
delete(_objects,_object,_rest),
retract(hasObject(_current,_objects)),
asserta(hasObject(_current,_rest)),
inventory(_collected),
append([_object],_collected,_newInventory),
retract(inventory(_collected)),
assert(inventory(_newInventory)).
pick(_):-
write_ln("what are you trying to pick !!!").
pickAll:-
currentLocation(_current),
hasObject(_current,_objects),
retract(hasObject(_current,_objects)),
inventory(_collected),
append(_objects,_collected,_newInventory),
retract(inventory(_collected)),
asserta(inventory(_newInventory)),
!.
pickAll:-
write_ln("there is nothing pickable here").
drop(_object):-
currentLocation(_current),
inventory(_collected),
member(_object,_collected),
delete(_collected,_object,_newInventory),
hasObject(_current,_objects),
append([_object],_objects,_newObjects),
retract(hasObject(_current,_objects)),
asserta(hasObject(_current,_newObjects)),
retract(inventory(_collected)),
asserta(inventory(_newInventory)),
!.
drop(_object):-
currentLocation(_current),
inventory(_collected),
member(_object,_collected),
delete(_collected,_object,_newInventory),
asserta(hasObject(_current,[_object])),
retract(inventory(_collected)),
asserta(inventory(_newInventory)),
!.
drop(_):-
write_ln("what are you trying to drop !!!").
/* game entry */
/* game entry point */
start:-
asserta(finished(no)),
nl,
write("Hello warrior, welcome to the dungeon,"),nl,
write("you came here looking after power, glory, and fortune."),nl,
write("You're standing in front of the dungeon guardian"),nl,
write("he will ask you a question,"),nl,
write("you have to solve the next enigma in order to start your journey."),nl,
write("if you respond wrongly you'll be killed by the guardian"),nl,
write("do you want to accept ? (yes/no)"),nl,
readln(_decision),
delete(_decision,.,_decision2),
acceptFirstExam(_decision2),
look,
gameLoop,
!.
start.
gameLoop:-
repeat,
process_sentence(_c),
execute_command(_c),
(_c==quit; end).
execute_command(instructions):-instructions,!.
execute_command(look):-look,!.
execute_command(inventory):-inventory,!.
execute_command(north):-north,!.
execute_command(east):-east,!.
execute_command(south):-south,!.
execute_command(west):-west,!.
execute_command(move(_direction)):-move(_direction),!.
execute_command(enter(_place)):-enter(_place),!.
execute_command(exit):-exit,!.
execute_command(pick(_object)):- pick(_object),!.
execute_command(pickAll):-pickAll,!.
execute_command(drop(_object)):-drop(_object),!.
execute_command(talkTo(_char)):-talkTo(_char),!.
execute_command(give((_char,_objects))):-give(_char,_objects),!.
execute_command(quit):-quit,!.
execute_command(_):-
write_ln("warrior, what are you trying to say !!!"),!.
quit:-
write_ln("you quit the game").
end:-
finished(yes),!.
answersPool([himself,"himself","human himself","human","itself"]).
acceptFirstExam([yes]):-
nl,
write_ln("you're a courageous warrior, this is you're enigma :"),
write_ln("who is the human greatest enemy ?"),
readln(_answer),
delete(_answer,.,_answer2),
firstExamAnswer(_answer2),
!.
acceptFirstExam(_):-
nl,
write_ln("you're not qualified to get this journey .come back when you get strong"),
nl,
retract(finished(no)),
asserta(finished(yes)),
fail.
firstExamAnswer([H|_]):-
answersPool(_possibleAnswers),
member(H,_possibleAnswers),
nl,
write_ln("Gongrats you've passed your first test"),
write_ln("you can start your journey now good luck"),
write_ln(", you will need it..."),nl,
!.
firstExamAnswer(_):-
nl,
write_ln("you're not qualified to get this journey . I'll take your soul now . shine!!"),
retract(finished(no)),
asserta(finished(yes)),
fail.
/* NLP */
process_sentence(_c):-
nl,
readln(_s),
delete(_s,.,_s2),
sentence(_parsedList,_s2,[]),
_c=.._parsedList,
!.
process_sentence(_):-
write_ln("warrior, are you trying to say something ?"),fail.
sentence([_pred,_arg])-->verb(_pred,_type),nounphrase(_type,_arg).
sentence([_pred])-->pred(_pred).
verb(move,player_move)-->[go];[move].
verb(talkTo ,talk_to_char)-->[talk, to];[ask].
verb(give,give_char)-->[give].
verb(enter,enter_place)-->[enter];[go,into].
verb(pick,pick_object)-->[pick].
verb(drop,drop_object)-->[drop].
nounphrase(_type,_noun)-->determiner,noun(_type,_noun).
nounphrase(_type,_noun)-->noun(_type,_noun).
determiner-->[the].
determiner-->[a].
noun(player_move,_arg)-->pred(_arg).
noun(talk_to_char,_arg)-->[_arg].
noun(talk_to_char,"wise man")-->[wise,man].
noun(give_char,(mage,["phoenix egg","flower of life"]))-->
[mage,phoenix,egg,and,flower,of,life];[mage,flower,of,life,and,phoenix,egg].
noun(give_char,("wise man",["egyptian treasure"]))-->[wise,man,egyptian,treasure].
noun(enter_place,_args)-->[_args].
noun(enter_place,"treasure room")-->[treasure,room].
noun(enter_place,"reaper room")-->[reaper,room].
noun(enter_place,"water hole")-->[water,hole].
noun(pick_object,"phoenix egg")-->[phoenix,egg].
noun(pick_object,"egyptian treasure")-->[egyptian,treasure].
noun(pick_object,"flower of life")-->[flower,of,life].
noun(pick_object,"egyptian sword")-->[egyptian,sword].
noun(pick_object,"sword of souls")-->[sword,of,souls].
noun(pick_object,warriors)-->[warriors].
noun(pick_object,armor)-->[armor].
noun(pick_object,"sun symbol")-->[sun,symbol].
noun(pick_object,"moon symbol")-->[moon,symbol].
noun(pick_object,"key of earth")-->[key,of,earth].
noun(pick_object,"key of fire")-->[key,of,fire].
noun(pick_object,"key of wind")-->[key,of,wind].
noun(pick_object,"key of water")-->[key,of,water].
noun(pick_object,"sword of ice and fire")-->[sword,of,ice,and,fire].
noun(pick_object,"flames of regret")-->[flames,of,regret].
noun(drop_object,"phoenix egg")-->[phoenix,egg].
noun(drop_object,"egyptian treasure")-->[egyptian,treasure].
noun(drop_object,"flower of life")-->[flower,of,life].
noun(drop_object,"egyptian sword")-->[egyptian,sword].
noun(drop_object,"sword of souls")-->[sword,of,souls].
noun(drop_object,warriors)-->[warriors].
noun(drop_object,armor)-->[armor].
noun(drop_object,"sun symbol")-->[sun,symbol].
noun(drop_object,"moon symbol")-->[moon,symbol].
noun(drop_object,"key of earth")-->[key,of,earth].
noun(drop_object,"key of fire")-->[key,of,fire].
noun(drop_object,"key of wind")-->[key,of,wind].
noun(drop_object,"key of water")-->[key,of,water].
noun(drop_object,"sword of ice and fire")-->[sword,of,ice,and,fire].
noun(drop_object,"flames of regret")-->[flames,of,regret].
noun(drop_object,something)-->[_|_].
pred(look)-->[look].
pred(look)-->[look,around].
pred(look)-->[observe].
pred(exit)-->[exit].
pred(exit)-->[go,out].
pred(inventory)-->[inventory].
pred(inventory)-->[check,inventory].
pred(north)-->[north].
pred(east)-->[east].
pred(south)-->[south].
pred(west)-->[west].
pred(pickAll)-->[pick,all].
pred(quit)-->[quit].
pred(himself)-->[himself].