-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.py
75 lines (55 loc) · 1.87 KB
/
API.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
import sys
class MouseCrashedError(Exception):
pass
def command(args, return_type=None):
line = " ".join([str(x) for x in args]) + "\n"
sys.stdout.write(line)
sys.stdout.flush()
if return_type:
response = sys.stdin.readline().strip()
if return_type == bool:
return response == "true"
return return_type(response)
def mazeWidth():
return command(args=["mazeWidth"], return_type=int)
def mazeHeight():
return command(args=["mazeHeight"], return_type=int)
def wallFront():
return command(args=["wallFront"], return_type=bool)
def wallRight():
return command(args=["wallRight"], return_type=bool)
def wallLeft():
return command(args=["wallLeft"], return_type=bool)
def moveForward(distance=None):
args = ["moveForward"]
# Don't append distance argument unless explicitly specified, for
# backwards compatibility with older versions of the simulator
if distance is not None:
args.append(distance)
response = command(args=args, return_type=str)
if response == "crash":
raise MouseCrashedError()
def turnRight():
command(args=["turnRight"], return_type=str)
def turnLeft():
command(args=["turnLeft"], return_type=str)
def setWall(x, y, direction):
command(args=["setWall", x, y, direction])
def clearWall(x, y, direction):
command(args=["clearWall", x, y, direction])
def setColor(x, y, color):
command(args=["setColor", x, y, color])
def clearColor(x, y):
command(args=["clearColor", x, y])
def clearAllColor():
command(args=["clearAllColor"])
def setText(x, y, text):
command(args=["setText", x, y, text])
def clearText(x, y):
command(args=["clearText", x, y])
def clearAllText():
command(args=["clearAllText"])
def wasReset():
return command(args=["wasReset"], return_type=bool)
def ackReset():
command(args=["ackReset"], return_type=str)