-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAPI.py
116 lines (85 loc) · 3.02 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
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
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 checkWall(wallCommand, half_steps_away=None):
args = [wallCommand]
if half_steps_away is not None:
args.append(half_steps_away)
return command(args, return_type=bool)
def wallFront(half_steps_away=None):
return checkWall("wallFront", half_steps_away)
def wallBack(half_steps_away=None):
return checkWall("wallBack", half_steps_away)
def wallLeft(half_steps_away=None):
return checkWall("wallLeft", half_steps_away)
def wallRight(half_steps_away=None):
return checkWall("wallRight", half_steps_away)
def wallFrontLeft(half_steps_away=None):
return checkWall("wallFrontLeft", half_steps_away)
def wallFrontRight(half_steps_away=None):
return checkWall("wallFrontRight", half_steps_away)
def wallBackLeft(half_steps_away=None):
return checkWall("wallBackLeft", half_steps_away)
def wallBackRight(half_steps_away=None):
return checkWall("wallBackRight", half_steps_away)
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 moveForwardHalf(num_half_steps=None):
args = ["moveForwardHalf"]
if num_half_steps is not None:
args.append(num_half_steps)
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 turnRight90():
turnRight()
def turnLeft90():
turnLeft()
def turnRight45():
command(args=["turnRight45"], return_type=str)
def turnLeft45():
command(args=["turnLeft45"], 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)