-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloodfillQueue.py
151 lines (132 loc) · 3.82 KB
/
FloodfillQueue.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
#!/usr/bin/python
"""Template for your tron bot"""
import tron
import sys
import random
from collections import deque
def which_move(board):
#file = open('debug.log', "a")
#sys.stderr = file
if len(board.moves()) == 0:
return tron.SOUTH
if len(board.moves()) == 1:
#sys.stderr.write("quickchoice: " + str(board.moves()[0]) + "\n\n")
return board.moves()[0]
enemyReachable = []
for dir in board.moves(board.them()):
enemyReachable.append(board.rel(dir, board.them()))
spaceCount = {}
for dir in board.moves():
dest = board.rel(dir)
if dest in enemyReachable:
spaceCount[dir] = 1
else:
spaceCount[dir] = floodfill(board, dest, [])
#sys.stderr.write("spacecount: " + str(spaceCount) + "\n")
enemySpaceCount = {}
for dir in board.moves():
myPos = board.rel(dir)
enemySpaceCount[dir] = floodfill(board, board.them(), [myPos])
#sys.stderr.write("enemySpaceCount: " + str(enemySpaceCount) + "\n")
bestchoices = []
maxscore = None
for dir in spaceCount.keys():
score = spaceCount[dir] - enemySpaceCount[dir]
if score == maxscore:
bestchoices.append(dir)
elif maxscore == None or score > maxscore:
maxscore = score
bestchoices = [dir]
#sys.stderr.write("bestchoices: " + str(bestchoices) + "\n")
'''
if len(bestchoices) == 1:
return bestchoices[0]
bestchoice = bestchoices[0]
maxcount = 0
for dir in bestchoices:
count = 0
origin = board.me()
while board.passable(board.rel(dir, origin)):
count = count + 1
origin = board.rel(dir, origin)
if count > maxcount:
maxcount = count
bestchoice = dir
return bestchoice
'''
if len(bestchoices) > 1:
maxpathlength = -1
oldbestchoices = bestchoices
bestchoices = []
for dir in oldbestchoices:
pathlength = 0
node = board.me()
while board.passable(board.rel(dir, node)):
pathlength = pathlength + 1
node = board.rel(dir, node)
if pathlength == maxpathlength:
bestchoices.append(dir)
elif pathlength > maxpathlength:
maxpathlength = pathlength
bestchoices = [dir]
forward = findForward(board)
if forward != None and forward in bestchoices:
choice = forward
#sys.stderr.write("choice: " + str(choice) + "\n\n")
return choice
choice = random.choice(bestchoices)
#sys.stderr.write("choice: " + str(choice) + "\n\n")
return choice
def floodfill(board, origin, visited):
queue = deque()
queue.append(origin)
count = -1
'''
while len(queue) > 0:
node = queue.popleft()
count += 1
visited.append(node)
for dir in board.unvisitedMoves(node, visited):
other = board.rel(dir, node)
if other not in queue:
queue.append(board.rel(dir, node))
'''
while len(queue) > 0:
node = queue.popleft()
if node in visited:
continue
west = node
while board.passable(board.rel(tron.WEST, west)) and west not in visited:
west = board.rel(tron.WEST, west)
east = node
while board.passable(board.rel(tron.EAST, east)) and east not in visited:
east = board.rel(tron.EAST, east)
westToEast = west
while westToEast != board.rel(tron.EAST, east):
north = board.rel(tron.NORTH, westToEast)
if board.passable(north) and north not in visited:
queue.append(north)
south = board.rel(tron.SOUTH, westToEast)
if board.passable(south) and south not in visited:
queue.append(south)
count += 1
visited.append(westToEast)
westToEast = board.rel(tron.EAST, westToEast)
return count
def findForward(board):
forward = None
if len(board.moves()) == 3:
originDir = (set(tron.DIRECTIONS) - set(board.moves())).pop()
forward = None
if originDir == tron.NORTH:
forward = tron.SOUTH
elif originDir == tron.SOUTH:
forward = tron.NORTH
elif originDir == tron.EAST:
forward = tron.WEST
elif originDir == tron.WEST:
forward = tron.EAST
return forward
# you do not need to modify this part
for board in tron.Board.generate():
tron.move(which_move(board))