-
Notifications
You must be signed in to change notification settings - Fork 6
/
wumpus.pl
1762 lines (1489 loc) · 59.7 KB
/
wumpus.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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Hunt The Wumpus - World Simulator %
% Copyright (C) 2012 - 2022 Ruben Carlo Benante <rcb at beco dot cc> %
% %
% This program is free software; you can redistribute it and/or modify %
% it under the terms of the GNU General Public License as published by %
% the Free Software Foundation; version 2 of the License. %
% %
% This program is distributed in the hope that it will be useful, %
% but WITHOUT ANY WARRANTY; without even the implied warranty of %
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the %
% GNU General Public License for more details. %
% %
% You should have received a copy of the GNU General Public License along %
% with this program; if not, write to the Free Software Foundation, Inc., %
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Hunt The Wumpus - World Simulator - Version 5.0, by Dr. Beco
%
% Edited, Compiled, Modified by:
% Author:
% - Ruben Carlo Benante (rcb@beco.cc)
% Copyright: 2012 - 2022
% License: GNU GPL Version 2.0
%
% Based on:
% - Original by Gregory Yob (1972)
% - Larry Holder (accessed version Oct/2005)
% - Walter Nauber (09/02/2001)
% - An Anonymous version of Hunt The Wumpus with menus (aeric? 2012?)
%
% Special thanks to:
% - Larry Holder (holder@cse.uta.edu) (version 1.0 and version 2.3)
% - Walter Nauber (walter.nauber@tu-dresden.de) (swi-prolog version)
%
% A Prolog implementation of the Wumpus world described in Russell and
% Norvig's "Artificial Intelligence: A Modern Approach", Section 6.2.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Agent actions:
%
% 1. goforward, % moves the agent forward
% 2. turnright, % turns to right / clockwise
% 3. turnleft, % turns to left / anti-clockwise
% 4. grab, % grabs something (gold, arrow, ...)
% 5. shoot, % shoots an arrow
% 6. sit, % sit and do nothing
% 7. climb, % climbs out the cave from square [1,1]
% 8. gps, % reads G=[[X,Y], O], agent position and orientation
%
% World Setup:
%
% world_setup([Size, Type, Move, Gold, Pit, Bat, [RandS, RandA]]).
% 1. Size: 2..20, with some constrictions: [2-9] grid; 20, dodeca; 4, fig62
% 2. Type: fig62, grid or dodeca
% 3. Move: walker, runner, wanderer, spinner, hoarder, spelunker, stander, trapper and bulldozer
% 4. Gold: Integer is deterministic number, float from 0.0<G<1.0 is probabilistic
% 5. Pits: Idem, 0 is no pits.
% 6. Bats: Idem, 0 is no bats.
% Optional, advanced setup:
% 7. RandS: yes/no, random agent start position
% 8. RandA: yes/no, random agent start angle (not implemented)
%
% fig62:
% gold: [2,3]
% pits: [3,1], [3,3], [3,4]
% wumpus: [1,3], fixed location (stander)
%
% grid:
% size: from 2 to 9; 0 = random size
% gold: free to choose from 1 to NxN-1, not in [1,1]
% pits: any number, random location, not in [1,1], [2,1] and [1,2]
% wumpus: only one, random location, not in [1,1]
%
% dodeca:
% size: 20
% gold: random quantity not in [1,1]
% pits: random quantity not in [1,1], [2,1] and [1,2]
% wumpus: only one, not in [1,1]
%
% External:
% init_agent.
% run_agent(Perception, Action).
% world_setup([Size, Type, Move, Golds, Pits, Bats, [RandS, RandA]]).
% Types of Wumpus Movement
% walker : original: moves when it hears a shoot, or you enter its cave
% runner : go forward and turn left or right on bumps, maybe on pits
% wanderer : arbitrarily choses an action from [sit,turnleft,turnright,goforward]
% spinner : goforward, turnright, repeat.
% hoarder : go to one of the golds and sit
% spelunker : go to a pit and sit
% stander : do not move (default)
% trapper : goes hunting agent as soon as it leaves [1,1]; goes home otherwise
% bulldozer : hunt the agent as soon as it smells him
%
% Actions: - 4 per square
% Tries: - Number of trials
%
% Gold: - Gold probability per square. When fig62 or pit3, only one gold. (default 0.1)
% Hazard: Pit: - Pit probability per square. When fig62 or pit3, only 3 pits. (default 0.2)
% Hazard: Bat: - Bat probability per square. When fig62, no bats. (default 0.1)
%
% Hazards and gold:
% - An integer number sets total.
% - A float number from 0.0 < P < 1.0 sets the probability.
%
% Score:
% * +500 points for each gold AFTER climbing alive
% * +1000 points for killing the Wumpus
% * -1000 for dying (1. eaten alive by wumpus, 2. falling into a pit, 3. walking until exhausted)
% * -1 for action (sit, turnright, turnleft, goforward, grab, shoot, climb, gps)
%
% Protect all predicates (make private), except the ones listed bellow:
:- module(wumpus, % module name WUMPUS
[start/0, % start an automatic agent
manual_setup/1, % manually configure the world
manual_init/0, % manually initialize the world
go/0, % shortcut for goforward
goforward/0, % manually moves the agent forward
turnright/0, % manually turns to right / clockwise
turn/0, % shortcut for turnright
turnr/0, % shortcut for turnright
tr/0, % shortcut for turnright
turnleft/0, % manually turns to left / anti-clockwise
turnl/0, % shortcut for turnleft
tl/0, % shortcut for turnleft
grab/0, % manually grabs something (gold, arrow, ...)
gr/0, % shortcut for grab
shoot/0, % manually shoots an arrow
sh/0, % shortcut for shoot
sit/0, % sit and do nothing
si/0, % shortcut for sit
climb/0, % manually climbs out the cave
cl/0, % shortcut for climb
gps/0, % manually reads the GPS
gp/0]). % shortgut for gps
:- dynamic([
get_setup/1, % get world setup (from world_setup or from default setup)
world_extent/1, % ww World extent size
wumpus_location/2, % ww Wumpus location: (X,Y) on grid; (CaveNumber,Level) on dodeca;
wumpus_health/1, % ww Wumpus health: alive/dead
wumpus_orientation/1, % ww Wumpus orientation: 0, 90, 180, 270
wumpus_last_action/1, % ww Wumpus last action
wumpus_move_rule/1, % ww Wumpus movement rule: walker, runner, wanderer, spinner, hoarder, spelunker, stander, trapper, bulldozer
gold/2, % ww Gold positions
pit/2, % ww Pit positions
bat/2, % ww Bat positions
gold_probability/1, % ww Probability that a location has gold (default 0.10)
pit_probability/1, % ww Probability that a non-(1,1) location has a pit (default 0.20)
bat_probability/1, % ww Probability that a non-(1,1) location has a bat (default 0.10)
max_agent_actions/1, % ww Maximum actions per trial allowed by agent (default 4*squardes)
agent_location/2, % Agent location. (X,Y) on grid; (CaveNumber, Level) on dodeca;
agent_old_location/2, % Agent previous location. (X,Y) on grid; (CaveNumber, Level) on dodeca;
agent_orientation/1, % Agent orientation: 0/East/Right, 90/North/Up, 180/West/Left, 270/South/Down
agent_in_cave/1, % Agent is inside cave: yes/no
agent_health/1, % Agent health: alive/dead
agent_gold/1, % Number of golds that the agent has (start with 0)
agent_arrows/1, % Number of arrows that the agent has (start with 1)
agent_score/1, % Agent Game Score
agent_num_actions/1, % Number of the current agent action
ww_initial_state/1 % list of ww configuration values
]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Run by hand
%
%L=[Size, Type, Move, Gold, Pit, Bat, [RandS, RandA]]
%L=[4, grid, stander, 0.1, 0.2, 0.1, [no, no]] % default
manual_setup(L0) :-
check_setup(L0, L1),
retractall(get_setup(_)),
assert(get_setup(L1)),
!.
manual_init :-
initialize(P), % also sets get_setup to default in case of no manual_setup
get_setup(L),
format("~nWorld Setup: ~w~n", [L]),
format("First Impression: ~w~n", [P]),
display_world,
!.
% Manual actions to play directly at the swipl PROLOG prompt
go :- goforward.
goforward :- manual_execute(goforward).
turn :- turnright.
turnr :- turnright.
tr :- turnright.
turnright :- manual_execute(turnright).
turnl :- turnleft.
tl :- turnleft.
turnleft :- manual_execute(turnleft).
gr :- grab.
grab :- manual_execute(grab).
sh :- shoot.
shoot :- manual_execute(shoot).
cl :- climb.
climb :- manual_execute(climb).
si :- sit.
sit :- manual_execute(sit).
gp :- gps.
gps :- manual_execute(gps).
manual_execute(A) :-
agent_num_actions(N), % current action
execute(A, P),
format("~nAction #~w: ~w~n", [N, A]),
display_world,
format("Perception: ~w~n", [P]),
retractall(agent_num_actions(_)),
N1 is N + 1,
assert(agent_num_actions(N1)),
agent_score(S),
format("Score: ~d~n", S),
!.
% evaluate_agent(Trials,Score,Time): Performs Trials trials, where each
% trial involves generating a random wumpus world, initializing the
% agent, running the agent until it dies or leaves the cave, and then
% recording the score and time spent running the agent. The total
% score and time are returned in Score and Time (millisecs).
%
% This procedure requires the external definition of 2 procedures:
%
% init_agent: Called after new world is initialized. Should perform
% any needed agent initialization.
%
% run_agent(Percept,Action): Given the current Percept, this procedure
% should return an appropriate Action, which is then
% executed.
start :-
evaluate_agent(1,S), % only one trial
format("Score: ~d~n", S).
evaluate_agent(Trials,Score) :-
run_agent_trials(Trials,1,Score).
% run_agent_trials(Trials,NextTrial,Score,Time): Runs trials from NextTrial
% to Trial and returns the total Score and Time (millisecs) spent inside
% calls to init_agent and run_agent.
run_agent_trials(Trials,NextTrial,0) :-
NextTrial > Trials,
!.
run_agent_trials(Trials,NextTrial,Score) :-
NextTrial =< Trials,
format("Trial ~d~n",[NextTrial]),
initialize(Percept), % world and agent
format("External init_agent...~n"),
init_agent, % needs to be defined externally
display_world,
!,
run_agent_action(Percept),
agent_score(Score1),
NextTrial1 is NextTrial + 1,
run_agent_trials(Trials,NextTrial1,Score2),
Score is Score1 + Score2.
% run_agent_action(NumActions,Percept,Time): Continues to ask for and
% execute actions from run_agent(Percept,Action) until either the
% agent dies, leaves the cave or executes the maximum M actions as
% defined by max_agent_actions(M). In any case, the total time
% spent during calls to run_agent is returned in Time (millisecs).
run_agent_action(_) :-
( agent_health(dead) ; % trial over when agent dies or
agent_in_cave(no) ), % leaves cave
!.
run_agent_action(_) :- % agent allowed only N actions as
max_agent_actions(N), % defined by max_agent_actions(N)
agent_num_actions(NumActions), % current action
NumActions > N,
!.
run_agent_action(Percept) :-
run_agent(Percept,Action), % needs to be defined externally
check_agent_action(Action), % check for goforward, turnright, turnleft, shoot, grab, sit, climb or gps.
agent_num_actions(NumActions), % current action
format("~nExternal action #~w: run_agent(~w,~w)~n", [NumActions, Percept, Action]),
execute(Action,Percept1),
display_world,
retractall(agent_num_actions(_)),
NumActions1 is NumActions + 1,
assert(agent_num_actions(NumActions1)),
!,
run_agent_action(Percept1).
run_agent_action(Percept) :-
format("External function run_agent(~w, Nop) failed miserably!~n", [Percept]),
!, fail.
% initialize(Percept): initializes the Wumpus world and our fearless
% agent and returns the Percept from square 1,1.
% Percept = [Stench,Breeze,Glitter,Bump,Scream,Rustle,GPS]
initialize([Stench, Breeze, Glitter, Bump, no, Rustle, GPS]) :-
initialize_world,
initialize_agent,
stench(Stench), % #1 stench(Stench) Wumpus may be nearby (no Wumpus on [1,1])
breeze(Breeze), % #2 no pit on [1,1] and grid: [1,2],[2,1] or dodeca: [2,2],[5,2],[8,2]
glitter(Glitter), % #3 no gold on [1,1]
will_bump(Bump), % #4 check if agent is facing a wall at start
rustle(Rustle), % #6 no bat on [1,1], and grid: [1,2],[2,1] or dodeca: [2,2],[5,2],[8,2]
get_gps(GPS). % #7 get GPS coordinates [[Xpos, Ypos], Angle]
% initialize_world: gather information
initialize_world :-
ww_retract_all, % retract ww list (wumpus, gold, pit and bat, and all initial state variables)
assert_setup, % assert user or default setup
get_setup(L), % L=[Size, Type, Move, Gold, Pit, Bat, Adv], Adv=[RandS, RandA]
L=[Size, Type, Move, Gold, Pit, Bat|_],
ww_addto_init_state(world_extent(Size)),
random(0, 4, WAngN),
WAng is WAngN * 90,
ww_addto_init_state(wumpus_orientation(WAng)), % Random Wumpus start angle
ww_addto_init_state(wumpus_health(alive)), % Wumpus is alive and well (and hungry)
ww_addto_init_state(wumpus_last_action(sit)), % Wumpus last move to use as basic AI memory
ww_addto_init_state(gold_probability(Gold)), % Probability that a location has gold
ww_addto_init_state(pit_probability(Pit)), % Probability that a non-(1,1) location has a pit
ww_addto_init_state(bat_probability(Bat)), % Probability that a location has bats
set_max_agent_actions(Size, Type, Actions), % 4 actions per square average (fig62 is 2.875 moves per square)
ww_addto_init_state(max_agent_actions(Actions)), % Maximum actions per trial allowed by agent
ww_addto_init_state(wumpus_move_rule(Move)), % Wumpus move style
initialize_world_type(L).
% initialize_world_type(World): Initializes the Wumpus world
% World = [Size, Type, Move, Gold, Pit, Bat, [Ax, Ay]]
%
% initialize_world_type(World): Initializes the Wumpus world in Figure 6.2 of Russell & Norvig
initialize_world_type([_,fig62,_,_,_,_|_]) :-
ww_addto_init_state(wumpus_location(1,3)), % wumpus location
ww_addto_init_state(gold(2,3)), % gold position
ww_addto_init_state(pit(3,1)), % pit 1
ww_addto_init_state(pit(3,3)), % pit 2
ww_addto_init_state(pit(4,4)), % pit 3
ww_assert_all.
initialize_world_type([E, Type, _, PG, PP, PB|_]) :-
gold_squares(E, Type, GS),
hazard_squares(E, Type, HS),
ww_place_it(gold, PG, GS), % place gold (not [1,1])
ww_place_it(pit, PP, HS), % AllSqrs3
ww_place_it(bat, PB, HS), % place some bats not near the entrance
ww_place_it(wumpus_location, 1, GS), % exactly one wumpus, initialize it not in [1,1]
ww_assert_all.
% initialize_agent: agent is initially alive, destitute (except for one
% arrow), in grid 1,1 and facing to the right (0 degrees).
initialize_agent :-
retractall(agent_orientation(_)),
retractall(agent_in_cave(_)),
retractall(agent_health(_)),
retractall(agent_gold(_)),
retractall(agent_arrows(_)),
retractall(agent_score(_)),
retractall(agent_num_actions(_)),
assert(agent_orientation(0)),
assert(agent_in_cave(yes)),
assert(agent_health(alive)),
assert(agent_gold(0)),
assert(agent_arrows(1)),
assert(agent_score(0)),
assert(agent_num_actions(1)),
initialize_agent_advanced.
initialize_agent_advanced :-
get_setup(L), % [Size, Type, Move, Gold, Pit, Bat, Adv], Adv=[RandS, RandA]
L=[Size, Type, _Move, _Gold, _Pit, _Bat, Adv], % [Ax, Ay]],
initialize_agent_location(Size, Type, Adv),
initialize_agent_orientation(Adv).
initialize_agent_location(S, T, [yes|_]) :- % RandS = yes, random agent start location
all_squares_type(T, S, All),
findall([Xb, Yb], bat(Xb, Yb), AllBats), % find all bats
findall([Xp, Yp], pit(Xp, Yp), AllPits), % find all pits
findall([Xg, Yg], gold(Xg, Yg), AllGolds), % find all golds
wumpus_location(Wx, Wy), % find Wumpus
subtract(All, AllBats, AllButBats), % All but bats
subtract(AllButBats, AllPits, AllBBPits), % All but bats and pits
subtract(AllBBPits, AllGolds, AllBBPGolds), % All but bats, pits and golds
delete(AllBBPGolds, [Wx, Wy], AllButs), % All but bats, pits, golds and Wumpus
random_member([Ax, Ay], AllButs), % Pick one
retractall(agent_location(_,_)),
assert(agent_location(Ax, Ay)),
retractall(agent_old_location(_,_)),
assert(agent_old_location(Ax, Ay)), % In the begining, they are the same (previous and current)
!.
initialize_agent_location(_, _, _) :- % RandS = no, agent at [1,1]
retractall(agent_location(_,_)),
assert(agent_location(1, 1)),
retractall(agent_old_location(_,_)),
assert(agent_old_location(1, 1)). % In the begining, they are the same (previous and current)
initialize_agent_orientation([_, yes|_]) :-
retractall(agent_orientation(_)),
random(0, 4, Ai),
A is Ai * 90,
assert(agent_orientation(A)).
initialize_agent_orientation(_).
gold_squares(E, T, GS) :- % not used by fig62
all_squares_type(T, E, All),
delete(All, [1,1], GS). % all squares but [1,1]
hazard_squares(E, grid, HS) :- % not used by fig62
gold_squares(E, grid, GS),
subtract(GS, [[1,2],[2,1]], HS). % all squares but [1,1],[2,1],[1,2]
hazard_squares(E, dodeca, HS) :-
gold_squares(E, dodeca, GS), % all squares but [1,1]
subtract(GS, [[2,2],[5,2],[8,2]], HS). % all squares but [2,2],[5,2],[8,2]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% dodeca_map : mapping dodecahedron tunnels.
% For each Cave from 1 to 20, there are exactly 3 tunnels Cn =\= 0
% [Cave1, [ C0, C1, C2, C3]], ..., [Cave20, [C0, C1, C2, C3]]
%
% C0 = East / right / 0
% C1 = North / up / 90
% C2 = West / left / 180
% C3 = South / down / 270
dodeca_map([
[1, [2, 8, 5, 0]], % Cave 1: east 2, north 8, west 5, south none
[2, [0, 3, 10, 1]], % Cave 2: east none, north 3, west 10, south 1
[3, [2, 0, 4, 12]], % Cave 3: east 2, north none, west 4, south 12
[4, [3, 0, 5, 14]], % Cave 4: east 3, north none, west 5, south 14
[5, [6, 4, 0, 1]], % Cave 5: east 6, north 4, west none, south 1
[6, [0, 15, 5, 7]], % Cave 6: east none, north 15, west 5, south 7
[7, [0, 17, 6, 8]], % Cave 7: east none, north 17, west 6, south 8
[8, [9, 0, 7, 1]], % Cave 8: east 9, north none, west 7, south 1
[9, [10, 18, 0, 8]], % Cave 9: east 10, north 18, west none, south 8
[10, [2, 11, 0, 9]], % Cave 10: east 2, north 11, west none, south 9
[11, [0, 12, 19, 10]], % Cave 11: east none, north 12, west 19, south 10
[12, [0, 3, 13, 11]], % Cave 12: east none, north 3, west 13, south 11
[13, [12, 0, 14, 20]], % Cave 13: east 12, north none, west 14, south 20
[14, [13, 4, 0, 15]], % Cave 14: east 13, north 4, west none, south 15
[15, [16, 14, 0, 6]], % Cave 15: east 16, north 14, west none, south 6
[16, [0, 20, 15, 17]], % Cave 16: east none, north 20, west 15, south 17
[17, [18, 16, 0, 7]], % Cave 17: east 18, north 16, west none, south 7
[18, [0, 19, 17, 9]], % Cave 18: east none, north 19, west 17, south 9
[19, [11, 20, 0, 18]], % Cave 19: east 11, north 20, west none, south 18
[20, [19, 13, 16, 0]] % Cave 20: east 19, north 13, west 16, south none
]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% execute(Action,Percept): executes Action and returns Percept
%
% Action is one of:
% 1. goforward: move one square along current orientation if possible
% 2. turnleft: turn left +90 degrees
% 3. turnright: turn right -90 degrees
% 4. grab: pickup gold if in square
% 5. shoot: shoot an arrow along orientation, killing wumpus if
% in that direction
% 6. climb: if in square 1,1, leaves the cave and adds 500 points
% for each piece of gold
% 7. sit: do nothing, costs one action and -1 score
% 8. gps: reads G=[[X,Y], O], agent position and orientation
%
% Percept = [Stench,Breeze,Glitter,Bump,Scream,Rustle]
% Percept = [Fetor,Ventilation,Luster,Tumble,Bellow,Rustle,GPS]
% Percept = [F,V,L,T,B,R,G]
% each having a value of either 'yes' or 'no', except
% GPS = [[Xpos,Ypos],Orient] or GPS=[]
execute(_,[no,no,no,no,no,no,[]]) :-
agent_health(dead), !, % agent must be alive to execute actions
format("You are dead!~n",[]).
execute(_,[no,no,no,no,no,no,[]]) :-
agent_in_cave(no), !, % agent must be in the cave
format("You have left the cave.~n",[]).
execute(goforward,[Stench,Breeze,Glitter,Bump,no,Rustle,[]]) :-
decrement_score,
goforward(Bump), % update location and check for bump
move_wumpus(goforward), % move wumpus according to the rule set, before bats grab him
elsewhereville(Rustle), % check for bats nearby or in the current square
update_agent_health, % check for wumpus, pit or max actions
stench(Stench), % update rest of percept
breeze(Breeze),
glitter(Glitter),
update_agent_orientation(Bump). %dodeca agent is with his back the the door he came through
execute(turnleft,[Stench,Breeze,Glitter,no,no,Rustle,[]]) :-
decrement_score,
agent_orientation(Angle),
NewAngle is (Angle + 90) mod 360,
retract(agent_orientation(Angle)),
assert(agent_orientation(NewAngle)),
move_wumpus(turnleft), % move wumpus according to the rule set
update_agent_health, % check for wumpus, pit or max actions
stench(Stench),
breeze(Breeze),
glitter(Glitter),
rustle(Rustle).
execute(turnright,[Stench,Breeze,Glitter,no,no,Rustle,[]]) :-
decrement_score,
agent_orientation(Angle),
NewAngle is (Angle + 270) mod 360,
retract(agent_orientation(Angle)),
assert(agent_orientation(NewAngle)),
move_wumpus(turnright), % move wumpus according to the rule set
update_agent_health, % check for wumpus, pit or max actions
stench(Stench),
breeze(Breeze),
glitter(Glitter),
rustle(Rustle).
execute(grab,[Stench,Breeze,no,no,no,Rustle,[]]) :-
decrement_score,
get_the_gold,
move_wumpus(grab), % move wumpus according to the rule set
update_agent_health, % check for wumpus, pit or max actions
stench(Stench),
breeze(Breeze),
rustle(Rustle).
execute(shoot,[Stench,Breeze,Glitter,no,Scream,Rustle,[]]) :-
decrement_score,
move_wumpus(shoot), % move wumpus according to the rule set
shoot_arrow(Scream), % shoot after wumpus move, as it may dodge the arrow
update_agent_health, % check for wumpus, pit or max actions
stench(Stench),
breeze(Breeze),
glitter(Glitter),
rustle(Rustle).
execute(climb,[no,no,no,no,no,no,[]]) :-
agent_location(1,1), !,
decrement_score,
agent_gold(G),
retract(agent_score(S)),
S1 is (S + (500 * G)),
assert(agent_score(S1)),
retract(agent_in_cave(yes)),
assert(agent_in_cave(no)),
format("I am outta here.~n",[]).
execute(climb,[Stench,Breeze,Glitter,no,no,Rustle,[]]) :-
decrement_score,
format("You cannot leave the cave from here.~n",[]),
move_wumpus(climb), % move wumpus according to the rule set
update_agent_health, % check for wumpus, pit or max actions
stench(Stench),
breeze(Breeze),
glitter(Glitter),
rustle(Rustle).
execute(sit,[Stench,Breeze,Glitter,no,no,Rustle,[]]) :-
decrement_score,
move_wumpus(sit), % move wumpus according to the rule set
update_agent_health, % check for wumpus, pit or max actions
stench(Stench),
breeze(Breeze),
glitter(Glitter),
rustle(Rustle).
execute(gps,[Stench,Breeze,Glitter,no,no,Rustle,GPS]) :-
decrement_score,
move_wumpus(gps), % move wumpus according to the rule set
update_agent_health, % check for wumpus, pit or max actions
stench(Stench),
breeze(Breeze),
glitter(Glitter),
rustle(Rustle),
get_gps(GPS).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Perceptions [Stench,Breeze,Glitter,Bump,Scream,GPS]
% stench(Stench): Stench = yes if wumpus (dead or alive) is in a square
% directly up, down, left, or right of the current agent location.
stench(yes) :-
agent_location(X,Y),
has_hazard_perception(X, Y, wumpus_location),
!.
stench(no).
% breeze(Breeze): Breeze = yes if a pit is in a square directly up, down,
% left, or right of the current agent location.
breeze(yes) :-
agent_location(X,Y),
has_hazard_perception(X, Y, pit),
!.
breeze(no).
% glitter(Glitter): Glitter = yes if there is gold in the current agent
% location.
glitter(yes) :-
agent_location(X,Y),
gold(X,Y),
!.
glitter(no).
% gps(GPS): GPS=[[Xpos,Ypos],Orient], give to agent the coordinates
get_gps([[X, Y], A]) :-
allow_gps(yes), % check if GPS consults are allowed
agent_location(X, Y),
agent_orientation(A),
!.
get_gps([]).
allow_gps(yes). % DEBUG: change this setting to have options to block gps. TODO
% goforward(Bump): Attempts to move agent forward one unit along
% its current orientation.
goforward(no) :-
agent_orientation(Angle),
agent_location(X,Y),
new_location(X,Y,Angle,X1,Y1), % fail if bump
!,
retractall(agent_old_location(_,_)), % keep track where agent was before
assert(agent_old_location(X,Y)), % update old location
retractall(agent_location(_,_)), % update new location
assert(agent_location(X1,Y1)). % if it has bats, it will update again
goforward(yes) :- % Ran into wall, Bump = yes
format("Not possible! Bumped a wall!~n", []).
% agent will bump if he walks
will_bump(yes) :-
agent_orientation(A),
agent_location(X, Y),
facing_wall(X, Y, A), !.
will_bump(no).
% rustle(Rustle): Rustle = yes if bats are nearby (adjacent)
% Rustle = no if no bats or
% agent is in the bat's square (for a period short of time of course)
rustle(yes) :-
agent_location(X, Y),
%\+ bat(X, Y),
has_hazard_perception(X, Y, bat),
!.
rustle(no).
% check for bats nearby or in the current square
%elsewhereville(yes) :-
% rustle(yes), % just adjacent, not in the current square
% !.
elsewhereville(Rustle) :-
agent_location(X, Y),
bat(X, Y),
!,
all_squares(All),
delete(All, [X, Y], All1), % all squares but the current one
findall([BX,BY], bat(BX, BY), AllBats), % find all bats
subtract(All1, AllBats, BatList), % remove all bats from possible new square
random_member([AX, AY], BatList), % chose a new square [AX, AY]
retractall(agent_old_location(_,_)), % update old location (again)
assert(agent_old_location(X,Y)), % the old location has a bat!
retractall(agent_location(_,_)),
assert(agent_location(AX, AY)),
rustle(Rustle),
format("Zap! Super bat snatch! Elsewhereville for you!~n", []).
elsewhereville(Rustle) :- % no rustle, no bat move
rustle(Rustle).
% shoot_arrow(Scream): If agent has an arrow, then shoot it in the
% direction the agent is facing and listen for Scream.
shoot_arrow(Scream) :-
agent_arrows(Arrows),
Arrows > 0, !, % agent has an arrow and will use it!
Arrows1 is Arrows - 1, % update number of arrows
retract(agent_arrows(Arrows)),
assert(agent_arrows(Arrows1)),
format("You now have ~d arrow(s).~n",Arrows1),
agent_location(X,Y),
agent_orientation(Angle),
propagate_arrow(X,Y,Angle,Scream).
shoot_arrow(no).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% update_agent_health:
% kills agent if in a room with a live wumpus or a pit.
update_agent_health :-
agent_location(X,Y),
wumpus_health(alive),
wumpus_location(X,Y),
!,
retract(agent_health(alive)),
assert(agent_health(dead)),
retract(agent_score(S)),
S1 is S - 1000,
assert(agent_score(S1)),
format("... Oops! You are Wumpus food!~n",[]).
update_agent_health :-
agent_location(X,Y),
pit(X,Y),
!,
retract(agent_health(alive)),
assert(agent_health(dead)),
retract(agent_score(S)),
S1 is S - 1000,
assert(agent_score(S1)),
format("Yyiiiieeee... Fell in pit!~n",[]).
update_agent_health :-
agent_num_actions(N), % current action
max_agent_actions(M), % max allowed actions
N >= M,
!,
retract(agent_health(alive)),
assert(agent_health(dead)),
retract(agent_score(S)),
S1 is S - 1000,
assert(agent_score(S1)),
format("You've starved to death inside this faultfinding cave!~n",[]).
update_agent_health.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% update_agent_orientation:
% dodeca map: agent is with his back to the door he came through.
% no bumps, no bats, agent moved alright
update_agent_orientation(no) :-
get_setup([_,dodeca|_]),
agent_old_location(Xo, _),
agent_location(Xn, _),
back_door(Xo, Xn, A), %fails if there is no path back (bat drop)
retract(agent_orientation(_)),
assert(agent_orientation(A)).
update_agent_orientation(_) :- !. % if bumped, not dodeca or no path (bat drop): nothing to do
% get_the_gold: adds gold to agents loot if any gold in the square
back_door(Xo, Xn, A) :-
dodeca_map(L),
member([Xn, Adjs], L),
nth0(I, Adjs, Xo),
A is ((I + 2) mod 4) * 90. % new angle, back to the door he came
get_the_gold :-
agent_location(X,Y),
gold(X,Y), !, % there's gold in this square!
agent_gold(NGold), % add to agents loot
NGold1 is NGold + 1,
retract(agent_gold(NGold)),
assert(agent_gold(NGold1)),
format("You now have ~d piece(s) of gold!~n",NGold1),
retract(gold(X,Y)). % delete gold from square
get_the_gold.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Display the World
%
% display_world: Displays everything known about the wumpus world
display_world :-
display_board,
wumpus_orientation(WA),
wumpus_health(WH),
wumpus_location(WLX, WLY),
wumpus_last_action(WAct),
wumpus_move_rule(Rule),
agent_orientation(AA),
agent_health(AH),
agent_arrows(N),
agent_gold(G),
agent_location(X,Y),
format('wumpus_move_rule(~w)~n',[Rule]),
format('wumpus_health(~w)~n',[WH]),
format('wumpus_orientation(~d)~n',[WA]),
format('wumpus_location(~d,~d)~n',[WLX, WLY]),
format('wumpus_last_action(~w)~n~n',[WAct]),
format('agent_health(~w)~n',[AH]),
format('agent_orientation(~d)~n',[AA]),
format('agent_location(~d,~d)~n',[X, Y]),
format('agent_arrows(~d)~n',[N]),
format('agent_gold(~d)~n',[G]).
display_board :- % squared board
get_setup([E,Type|_]),
(Type == grid ; Type == fig62),
display_rows(E, E).
display_board :- % dodecahedron board
get_setup([_,dodeca|_]),
display_dodeca.
display_rows(0, E) :-
!,
display_dashes(E).
display_rows(Row, E) :-
display_dashes(E),
display_square(1, Row, E),
Row1 is Row - 1,
display_rows(Row1 ,E).
display_square(X, _, E) :-
X > E,
!,
format('|~n',[]).
display_square(X, Y, E) :-
format('|', []),
display_info(X, Y),
X1 is X + 1,
display_square(X1, Y, E).
% display the letters 'W ', 'A ', 'P ' or 'G ', or spaces
display_info(X, Y) :-
display_location_fact(wumpus_location, X, Y, 'W'), % Wumpus
display_location_fact(agent_location, X, Y, 'A'), % Agent
display_location_fact(pit, X, Y, 'P'), % Pit
display_location_fact(bat, X, Y, 'B'), % Bat
display_location_fact(gold, X, Y, 'G'). % Gold
display_location_fact(Functor, X, Y, C) :-
Fact =.. [Functor, X, Y], % Fact = gold(X, Y)
Fact, % is Fact true?
!,
format('~w',[C]).
display_location_fact(_, _, _, _) :-
format(' ',[]).
% display ------ * E + -
display_dashes(E) :-
RowLen is (E * 6) + 1,
name('-', [Dash]),
format('~*c~n', [RowLen, Dash]).
% display dodeca 3 x 3 context near agent
display_dodeca :-
display_dodeca_rows(3).
display_dodeca_rows(0) :-
!,
display_dashes(3).
display_dodeca_rows(R) :-
display_dashes(3),
display_dodeca_1row(R),
R1 is R - 1,
display_dodeca_rows(R1).
display_dodeca_1row(3) :-
dodeca_map(L),
agent_location(X, _),
member([X, [_, C1, _, _]], L), % North
D = [0, C1, 0], % C1 may be zero
display_dodeca_squares(D).
display_dodeca_1row(2) :-
dodeca_map(L),
agent_location(X, _),
member([X, [C0, _, C2, _]], L), % East + West
D = [C2, X, C0], % Ci may be zero
display_dodeca_squares(D).
display_dodeca_1row(1) :-
dodeca_map(L),
agent_location(X, _),
member([X, [_, _, _, C3]], L), % South
D = [0, C3, 0], % C3 may be zero
display_dodeca_squares(D).
display_dodeca_squares([]) :-
!,
format('|~n',[]).
display_dodeca_squares([0|T]) :-
format('| ', []),
display_dodeca_squares(T).
display_dodeca_squares([X|T]) :-
format('|', []),
all_squares_type(dodeca, 20, S),
member([X, Y], S),
display_info(X, Y),
display_dodeca_squares(T).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Dynamic facts
% ww_addto_init_state(Fact): Adds Fact to the list L stored in
% ww_initial_state(L).
ww_addto_init_state(Fact) :-
(ww_initial_state(L) ; L = []), !,
retractall(ww_initial_state(_)),
list_to_set([Fact|L],S), % avoid duplicates
assert(ww_initial_state(S)).
% ww_assert_all :- assert all facts on ww list
ww_assert_all :-
(ww_initial_state(L) ; L = []), !,
ww_retract_all,
ww_assert_list(L),
assert(ww_initial_state(L)).
% assert_list(L): Assert all facts on list L.
ww_assert_list([]).
ww_assert_list([Fact|Facts]) :-
assert(Fact), % assert_once(Fact),
ww_assert_list(Facts).
% retract wumpus, gold, pit and bat
ww_retract_all :-
(ww_initial_state(L) ; L = []), !,
retractall(ww_initial_state(_)),
maplist(ww_par, L, Pl), % Pl = [[gold, 2], [pit, 2], ...]
list_to_set(Pl, S),
ww_retract_list(S).
ww_retract_list([]).
ww_retract_list([[N, A]|Fs]) :-
functor(U, N, A), % functor(U, gold, 2), U=gold(_,_)
retractall(U), % retract all facts in the list
ww_retract_list(Fs).
ww_par(F, P) :- % F=gold(1,3), P=[gold, 2]
functor(F, N, A),
P = [N, A].
% place_objects(Object,P,Squares): For each square in Squares, place
% Object at square with probability P.
ww_place_it(_, _, []).
ww_place_it(gold, Qt, Sq) :-
float(Qt), !,
ww_place_objects_det(gold, 1, Sq), % put one for sure
ww_place_objects_prob(gold, Qt, Sq). % and lets see how many others
ww_place_it(Ob, Qt, Sq) :-
float(Qt),
ww_place_objects_prob(Ob, Qt, Sq).
ww_place_it(Ob, Qt, Sq) :-
integer(Qt),
ww_place_objects_det(Ob, Qt, Sq).
ww_place_objects_prob(_, _, []).
ww_place_objects_prob(Object, P, [Sq|Squares]) :-
maybe(P), % succeeds with probability P
!,
Fact =.. [Object|Sq], % Fact = pit(X,Y)
ww_addto_init_state(Fact),
ww_place_objects_prob(Object, P, Squares).
ww_place_objects_prob(Object, P, [_|Squares]) :-
ww_place_objects_prob(Object, P, Squares).
ww_place_objects_det(_, _, []).
ww_place_objects_det(_, 0, [_|_]).
ww_place_objects_det(Obj, Qtd, [H|T]) :-
Qtd>0,
random_member(Sq, [H|T]),
delete([H|T], Sq, S1),
Fact =.. [Obj | Sq], % Fact = pit(X,Y)
ww_addto_init_state(Fact),
Q1 is Qtd - 1,
ww_place_objects_det(Obj, Q1, S1).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check for goforward, turnright, turnleft, shoot, grab, climb, sit or gps.