-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.py
141 lines (123 loc) · 2.85 KB
/
backup.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
from snakegame.common import *
from random import choice
def conv((x, y)):
return (W * y) + x
def find(n):
global PAR
if n == PAR[n]:
return n
else:
PAR[n] = find(PAR[n])
return PAR[n]
cfind = lambda x: find(conv(x))
def join(a, b):
global PAR, RANK, SIZE
pa = cfind(a)
pb = cfind(b)
if pa == pb:
return
if RANK[pa] < RANK[pb]:
PAR[pa] = pb
SIZE[pb] += SIZE[pa]
elif RANK[pb] < RANK[pa]:
PAR[pb] = pa
SIZE[pa] += SIZE[pb]
else:
PAR[pb] = pa
RANK[pa] += 1
SIZE[pa] += SIZE[pb]
def axisdis(a, b, tot):
lo, hi = sorted([a, b])
return min(hi - lo, tot - hi + lo)
def dis((ax, ay), (bx, by)):
return axisdis(ax, bx, W) + axisdis(ay, by, H)
def unionfind_bot(board, pos):
(x, y) = pos
me = board[y][x]
global H, W, PAR, RANK, SIZE
H = len(board)
W = len(board[0])
enemies = apple = False
mylen = 0
for y in xrange(H):
for x in xrange(W):
if board[y][x] not in "*.":
if board[y][x].upper() == me:
mylen += 1
else:
enemies = True
RANK = [0] * (H * W)
SIZE= [1] * (H * W)
PAR = []
for i in xrange(H * W):
PAR.append(i)
seen = []
for y in xrange(H):
seen.append([False] * W)
seen[y][x] = True
q = [pos]
ptr = 0
while ptr < len(q):
x, y = q[ptr]
ptr += 1
for dir in directions.values():
nx = (x + dir[0]) % W
ny = (y + dir[1]) % H
if board[ny][nx] in ".*" and not seen[ny][nx]:
seen[ny][nx] = True
q.append((nx, ny))
if not apple and board[y][x] == "*":
apple = (x, y)
if pos != (x, y):
join((nx, ny), (x, y))
elif seen[ny][nx] and pos != (x, y):
join((nx, ny), (x, y))
x, y = pos
hi = 0
for dir in directions:
nx = (x + directions[dir][0]) % W
ny = (y + directions[dir][1]) % H
n = cfind((nx, ny))
hi = max(hi, SIZE[n])
poss = []
good = []
for dir in "DRUL":
nx = (x + directions[dir][0]) % W
ny = (y + directions[dir][1]) % H
if SIZE[cfind((nx, ny))] == hi:
poss.append((dir, board[ny][nx]))
for (dir, loc) in poss:
if mylen < H and not enemies:
#if we can't wrap around yet but there aren't enemies about
if apple:
nx = (x + directions[dir][0]) % W
ny = (y + directions[dir][1]) % H
if dis((nx, ny), apple) < dis((x, y), apple):
return dir
elif enemies:
if loc == ".":
good.append(dir)
else:
if hi == 1 and mylen == H * W - 1:
print "matt_bot covered the board :D"
return dir
if good:
return choice(good)
elif enemies and mylen == 1:
for dir in "DRUL":
nx = (x + directions[dir][0]) % W
ny = (y + directions[dir][1]) % H
if SIZE[cfind((nx, ny))] >= 2:
if board[ny][nx] == ".":
return dir
if hi == 0:
print "Going down"
return choice(poss)[0]
if __name__ == '__main__':
from snakegame.engines.pyglet import PygletEngine
import old_matt
engine = PygletEngine(20, 20, 15)
engine.add_bot(unionfind_bot)
for i in range(25):
engine.add_bot(old_matt.safe_bot)
engine.run()