-
Notifications
You must be signed in to change notification settings - Fork 0
/
WWSolver.py
471 lines (424 loc) · 16.8 KB
/
WWSolver.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
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
import copy
class WWSolver:
# Attributes
# Constructor
def __init__(self):
self.worldmap = []
self.world = {}
# Not yet creating agent object, as the world is empty
#self.agent = self.agent(self)
# Methods
def loadWorld(self, text):
#Parse text
lines = text.splitlines()
size_str = lines[0]
size_X = int(size_str[-3])
size_Y = int(size_str[-1])
size_X += 1
size_Y += 1
self.worldmap = [["" for i in range(size_X)] for j in range(size_Y)]
for i in range(1, len(lines)):
line = lines[i].split(",")[:-1] if lines[i].split(",")[-1] == '' else lines[i].split(",")
# NOTE: x and y are switched from normal convention in the provided example, so using that convention here
x = int(line[1])
y = int(line[0])
features = "/".join(line[2::])
self.world[(x,y)] = features
def printWorldMap(self):
print("World Map (True):")
truemap = copy.deepcopy(self.worldmap)
for j in range(len(truemap)):
for i in range(len(truemap[j])):
key = (i,j)
if key in self.world:
truemap[j][i] = self.world[key]
print(truemap[j])
print()
def printAgentMaps(self):
print("Agent Map (Known):")
knowmap = copy.deepcopy(self.worldmap)
for j in range(len(knowmap)):
for i in range(len(knowmap[j])):
key = (i,j)
if key in self.agent.knowledge:
knowmap[j][i] = self.agent.knowledge[key]
print(knowmap[j])
print()
print("Agent Map (Predicted):")
predmap = copy.deepcopy(self.worldmap)
for j in range(len(predmap)):
for i in range(len(predmap[j])):
key = (i,j)
if key in self.agent.prediction:
predmap[j][i] = self.agent.prediction[key]
print(predmap[j])
print()
def solve(self):
# Create agent object with world details
self.agent = self.agent(self)
def killWumpus(self):
world = self.world
# This method is called if the Wumpus is shot
# Removes Wumpus and all stench from world
WS_locs = []
for key,val in world.items():
if any(("W" in feature) or ("S" in feature) for feature in val):
WS_locs.append(key)
for key in WS_locs:
if len(world[key]) == 1:
del world[key]
elif len(world[key]) == 3:
val = world[key]
val = val.replace("S", "")
val = val.replace("W", "")
val = val.replace("/", "")
if len(val) == 0:
del world[key]
else:
world[key] = val
else:
val = world[key]
val = val.replace("/S", "")
val = val.replace("S/", "")
val = val.replace("/W", "")
val = val.replace("W/", "")
world[key] = val
# Need to also remove Wumpus and Stench from Agent knowledge and predictions
#self.agent.???
# This method is for checking and debugging while writing code
def checkPrint(self):
self.printWorldMap()
print(self.world)
print()
self.printAgentMaps()
print("Known Map")
print(self.agent.knowledge)
print("Predicted Map")
print(self.agent.prediction)
print()
# Agent inner class
class agent:
# Constructor of inner class
def __init__(self, WWSolver):
self.WWSolver = WWSolver
#truemap = WWSolver.worldmap
#print("LENGTH WorldMap" + str(len(truemap)))
self.last_moves = {}
self.predicted_moves = {}
self.knowledge = {}
self.prediction = {}
self.best_moves = {}
for j in range(len(WWSolver.worldmap)):
for i in range(len(WWSolver.worldmap[j])):
self.position = [int(j), int(i)] #The position of the cell is defined here.
self.predict = ''
self.right = True
self.left = True
self.up = True
self.down = True
self.gold = False
self.length_map = len(WWSolver.worldmap)
print("Start!")
self.checkSquare()
print("Predicted Moves: \n" + str(self.predicted_moves))
print("Best Moves: \n" + str(self.best_moves))
self.make_move(self.best_moves)
# Methods of inner class
def make_move(self, best_moves):
world = self.WWSolver.world
for key,val in best_moves:
self.position[0] = key
self.position[1] = val
print("Agent's new position is on :" + str(key) + ", " + str(val))
check_key = (key, val)
if check_key in world:
feature = world[(key, val)]
self.shoot(key, val)
self.knowledge[(key,val)] = feature
self.prediction[(key,val)] = feature
if('G' in feature):
print("Gold taken")
break;
def checkSquare(self):
world = self.WWSolver.world
x = self.position[0]
y = self.position[1]
key = (x,y)
print(key)
if key in world:
features = world[(self.position[0],self.position[1])]
if features == 'E':
print("The agent's current position is at " + str(x) + "," + str(y))
self.knowledge[(x,y)] = features
self.prediction[(x,y)] = features
#solver = WWSolver.printAgentMaps(self.WWSolver.worldmap)
if x == 0 and y == 0:
self.last_moves[(x,y)] = features
self.predicted_moves[(x,y)] = features
if (self.right):
self.position[0] += 1
self.start_moving(self.position[0], self.position[1], self.length_map)
if (self.down):
self.position[1] += 1
self.start_moving(self.position[0], self.position[1], self.length_map)
elif x < self.length_map and y < self.length_map:
self.start_moving(self.position[0], self.position[1], self.length_map)
else:
self.knowledge[(x,y)] = features
self.prediction[(x,y)] = features
#solver = WWSolver.printAgentMaps(self.WWSolver.worldmap)
if x == 0 and y == 0:
self.last_moves[(x,y)] = features
self.predicted_moves[(x,y)] = features
if (self.right):
self.position[0] += 1
self.start_moving(self.position[0], self.position[1], self.length_map)
if (self.down):
self.position[1] += 1
self.start_moving(self.position[0], self.position[1], self.length_map)
elif x < self.length_map and y < self.length_map:
self.start_moving(self.position[0], self.position[1], self.length_map)
print("Features at " + str(x) + "," + str(y) + " are: " + features)
self.knowledge[(x,y)] = features
self.prediction[(x,y)] = features
else:
print("No features at " + str(x) + "," + str(y))
def shoot(self, pos_x, pos_y):
world = self.WWSolver.world
# Get agent position
x = pos_x
y = pos_y
W_loc_x = -1
W_loc_y = -1
# Get Wumpus position
for key,val in world.items():
if any("W" in feature for feature in val):
W_loc_x = key[0]
W_loc_y = key[1]
# Check if Wumpus is shot
# If so, remove it and all stench from board
if W_loc_x > 0 and W_loc_y > 0:
hit = False
if x == W_loc_x and y > W_loc_y:
# direction = 'Up'
hit = True
elif x == W_loc_x and y < W_loc_y:
hit = True
# direction = 'Down'
elif x > W_loc_x and y == W_loc_y:
hit = True
# direction = 'Left'
elif x < W_loc_x and y == W_loc_y:
hit = True
# direction = 'Right'
if hit:
self.WWSolver.killWumpus()
print("Shot the Wumpus")
else:
print("You missed your shot!")
else:
print("Wumpus has already been shot!!!")
def moveRight(self):
#world = self.WWSolver.world
self.right = False
self.position[0] += 1
#self.checkSquare(world)
def moveLeft(self):
#world = self.WWSolver.world
self.left = False
self.position[0] -= 1
#self.checkSquare(world)
def moveUp(self):
#world = self.WWSolver.world
self.up = False
self.position[1] += 1
#self.checkSquare(world)
def moveDown(self):
#world = self.WWSolver.world
self.down = False
self.position[1] -= 1
#self.checkSquare(world)
def move(self):
#world = self.WWSolver.world
if(self.right==False):
self.right = True
self.moveLeft()
elif(self.left==False):
self.left = True
self.moveRight()
elif(self.up==False):
self.up = True
self.MoveDown()
elif(self.down==False):
self.down = True
self.moveUp()
def start_moving(self, pos0, pos1, length_map):
world = self.WWSolver.world
key = (pos0, pos1)
if key in world:
features = world[(pos0,pos1)]
temp_xR = pos0 + 1# Right => +1
temp_xD = pos1 + 1# Down => +1
temp_xU = pos1 - 1# Up => -1
temp_xL = pos0 - 1# Left => -1
if features == '':
self.best_moves[(pos0,pos1)] = features
elif features == 'B':
self.predicted_moves[(pos0,pos1)] = features
self.best_moves[(pos0,pos1)] = features
if temp_xR < length_map and temp_xR >= 0:
pos0 += 1
temp_xR -= 1
key = (pos0, pos1)
if key in world:
features = world[(pos0, pos1)]
else:
features = ''
predict = 'P'
if features == predict:
pos0 -= 1
self.predicted_moves[(pos0,pos1)] = predict
self.move()
elif features == '':
self.best_moves[(pos0,pos1)] = features
if temp_xD < length_map and temp_xD >= 0:
pos1 += 1
temp_xD -= 1
key = (pos0, pos1)
if key in world:
features = world[(pos0, pos1)]
else:
features = ''
predict = 'P'
if features == predict:
pos1 -= 1
self.predicted_moves[(pos0,pos1)] = predict
self.move()
elif features == '':
self.best_moves[(pos0,pos1)] = features
if temp_xU < length_map and temp_xU >= 0:
pos1 -= 1
temp_xU -= 1
key = (pos0, pos1)
if key in world:
features = world[(pos0, pos1)]
else:
features = ''
predict = 'P'
if features == predict:
pos1 += 1
self.predicted_moves[(pos0,pos1)] = predict
self.move()
elif features == '':
self.best_moves[(pos0,pos1)] = features
if temp_xL < length_map and temp_xL >= 0:
pos0 -= 1
key = (pos0, pos1)
if key in world:
features = world[(pos0, pos1)]
else:
features = ''
predict = 'P'
if features == predict:
pos0 += 1
self.predicted_moves[(pos0,pos1)] = predict
self.move()
elif features == '':
self.best_moves[(pos0,pos1)] = features
elif features == 'S' or features == 'B/S':
self.predicted_moves[(pos0,pos1)] = features
self.best_moves[(pos0,pos1)] = features
if temp_xR < length_map and temp_xR >= 0:
pos0 += 1
temp_xR -= 1
key = (pos0, pos1)
if key in world:
features = world[(pos0, pos1)]
else:
features = ''
predict = 'W'
if features == predict:
pos0 -= 1
self.predicted_moves[(pos0,pos1)] = predict
self.move()
elif features != predict and features == 'P':
pos0 -= 1
self.predicted_moves[(pos0,pos1)] = predict
self.move()
elif features != predict and 'G' in features:
self.best_moves[(pos0,pos1)] = features
self.predicted_moves[(pos0,pos1)] = features
self.gold = True
elif features == '':
self.best_moves[(pos0,pos1)] = features
if temp_xD < length_map and temp_xD >= 0:
pos1 += 1
temp_xD -= 1
key = (pos0, pos1)
if key in world:
features = world[(pos0, pos1)]
else:
features = ''
predict = 'W'
if features == predict:
pos1 -= 1
self.predicted_moves[(pos0,pos1)] = predict
self.move()
elif features != predict and features == 'P':
pos1 -= 1
self.predicted_moves[(pos0,pos1)] = predict
self.move()
elif features != predict and 'G' in features:
self.best_moves[(pos0,pos1)] = features
self.predicted_moves[(pos0,pos1)] = features
self.gold = True
elif features == '':
self.best_moves[(pos0,pos1)] = features
if temp_xU < length_map and temp_xU >= 0:
pos1 -= 1
temp_xU -= 1
key = (pos0, pos1)
if key in world:
features = world[(pos0, pos1)]
else:
features = ''
predict = 'W'
if features == predict:
pos1 -= 1
self.predicted_moves[(pos0,pos1)] = predict
self.move()
elif features != predict and features == 'P':
pos1 -= 1
self.predicted_moves[(pos0,pos1)] = predict
self.move()
elif features != predict and 'G' in features:
self.best_moves[(pos0,pos1)] = features
self.predicted_moves[(pos0,pos1)] = features
self.gold = True
elif features == '':
self.best_moves[(pos0,pos1)] = features
if temp_xL < length_map and temp_xL >= 0:
pos0 -= 1
key = (pos0, pos1)
if key in world:
features = world[(pos0, pos1)]
else:
features = ''
predict = 'W'
if features == predict:
pos0 += 1
self.predicted_moves[(pos0,pos1)] = predict
self.move()
elif features != predict and features == 'P':
pos0 += 1
self.predicted_moves[(pos0,pos1)] = predict
self.move()
elif features != predict and 'G' in features:
self.best_moves[(pos0,pos1)] = features
self.predicted_moves[(pos0,pos1)] = features
self.gold = True
elif features == '':
self.best_moves[(pos0,pos1)] = features
else:
features = ''
self.best_moves[(pos0,pos1)] = features