-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.py
66 lines (54 loc) · 1.51 KB
/
mouse.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
from selenium import webdriver
from time import sleep
from random import shuffle
from re import sub
def loadMaze():
browser = webdriver.Firefox()
browser.get('file:///home/snail/Documents/code/mouse/rooms/room39.html')
return browser
def chooseDoor(maze,knownRooms):
doorList = []
idealDoorList = []
doorList = maze.find_elements_by_xpath('//a')
for door in doorList:
doorURL = door.get_attribute('href')
doorNumber = sub('^file.*rooms\/room|.html','',doorURL)
if doorNumber not in knownRooms:
knownRooms.append(doorNumber)
idealDoorList.append(door)
if len(idealDoorList) >= 1:
print 'Chose ideal door.'
print idealDoorList[0].get_attribute('href')
return idealDoorList[0]
else:
print 'Chose not ideal door.'
return doorList[0]
def runMaze(maze):
knownRooms = []
exitFound = 0
moveCounter = 0 #Debugging
while exitFound == 0:
roomNumber = maze.find_elements_by_id('roomNumber')
while len(roomNumber) < 1:
roomNumber = maze.find_elements_by_id('roomNumber')
roomNumber = roomNumber[0].text
roomNumber = roomNumber.replace('Room ','')
if roomNumber not in knownRooms:
knownRooms.append(roomNumber)
messages = maze.find_elements_by_id('message')
if len(messages) > 0:
message = messages[0].text
else:
message = ''
if message == 'YOU WIN!':
exitFound = 1
print message
print "You escape the maze!"
else:
door = chooseDoor(maze,knownRooms)
moveCounter = moveCounter + 1
print moveCounter
door.click()
maze.close()
maze = loadMaze()
runMaze(maze)