forked from BattlesnakeOfficial/starter-snake-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
69 lines (60 loc) · 1.57 KB
/
util.py
File metadata and controls
69 lines (60 loc) · 1.57 KB
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
"""
Grab bag of utilities
"""
# Merge two dictionaries recursively, copied from Stackoverflow
def merge_dict(d1, d2):
for k in d2:
if k in d1 and isinstance(d1[k], dict) and isinstance(d2[k], dict):
merge_dict(d1[k], d2[k])
else:
d1[k] = d2[k]
# Convert all known coords to tuples for ease of use
def tuplify(move_data):
new_data = move_data
board = move_data["board"]
new_snakes = []
for snake in board["snakes"]:
new_snakes.append(tuplify_snake(snake))
board["snakes"] = new_snakes
board["food"] = to_tuples(board["food"])
board["hazards"] = to_tuples(board["hazards"])
new_data["board"] = board
new_data["you"] = tuplify_snake(move_data["you"])
return new_data
# Convert coords in snake object to tuples
def tuplify_snake(snake):
new_snake = snake
new_snake["body"] = to_tuples(snake["body"])
new_snake["head"] = to_tuple(snake["head"])
return new_snake
# Convert list of coords to list of tuples
def to_tuples(coords):
tuples = []
for coord in coords:
tuples.append(to_tuple(coord))
return tuples
# Convert one coord to one tuple
def to_tuple(coord):
return (coord["x"], coord["y"])
# Default data for testing
def default_data():
return {
"turn": 3,
"board": {
"width": 3,
"height": 3,
"snakes": [],
"food": [],
"hazards": []
},
"you": {
"id": "me",
"health": 100,
"body": [(0, 0)]
}
}
# Return Manhattan distance between coords
def manhattan(coord1, coord2):
(x1, y1) = coord1
(x2, y2) = coord2
return abs(x1 - x2) + abs(y1 - y2)