-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontrol_hub.py
256 lines (238 loc) · 7.71 KB
/
control_hub.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
"""Control Hub that makes changes to the AryaBota environment - grid and robot"""
# Writes outcomes to a result file
import json
import yaml
from services.grid import Grid
from services.arya_bota import AryaBota
from services.utils import check_answer
# Opening config to read grid attributes
with open('config.yaml') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
BOT = AryaBota.get_instance()
GRID = Grid.get_instance()
results_file_path = config["app"]["results"]
def make_response(response_type, response):
"""Create a response object"""
if response_type == "value":
return {
"value": response
}
if response_type == "state":
return {
"stateChanges": [response]
}
if response_type == "error":
return {
"error_message": response
}
if response_type == "submit":
return response
def get_my_row():
"""return current row"""
return BOT.my_row()
def get_my_column():
"""return current column"""
return BOT.my_column()
def move(steps):
"""move ahead on grid"""
(success, message) = BOT.move(steps)
with open(results_file_path) as results_file:
results = json.loads(results_file.read())
if success:
results.append(make_response("state", BOT.get_state()))
else:
results.append(make_response("error", message))
# TODO: add specific error raising
raise Exception("Hitting obstacles or falling off the grid ;_;")
with open(results_file_path, "w") as results_file:
results_file.write(json.dumps(results))
def turn(direction = "left"):
"""turn left on grid"""
if direction == "left":
BOT.turn_left()
elif direction == "right":
BOT.turn_right()
with open(results_file_path) as results_file:
results = json.loads(results_file.read())
results.append(make_response("state", BOT.get_state()))
with open(results_file_path, "w") as results_file:
results_file.write(json.dumps(results))
def set_pen(status = "up"):
"""change pen status"""
BOT.set_pen(status)
def get_number_of_coins(row = None, column = None):
"""return number of coins at current position"""
if row is None:
row = BOT.my_row()
if column is None:
column = BOT.my_column()
return GRID.get_number_of_coins(row,column)
def obstacle_ahead(row = None, column = None):
"""return if obstacle ahead"""
if row is None:
row = BOT.my_row()
if column is None:
column = BOT.my_column()
state = GRID.get_state()
direction = BOT.get_dir()
if direction == "down":
if row+1 <= GRID.rows:
if({'position': {'row': row+1, 'column': column}} in state['obstacles']):
return 1
else:
return 1
elif direction == "up":
if row-1 > 0:
if({'position': {'row': row-1, 'column': column}} in state['obstacles']):
return 1
else:
return 1
elif direction == "right":
if column+1 <= GRID.columns:
if({'position': {'row': row, 'column': column+1}} in state['obstacles']):
return 1
else:
return 1
elif direction == "left":
if column-1 > 0:
if({'position': {'row': row, 'column': column-1}} in state['obstacles']):
return 1
else:
return 1
return 0
def obstacle_behind(row = None, column = None):
"""return if obstacle behind"""
if row is None:
row = BOT.my_row()
if column is None:
column = BOT.my_column()
state = GRID.get_state()
direction = BOT.get_dir()
if direction == "up":
if row+1 <= GRID.rows:
if({'position': {'row': row+1, 'column': column}} in state['obstacles']):
return 1
else:
return 1
elif direction == "down":
if row-1 > 0:
if({'position': {'row': row-1, 'column': column}} in state['obstacles']):
return 1
else:
return 1
elif direction == "left":
if column+1 <= GRID.columns:
if({'position': {'row': row, 'column': column+1}} in state['obstacles']):
return 1
else:
return 1
elif direction == "right":
if column-1 > 0:
if({'position': {'row': row, 'column': column-1}} in state['obstacles']):
return 1
else:
return 1
return 0
def obstacle_left(row = None, column = None):
"""return if obstacle to the left"""
if row is None:
row = BOT.my_row()
if column is None:
column = BOT.my_column()
state = GRID.get_state()
direction = BOT.get_dir()
if direction == "left":
if row+1 <= GRID.rows:
if({'position': {'row': row+1, 'column': column}} in state['obstacles']):
return 1
else:
return 1
elif direction == "right":
if row-1 > 0:
if({'position': {'row': row-1, 'column': column}} in state['obstacles']):
return 1
else:
return 1
elif direction == "down":
if column+1 <= GRID.columns:
if({'position': {'row': row, 'column': column+1}} in state['obstacles']):
return 1
else:
return 1
elif direction == "up":
if column-1 > 0:
if({'position': {'row': row, 'column': column-1}} in state['obstacles']):
return 1
else:
return 1
return 0
def obstacle_right(row = None, column = None):
"""return if obstacle to the right"""
if row is None:
row = BOT.my_row()
if column is None:
column = BOT.my_column()
state = GRID.get_state()
direction = BOT.get_dir()
if direction == "right":
if row+1 <= GRID.rows:
if({'position': {'row': row+1, 'column': column}} in state['obstacles']):
return 1
else:
return 1
elif direction == "left":
if row-1 > 0:
if({'position': {'row': row-1, 'column': column}} in state['obstacles']):
return 1
else:
return 1
elif direction == "up":
if column+1 <= GRID.columns:
if({'position': {'row': row, 'column': column+1}} in state['obstacles']):
return 1
else:
return 1
elif direction == "down":
if column-1 > 0:
if({'position': {'row': row, 'column': column-1}} in state['obstacles']):
return 1
else:
return 1
return 0
def home():
state = GRID.get_state()
row = BOT.my_row()
column = BOT.my_column()
pos = {
"position": {
"row": row,
"column": column
}
}
if pos in state["homes"]:
print("At home, yes")
return True
return False
def print_value(expr):
"""print value"""
with open(results_file_path) as results_file:
results = json.loads(results_file.read())
response = expr
results.append(make_response("value", response))
with open(results_file_path, "w") as results_file:
results_file.write(json.dumps(results))
def submit(value = None):
"""submit answer"""
with open(results_file_path) as results_file:
results = json.loads(results_file.read())
if value is not None:
response = check_answer(value)
else:
current_state = {
"arya_bota": BOT.get_state_for_answer(),
"grid": GRID.get_state_for_answer()
}
response = check_answer(current_state)
results.append(make_response("submit", response))
with open(results_file_path, "w") as results_file:
results_file.write(json.dumps(results))