This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhunt_the_wumpus.pl
141 lines (113 loc) · 4 KB
/
hunt_the_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
:- use_module(library(random)).
:- use_module(library(readln)).
:- [maze].
:- [obstacles].
:- [events].
:- [senses].
:- [printer].
:- [actions].
:- [math_helpers].
:- prompt(_, 'go/shoot + room number: ').
:- dynamic ([current_room/1,
wumpus/1,
quiver/1,
energy/1,
target/1,
pit1/1,
pit2/1,
bat1/1,
bat2/1,
targetedRooms/1,
lost_arrow/1]).
get_input :- readln(Input), get_input(Input), nl.
get_input(Input) :- process_input(Input), get_input.
% move to a different room
process_input([go, NewRoom]) :-
current_room(Current),
map_room(NewRoom, ActualRoomNum),
connected(Current, ActualRoomNum),
change_room(ActualRoomNum).
% shoot an arrow
process_input([shoot, _]) :-
quiver(ArrowsLeft),
lost_arrow(_),
ArrowsLeft = 0,
write("You have no arrows. Maybe you are lucky to find one..."), nl,
process_invalid_input.
process_input([shoot, FirstRoom|OtherRooms]) :-
current_room(Current),
map_room(FirstRoom, ActualRoomNum),
connected(Current, ActualRoomNum),
shoot_arrow(Current, [FirstRoom|OtherRooms]).
% quit
process_input([q]) :-
abort.
process_input(_) :-
process_invalid_input.
process_invalid_input :-
write("Either go or shoot in the direction of: "), nl,
print_choices, nl.
play :-
write("You are a brave hunter aiming to take down the fearsome Wumpus."), nl,
write("You will need to seek it out in this twisting maze."), nl,
write("enter 'go x' where x is one of the adjacent room numbers to explore."), nl, nl,
write("Be wary of not only the Wumpus but also of giant bats and bottomless pits."), nl,
write("Trust your senses, they will alert you when danger is near."), nl, nl,
write("You have your trusty bow and 5 arrows in your quiver, use them wisely."), nl,
write("You also have the special skill of shooting arrows through a curved path of up to five connected rooms."), nl,
write("To do so, enter 'shoot a b c d e' where a b c d and e are adjacent room numbers."), nl, nl,
write("The Wumpus is cunning. If you fire an arrow and miss your target:"), nl,
write(" If the Wumpus is nearby, it will hunt you down."), nl,
write(" If the Wumpus is far away but hears the arrow, it will escape to another room."), nl,
write("Best of luck, and happy hunting!"), nl, nl,
% purge the KB
retractall(pit1(_)),
retractall(pit2(_)),
retractall(bat1(_)),
retractall(bat2(_)),
retractall(wumpus(_)),
retractall(target(_)),
retractall(energy(_)),
retractall(quiver(_)),
retractall(targetedRooms(_)),
retractall(current_room(_)),
retractall(lost_arrow(_)),
retractall(rand_rooms(_)),
% assign a dummy room at the start
assertz(current_room(999)),
assertz(target(999)),
% 6 = 2 pits + 2 bats + 1 hunter + 1 lost arrow
rooms_list(R),
sample_without_replacement(R, 6, Sample),
%print(Sample), nl,
% set random room number mapping
sample_without_replacement(R, 20, RandRooms),
assertz(rand_rooms(RandRooms)),
% fill the quiver with arrows
assertz(quiver(5)),
% setup bottomless pits
nth1(1, Sample, P1),
nth1(2, Sample, P2),
assertz(pit1(P1)),
assertz(pit1(P2)),
% setup bat caves
nth1(3, Sample, B1),
nth1(4, Sample, B2),
assertz(bat1(B1)),
assertz(bat2(B2)),
% add location for Wumpus into KB
nth1(5, Sample, H),
random_location_for_wumpus(H, W),
assertz(wumpus(W)),
% setup lost arrow location
nth1(6, Sample, LA),
assertz(lost_arrow(LA)),
% uncomment the "map_room" and "write" calls below for debugging purposes
%map_room(MW, W),
%map_room(MLA, LA),
%write("Wumpus is actually here: "), print(W), nl,
%write("Wumpus appears to be here: "), print(MW), nl,
%write("Lost arrow is here: "), print(MLA), nl,
% perform initial room assignment to trigger events/senses
change_room(H),
get_input.