-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloodfillImpliedStack.py
90 lines (72 loc) · 2.14 KB
/
FloodfillImpliedStack.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
#!/usr/bin/python
"""Template for your tron bot"""
import tron
import random
def which_move(board):
file = open('debug.log', "a")
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, [])
file.write("spacecount: " + str(spaceCount) + "\n")
if not spaceCount:
# We lost :(
return tron.NORTH
currentEnemySpace = floodfill(board, board.them(), [])
file.write("currentEnemySpace: " + str(currentEnemySpace) + "\n")
enemySpaceCount = {}
for dir in board.moves():
myPos = board.rel(dir)
enemySpaceCount[dir] = floodfill(board, board.them(), [myPos])
file.write("enemySpaceCount: " + str(enemySpaceCount) + "\n")
bestchoices = []
maxscore = -1
for dir in spaceCount.keys():
changeEnemySpace = currentEnemySpace - enemySpaceCount[dir]
mySpace = spaceCount[dir]
pathlength = 0
origin = board.me()
while board.passable(board.rel(dir, origin)) and pathlength <= 5:
pathlength = pathlength + 1
origin = board.rel(dir, origin)
score = mySpace + changeEnemySpace + pathlength
if score == maxscore:
bestchoices.append(dir)
elif score > maxscore:
maxscore = score
bestchoices = [dir]
file.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
"""
choice = random.choice(bestchoices)
file.write("choice: " + str(choice) + "\n\n")
return choice
def floodfill(board, origin, visited):
visited.append(origin)
count = 1
for dir in board.unvisitedMoves(origin, visited):
count = count + floodfill(board, board.rel(dir, origin), visited)
return count
# you do not need to modify this part
for board in tron.Board.generate():
tron.move(which_move(board))