-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyTronBot.py
202 lines (172 loc) · 6.25 KB
/
MyTronBot.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/python
"""Template for your tron bot"""
import tron, time
def which_move(board):
if not board.moves():
return tron.SOUTH
if board.width >= 40 and board.height >= 40:
return wallhugMode(board)
if board.width >= 20 and board.height >= 20:
return floodfillMode(board)
enemyMoves = [board.rel(dir, board.them()) for dir in board.moves(board.them())]
shortestPath = tron.aStar.execute(board)
tron.log("ShortestPath: " + str(shortestPath))
enemyReachable = shortestPath is not None
tron.log("EnemyReachable: " + str(enemyReachable))
#enemyReachable = False
if not enemyReachable:
return survivalMode(board)
shortestPath = shortestPath[1:]
#if shortestPath[-1] != board.them() or time.clock() - board.startTime > 0.2:
# return shortestPathMode(board, shortestPath)
#enemySpaceCount = dict()
#for dir in board.moves():
# enemySpaceCount[dir] = tron.floodfill.floodfillScore(board, board.them(), [board.rel(dir)])
#tron.log("EnemySpaceCount: " + str(enemySpaceCount))
minimaxSpaceCount = tron.minimax.execute(board)
tron.log("minimaxspacecount: " + str(minimaxSpaceCount))
#if not minimaxSpaceCount:
# return shortestPathMode(board, shortestPath)
#if len(shortestPath) >= 6:
# return farAwayMode(board, shortestPath, minimaxSpaceCount)
return minimaxMode(board, minimaxSpaceCount, shortestPath)
def wallhugMode(board):
order = (tron.WEST, tron.EAST, tron.NORTH, tron.SOUTH)
decision = board.moves()[0]
for dir in order:
dest = board.rel(dir)
if not board.passable(dest):
continue
adj = board.adjacent(dest)
if any(board[pos] == tron.WALL for pos in adj):
decision = dir
break
return decision
def minimaxMode(board, minimaxSpaceCount, shortestPath):
maxScore = max(minimaxSpaceCount.values())
bestDirs = [dir for dir in board.moves() if minimaxSpaceCount[dir] == maxScore]
if len(bestDirs) == 1:
tron.log("Minimax Choice: " + str(bestDirs[0]))
return bestDirs[0]
for dir in bestDirs:
if board.rel(dir) == shortestPath[0]:
tron.log("Minimax/Shortestpath Choice: " + str(dir))
return dir
newBestDirs = []
minHeuristic = None
for dir in bestDirs:
heuristic = board.distance(board.rel(dir), board.them()) - len(board.adjacentImpassable(board.rel(dir))) * 2
if minHeuristic is None or heuristic < minHeuristic:
newBestDirs = [dir]
minHeuristic = heuristic
elif heuristic == minHeuristic:
newBestDirs.append(dir)
'''
if len(newBestDirs) == 1:
tron.log("Minimax/Heuristic 1 choice: " + str(newBestDirs[0]))
return newBestDirs[0]
for dir in newBestDirs:
if board.rel(dir) == shortestPath[0]:
tron.log("Minimax/Heuristic/Shortestpath Choice: " + str(dir))
return dir
'''
choice = newBestDirs[0]
tron.log("Minimax/Heuristic 2 choice: " + str(choice))
return choice
'''
def shortestPathMode(board, shortestPath):
choice = tron.SOUTH
for dir in board.moves():
if board.rel(dir) == shortestPath[0]:
choice = dir
tron.log("Choice: " + str(choice))
return choice
'''
'''
def farAwayMode(board, shortestPath, minimaxSpaceCount):
choice = None
bestChoices = []
maxScore = max(minimaxSpaceCount.values())
minScore = min(minimaxSpaceCount.values())
for dir in board.moves():
if board.rel(dir) == shortestPath[0]:
choice = dir
if ((minScore <= -20 and maxScore >= -8) or (minScore <= -10 and maxScore >= 0) or maxScore >= 8) and minimaxSpaceCount[dir] == maxScore:
bestChoices.append(dir)
if bestChoices and choice not in bestChoices:
choice = None
if len(bestChoices) == 1:
choice = bestChoices[0]
if choice == None:
shortestDist = None
for dir in bestChoices:
distance = board.distance(board.rel(dir), board.them())
if shortestDist == None or distance < shortestDist:
choice = dir
shortestDist = distance
tron.log("Choice: " + str(choice))
return choice
'''
def floodfillMode(board):
spaceCount = dict()
for dir in board.moves():
dest = board.rel(dir)
floodfilled = tron.floodfill.execute(board, dest)
#enemyReachable = enemyReachable or dest in enemyMoves or len(filter(lambda node : node in enemyMoves, floodfilled)) > 0
deadCorners = [node for node in floodfilled if len(board.adjacentImpassable(node)) == 3]
spaceCount[dir] = len(floodfilled) - len(deadCorners) + 1
tron.log("Spacecount: " + str(spaceCount))
enemySpaceCount = dict()
for dir in board.moves():
myPos = board.rel(dir)
floodfilled = tron.floodfill.execute(board, board.them(), [myPos])
deadCorners = [node for node in floodfilled if len(board.adjacentImpassable(node)) == 3]
enemySpaceCount[dir] = len(floodfilled) - len(deadCorners) + 1
tron.log("EnemySpacecount: " + str(enemySpaceCount))
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]
bestchoices = findLongestPathDirections(board.me(), bestchoices)
tron.log("Bestchoices: " + str(bestchoices))
choice = bestchoices[0]
tron.log("Choice: " + str(choice))
return choice
def survivalMode(board):
spaceCount = dict()
for dir in board.moves():
dest = board.rel(dir)
floodfilled = tron.floodfill.execute(board, dest)
#enemyReachable = enemyReachable or dest in enemyMoves or len(filter(lambda node : node in enemyMoves, floodfilled)) > 0
deadCorners = [node for node in floodfilled if len(board.adjacentImpassable(node)) == 3]
spaceCount[dir] = len(floodfilled) - len(deadCorners) + 1
tron.log("Spacecount: " + str(spaceCount))
choice = tron.optimizeSpaceAlgorithm.execute(board, spaceCount)
tron.log("Choice: " + str(choice))
return choice
def findLongestPathDirections(start, directions):
longestDirections = directions
if len(directions) > 1:
maxpathlength = -1
longestDirections = []
for dir in directions:
pathlength = 0
node = start
while board.passable(board.rel(dir, node)):
pathlength += 1
node = board.rel(dir, node)
if pathlength == maxpathlength:
longestDirections.append(dir)
elif pathlength > maxpathlength:
maxpathlength = pathlength
longestDirections = [dir]
return longestDirections
# you do not need to modify this part
for board in tron.Board.generate():
tron.move(which_move(board))
tron.log("Turn took: " + str(float(time.clock() - board.startTime)))