forked from BattlesnakeOfficial/starter-snake-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
86 lines (74 loc) · 2.04 KB
/
helper.py
File metadata and controls
86 lines (74 loc) · 2.04 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import copy
def findClosestFood(data):
food = data['board']['food']
head = data['you']['head']
distance = 100
closest = food[0]
for i in food:
temp = (abs(i['x'] - head['x']) + abs(i['y'] - head['y']))
if(temp <= distance):
distance = temp
closest = i
return closest
def impossibleMoves(data):
surrounding = []
cantMove = []
for i in range(4):
temp = copy.copy(data['you']['head'])
if (i == 1):
temp['y'] -= 1
if (temp['y'] < 0):
cantMove.append("up")
elif (i == 2):
temp['x'] += 1
if (temp['x'] > 10):
cantMove.append("right")
elif (i == 3):
temp['y'] += 1
if (temp['y'] > 10):
cantMove.append("down")
else:
temp['x'] -= 1
if (temp['x'] < 0):
cantMove.append("left")
surrounding.append(temp)
for snake in data['board']['snakes']:
for i in range(4):
if (surrounding[i] in snake['body']):
if (i == 1):
if ("up" not in cantMove):
cantMove.append("up")
elif (i == 2):
if ("right" not in cantMove):
cantMove.append("right")
elif (i == 3):
if ("down" not in cantMove):
cantMove.append("down")
else:
if ("left" not in cantMove):
cantMove.append("left")
return cantMove
def nextMove(data, foodX, foodY):
headX = data['you']['head']['x']
headY = data['you']['head']['y']
if (headX <= foodX and headY <= foodY):
if (headX == foodX):
move = "down"
else:
move = "right"
elif (headX <= foodX and headY >= foodY):
if (headX == foodX):
move = "up"
else:
move = "right"
elif (headX >= foodX and headY >= foodY):
if (headX == foodX):
move = "up"
else:
move = "left"
else:
if (headX == foodX):
move = "down"
else:
move = "left"
return move