-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo.py
61 lines (49 loc) · 1.49 KB
/
Demo.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
import math
from AI import BaseAI
from Interface import Driver
from Geometry import *
from Data import *
# Snake = namedtuple('Snake', 'id, nick, ang, head, body')
# Point = namedtuple('Point', 'x, y')
# Food = namedtuple('Food', 'id, p, size')
# Inputs = namedtuple('Inputs', 'left, right, acceleration')
d = Driver()
*hist, = range(20)
target = None
def demo(snake: Snake, snakes: [Snake], foods: [Food]) -> Inputs:
global target, hist
head = snake.head
ang = snake.ang
choice = lambda c: (lambda f: f.size * math.exp(-(dist(head, f.p)/100)) * math.exp(-abs(direction(head, f.p, ang)**3)))(c[1])
if hist[0]!=(0,0,0) and all(e==hist[0] for e in hist):
print("Cycle break !")
*hist, = range(20)
i,target = max(((i,f) for i,f in enumerate(foods) if f.id != target.id),
key = choice)
try:
d.exe('foods[%i].rad=10'%i)
except Exception:
pass
print("New target")
elif not target or not target.id in [f.id for f in foods if f]:
i, target = max(enumerate(foods),
key = choice)
try:
d.exe('foods[%i].rad=10'%i)
except Exception:
pass
print("New target")
direc = direction(head, target.p, ang)
if direc < - .55:
r = (1,0,0)
elif direc > .55:
r = (0,1,0)
else:
r = (0,0,0)
hist = hist[1:]+[r]
return r
while True:
ai = BaseAI(d.io, demo)
d.start()
ai.start()
ai.join()