-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWumpusED_2.py
384 lines (383 loc) · 17.6 KB
/
WumpusED_2.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
import Tkinter, os, random, pickle
import Image, ImageTk, ImageDraw, ImageFont
class GUI(Tkinter.Toplevel):
def __init__(self, root, width, height):
Tkinter.Toplevel.__init__(self, root)
self.rows = 12
self.cols = 12
self.done = 0
self.quit = 0
self.root = root
self.width = width
self.height = height
self.visible = 1
self.title("PyrobotSimulator: WumpusWorld")
self.canvas = Tkinter.Canvas(self,width=self.width,height=self.height,bg="white")
self.canvas.pack()
self.winfo_toplevel().protocol('WM_DELETE_WINDOW',self.destroy)
# --------------------------------------------------------
# Tries to find module folder
X = __import__(__name__)
folder = X.__file__
index = len(folder) - 1
for i in range(len(folder)):
if folder[i] == '/' or folder[i] == '\\':
index = i
folder = folder[:index]
# --------------------------------------------------------
self.goldFilename = folder + "/images/gold2.gif"
self.wumpusFilename = folder + "/images/wumpus2.gif"
self.pitFilename = folder + "/images/pit2.gif"
self.agentFilename = folder + "/images/agent2.gif"
self.doorFilename = folder + "/images/door.gif"
self.keyFilename = folder + "/images/key.gif"
self.robot_goldFilename = folder + "/images/robot_gold.gif"
self.robot_wumpusFilename = folder + "/images/robot_wumpus.gif"
self.robot_doorFilename = folder + "/images/robot_door.gif"
self.robot_keyFilename = folder + "/images/robot_key.gif"
self.exitFilename = folder + "/images/exit.gif"
# --------------------------------------------------------
self.goldImage = Image.open(self.goldFilename)
self.goldImage = self.goldImage.resize((50, 50), Image.BILINEAR)
self.wumpusImage = Image.open(self.wumpusFilename)
self.wumpusImage = self.wumpusImage.resize((50, 50), Image.BILINEAR)
self.pitImage = Image.open(self.pitFilename)
self.pitImage = self.pitImage.resize((50, 50), Image.BILINEAR)
self.agentImage = Image.open(self.agentFilename)
self.agentImage = self.agentImage.resize((50, 50), Image.BILINEAR)
self.doorImage = Image.open(self.doorFilename)
self.doorImage = self.doorImage.resize((50, 50), Image.BILINEAR)
self.keyImage = Image.open(self.keyFilename)
self.keyImage = self.keyImage.resize((50, 50), Image.BILINEAR)
self.robot_goldImage = Image.open(self.robot_goldFilename)
self.robot_goldImage = self.robot_goldImage.resize((50, 50), Image.BILINEAR)
self.robot_wumpusImage = Image.open(self.robot_wumpusFilename)
self.robot_wumpusImage = self.robot_wumpusImage.resize((50, 50), Image.BILINEAR)
self.robot_doorImage = Image.open(self.robot_doorFilename)
self.robot_doorImage = self.robot_doorImage.resize((50, 50), Image.BILINEAR)
self.robot_keyImage = Image.open(self.robot_keyFilename)
self.robot_keyImage = self.robot_keyImage.resize((50, 50), Image.BILINEAR)
self.exitImage = Image.open(self.exitFilename)
self.exitImage = self.exitImage.resize((50, 50), Image.BILINEAR)
# --------------------------------------------------------
self.goldImageTk = ImageTk.PhotoImage(self.goldImage)
self.wumpusImageTk = ImageTk.PhotoImage(self.wumpusImage)
self.pitImageTk = ImageTk.PhotoImage(self.pitImage)
self.agentImageTk = ImageTk.PhotoImage(self.agentImage)
self.doorImageTk = ImageTk.PhotoImage(self.doorImage)
self.keyImageTk = ImageTk.PhotoImage(self.keyImage)
self.robot_goldImageTk = ImageTk.PhotoImage(self.robot_goldImage)
self.robot_wumpusImageTk = ImageTk.PhotoImage(self.robot_wumpusImage)
self.robot_doorImageTk = ImageTk.PhotoImage(self.robot_doorImage)
self.robot_keyImageTk = ImageTk.PhotoImage(self.robot_keyImage)
self.exitImageTk = ImageTk.PhotoImage(self.exitImage)
self.ticCounter = 0
# --------------------------------------------------------
self.properties = ["sonar","location"]
for i in self.properties:
self.__dict__[i] = None
self.initWorld()
self.checkMovement()
self.count = 0
self.tag = "data-%d" % self.count
self.ports = [60000]
self.redraw()
def initWorld(self):
self.maxTics = 100
self.location = (2, 1)
self.x, self.y = self.location
self.dead = 0
self.arrow = 1
self.wumpusDead = 0
self.gold = 0
self.world = [['P' for y in range(self.cols)] for x in range(self.rows)]
# '' = nothing
# 'W' = wumpus
# 'G' = gold
# 'P' = pit
# 'A' = agent
# 'D' = door
# 'K' = key
self.world[2][10] = ''
self.world[3][10] = ''
self.world[4][10] = ''
self.world[6][10] = 'G'
self.world[7][10] = ''
self.world[8][10] = ''
self.world[9][10] = ''
self.world[10][10] = ''
self.world[2][9] = ''
self.world[4][9] = ''
self.world[8][9] = ''
self.world[2][8] = ''
self.world[4][8] = ''
self.world[5][8] = ''
self.world[6][8] = ''
self.world[7][8] = ''
self.world[8][8] = ''
self.world[9][8] = ''
self.world[10][8] = ''
self.world[1][7] = ''
self.world[2][7] = ''
self.world[8][7] = ''
self.world[1][6] = ''
self.world[4][6] = ''
self.world[5][6] = ''
self.world[6][6] = ''
self.world[8][6] = ''
self.world[9][6] = ''
self.world[10][6] = ''
self.world[1][5] = ''
self.world[2][5] = ''
self.world[4][5] = ''
self.world[6][5] = ''
self.world[2][4] = ''
self.world[6][4] = ''
self.world[8][4] = ''
self.world[9][4] = ''
self.world[10][4] = ''
self.world[1][3] = ''
self.world[2][3] = ''
self.world[3][3] = ''
self.world[4][3] = 'W'
self.world[5][3] = ''
self.world[6][3] = ''
self.world[8][3] = ''
self.world[10][3] = ''
self.world[4][2] = ''
self.world[6][2] = ''
self.world[8][2] = ''
self.world[10][2] = ''
self.world[2][1] = ''
self.world[3][1] = ''
self.world[4][1] = ''
self.world[6][1] = ''
self.world[7][1] = ''
self.world[8][1] = ''
self.world[10][1] = ''
self.exit_location = (1, 1)
self.doors_opens = {}
self.key_values = {}
self.wumpus_talk = {}
self.wumpus_talk[4, 3] = ['left', 'up', 'up', 'left']
self.win = False
self.lose = False
def add(self, loc, dir):
x = 0
if loc[0] + dir[0] >= 0 and loc[0] + dir[0] < self.cols:
x = loc[0] + dir[0]
else:
x = loc[0]
if loc[1] + dir[1] >= 0 and loc[1] + dir[1] < self.rows:
y = loc[1] + dir[1]
else:
y = loc[1]
self.location = (x, y)
self.x, self.y = self.location
def checkMovement(self):
value = None
if self.world[self.location[0]][self.location[1]] == 'W':
value = 'wumpus'
elif self.world[self.location[0]][self.location[1]] == 'G':
value = 'gold'
elif self.world[self.location[0]][self.location[1]] == 'D':
value = 'door'
elif self.world[self.location[0]][self.location[1]] == 'K':
value = 'key'
return value
def inLine(self, loc, change):
xpos, ypos = self.sum(loc , change)
while ypos >= 0 and ypos < self.rows and xpos >= 0 and xpos < self.cols:
if 'W' in self.world[xpos][ypos]:
self.wumpusDead = 1
self.world[xpos][ypos] = self.world[xpos][ypos].replace('W', '')
xpos, ypos = self.sum((xpos,ypos), change)
def sum(self, a, b):
return a[0] + b[0], a[1] + b[1]
def process(self, request, sockname):
retval = "error"
if request.count('connectionNum'):
connectionNum, port = request.split(":")
retval = self.ports.index( int(port) )
elif request == 'location':
retval = (self.location[0] + 1, self.location[1] + 1)
elif request == 'sonar':
#retval = (self.world[self.x+1][self.y]!='P',self.world[self.x][self.y-1]!='P',self.world[self.x-1][self.y]!='P',self.world[self.x][self.y+1]!='P')
retval = {'right' : self.world[self.x+1][self.y]!='P', 'down' : self.world[self.x][self.y-1]!='P', 'left' : self.world[self.x-1][self.y]!='P', 'up' : self.world[self.x][self.y+1]!='P'}
elif request == 'reset':
self.initWorld()
retval = "ok"
self.win = False
self.lose = False
self.ticCounter = 0
self.redraw()
elif request == 'win':
retval = self.win
elif self.win:
retval = 'You win!'
elif self.world[self.location[0]][self.location[1]] == 'E':
self.win = True
retval = 'You win!'
elif self.lose:
retval = 'Robot batery empty: You lose! (reset to restart)'
elif request == 'golds':
retval = sum([self.world[y].count('G') for y in range(self.cols)])
self.redraw()
elif request == 'talk':
if (self.world[self.location[0]][self.location[1]] == 'W'):
try:
retval = self.wumpus_talk[(self.location[0], self.location[1])].pop(0)
if len(self.wumpus_talk[(self.location[0], self.location[1])]) == 0:
self.world[self.location[0]][self.location[1]] = ''
except Exception:
pass
elif (self.world[self.location[0]][self.location[1]] == 'K'):
try:
retval = self.key_values[(self.location[0], self.location[1])].pop(0)
if len(self.key_values[(self.location[0], self.location[1])]) == 0:
self.world[self.location[0]][self.location[1]] = ''
except Exception:
pass
elif (self.world[self.location[0]][self.location[1]] == 'D'):
try:
if len(self.doors_opens[(self.location[0], self.location[1])][0]) == 0:
retval = None
else:
retval = self.doors_opens[(self.location[0], self.location[1])][0].pop(0)
except Exception:
pass
else:
retval = "This thing doesn't speak!"
self.redraw()
elif request == 'grab':
if (self.world[self.location[0]][self.location[1]] == 'G'):
self.world[self.location[0]][self.location[1]] = ''
if sum([self.world[y].count('G') for y in range(self.cols)]) == 0:
self.world[self.exit_location[0]][self.exit_location[1]] = 'E'
retval = 'ok'
else:
retval = 'no gold to grab'
self.redraw()
elif request == 'left':
if self.ticCounter > self.maxTics:
self.lose = True
retval = 'Robot batery empty: You lose! (reset to restart)'
else:
try:
if self.world[self.location[0] - 1][self.location[1]] != 'P':
self.add(self.location, (-1, 0))
self.ticCounter += 1
except Exception:
pass
retval = self.checkMovement()
self.redraw()
elif request == 'right':
if self.ticCounter > self.maxTics:
self.lose = True
retval = 'Robot batery empty: You lose! (reset to restart)'
else:
try:
if self.world[self.location[0] + 1][self.location[1]] != 'P':
self.add(self.location, (1, 0))
self.ticCounter += 1
except Exception:
pass
retval = self.checkMovement()
self.redraw()
elif request == 'up':
if self.ticCounter > self.maxTics:
self.lose = True
retval = 'Robot batery empty: You lose! (reset to restart)'
else:
try:
if self.world[self.location[0]][self.location[1] + 1] != 'P':
self.add(self.location, (0, 1))
self.ticCounter += 1
except Exception:
pass
retval = self.checkMovement()
self.redraw()
elif request == 'down':
if self.ticCounter > self.maxTics:
self.lose = True
retval = 'Robot batery empty: You lose! (reset to restart)'
else:
try:
if self.world[self.location[0]][self.location[1] - 1] != 'P':
self.add(self.location, (0, -1))
self.ticCounter += 1
except Exception:
pass
retval = self.checkMovement()
self.redraw()
elif request == 'supportedFeatures':
retval = []
elif request == 'builtinDevices':
retval = []
else:
#self.doors_opens = {(6, 8) : [['red', 'blue', 'green', 'pink'], 'currupipi', 3, 8]}
#self.wumpus_talk = {(3, 6) : ['left', 'right', 'up', 'down'], (8, 3) : ['left', 'right']}
#self.key_values = {(5, 8) : [currupipi]}
# Open the door if any
try:
hash_list = self.doors_opens[(self.location[0], self.location[1])]
if request == hash_list[1]:
self.world[self.location[0]][self.location[1]] = ''
self.world[hash_list[2]][hash_list[3]] = ''
retval = 'Opened'
self.redraw()
else:
retval = 'Failure'
except Exception:
pass
return pickle.dumps(retval)
def redraw(self):
oldtag = self.tag
self.count = int(not self.count)
self.tag = "data-%d" % self.count
for x in range(self.cols):
for y in range(self.rows):
posx = x * 50
posy = ((self.rows - 1) * 50) - y * 50
if 'P' in self.world[x][y]:
self.canvas.create_image(posx, posy, image = self.pitImageTk, anchor=Tkinter.NW,tag=self.tag)
if 'W' in self.world[x][y]:
self.canvas.create_image(posx, posy, image = self.wumpusImageTk, anchor=Tkinter.NW,tag=self.tag)
if 'G' in self.world[x][y]:
self.canvas.create_image(posx, posy, image = self.goldImageTk, anchor=Tkinter.NW,tag=self.tag)
if 'D' in self.world[x][y]:
self.canvas.create_image(posx, posy, image = self.doorImageTk, anchor=Tkinter.NW,tag=self.tag)
if 'K' in self.world[x][y]:
self.canvas.create_image(posx, posy, image = self.keyImageTk, anchor=Tkinter.NW,tag=self.tag)
if 'E' in self.world[x][y]:
self.canvas.create_image(posx, posy, image = self.exitImageTk, anchor=Tkinter.NW,tag=self.tag)
if self.location[0] == x and self.location[1] == y:
if 'G' in self.world[x][y]:
self.canvas.create_image(posx, posy, image = self.robot_goldImageTk, anchor=Tkinter.NW,tag=self.tag)
elif 'W' in self.world[x][y]:
self.canvas.create_image(posx, posy, image = self.robot_wumpusImageTk, anchor=Tkinter.NW,tag=self.tag)
elif 'D' in self.world[x][y]:
self.canvas.create_image(posx, posy, image = self.robot_doorImageTk, anchor=Tkinter.NW,tag=self.tag)
elif 'K' in self.world[x][y]:
self.canvas.create_image(posx, posy, image = self.robot_keyImageTk, anchor=Tkinter.NW,tag=self.tag)
elif 'E' in self.world[x][y]:
self.canvas.create_image(posx, posy, image = self.exitImageTk, anchor=Tkinter.NW,tag=self.tag)
else:
self.canvas.create_image(posx, posy, image = self.agentImageTk, anchor=Tkinter.NW,tag=self.tag)
# ------------------------------------------------------------------------
for x in range(self.cols):
px = x * 50
self.canvas.create_line(px, 0, px, 50 * self.rows, width = 2, fill = "black", tag = self.tag)
for x in range(self.rows):
px = x * 50
self.canvas.create_line( 0, px, 50 * self.cols, px, width = 2, fill = "black", tag = self.tag)
# ------------------------------------------------------------------------
self.canvas.delete(oldtag)
def destroy(self):
self.done = 1 # stop processing requests, if handing
self.quit = 1 # stop accept/bind toplevel
self.root.quit() # kill the gui
def INIT():
root = Tkinter.Tk()
root.withdraw()
return GUI(root, 600, 600)