-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.py
119 lines (89 loc) · 2.54 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
117
118
119
import sys
'''
orients :
0- North
1- East
2- South
3- West
'''
def orientation(orient,turning):
if (turning== 'L'):
orient-=1
if (orient==-1):
orient=3
elif(turning== 'R'):
orient+=1
if (orient==4):
orient=0
elif(turning== 'B'):
if (orient==0):
orient=2
elif (orient==1):
orient=3
elif (orient==2):
orient=0
elif (orient==3):
orient=1
return(orient)
def updateCoordinates(x,y,orient):
if (orient==0):
y+=1
if (orient==1):
x+=1
if (orient==2):
y-=1
if (orient==3):
x-=1
return(x,y)
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():
response = command(args=["moveForward"], return_type=str)
if response == "crash":
#log(str(cells[y][x]))
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)
def log(string):
sys.stderr.write("{}\n".format(string))