-
Notifications
You must be signed in to change notification settings - Fork 0
/
KB.py
121 lines (102 loc) · 4.69 KB
/
KB.py
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
######################################
# The KB class will store information about your interactions with the game.
# You will use the KB to help you make a decision about where to move next.
# So, for example, if you use the MOVE command and the game gives you the following results:
# You are in room 4.
# Passages lead to [1, 3, 8]
# I smell a Wumpus!
#
# The KB would suggest which is a better option of the three possibilities using previous facts and inference.
#####################################
import aima3.utils
import aima3.logic
class KB():
clauses = []
breeze=[]
not_breeze=[]
smell=[]
not_smell=[]
safe=[]
conns={}
def __init__(self):
self.conns["1"] = [2,4,5]
self.conns["2"] = [3,1,6]
self.conns["3"] = [4,2,7]
self.conns["4"] = [1,3,8]
self.conns["5"] = [1,6,8]
self.conns["6"] = [2,7,5]
self.conns["7"] = [3,8,6]
self.conns["8"] = [4,5,7]
def smell_info(self, location, smell_wumpus):
if smell_wumpus:
if str(location) not in (self.smell):
self.smell.append(str(location))
else:
if str(location) not in (self.not_smell):
self.not_smell.append(str(location))
def breeze_info(self, location, feel_breeze):
if feel_breeze:
if str(location) not in (self.breeze):
self.breeze.append(str(location))
else:
if str(location) not in (self.not_breeze):
self.not_breeze.append(str(location))
def location_info(self, current_room):
suggestion=""
if not str(current_room) in self.safe:
self.safe.append(str(current_room))
def make_suggestion(self, options):
suggestion=""
for rm in options:
found=False
if str(rm) in self.smell and str(rm) in self.breeze:
if str(rm) in self.safe:
suggestion =suggestion + "You have been here before but you could safely move to room " + str(rm) + "\n"
found=True
else:
suggestion =suggestion + "Strong suggestion to move to room " + str(rm) + "\n"
found=True
else:
#print("Step 1")
if not rm in self.breeze:
#print("Step 2")
conns_to_room=self.conns[str(rm)]
#print("is " + str(conns_to_room) + " in " + str(self.breeze))
counter=0
for rm_conn in conns_to_room:
if rm_conn==rm:
continue
##If one of the connected rooms senses a breeze then maybe the room has a pit
if str(rm_conn) in self.breeze:
counter = counter + 1
if counter ==1 and str(rm) not in self.safe:
suggestion =suggestion + "Uh oh: There is a chance that room " + str(rm) + " could mean trouble.\n"
found=True
elif counter >=2 and str(rm) not in self.safe:
suggestion =suggestion + "Uh oh: Room " + str(rm) + " means trouble.\n"
found=True
if not rm in self.smell:
#print("Step 3")
conns_to_room=self.conns[str(rm)]
#print("is " + str(conns_to_room) + " in " + str(self.smell))
counter=0
for rm_conn in conns_to_room:
if rm_conn==rm:
continue
##If one of the connected rooms senses a smell then maybe the room has a wumpus
if str(rm_conn) in self.breeze:
counter = counter + 1
if counter ==1 and str(rm) not in self.safe:
suggestion =suggestion + "Uh oh: There is a chance that room " + str(rm) + " could mean trouble.\n"
found=True
elif counter >=2 and str(rm) not in self.safe:
suggestion =suggestion + "Uh oh: Room " + str(rm) + " means trouble.\n"
found=True
if not found:
if str(rm) in self.safe:
suggestion =suggestion + "Sometimes safety is a place you've been before, try room " + str(rm) + "\n"
else:
suggestion += "I don't have enough information about room: " +str(rm) + ". Sorry, you are on your own.\n"
if (len(suggestion))<1:
suggestion="KB Doesn't Have Enough Information to Help--Sorry"
print(suggestion)