def execute(self, board, spaceCount): timer = time.clock() path = self.longestPathFloodfill(board, spaceCount) tron.debug("Longestpath: " + str(len(path)) + "\n") dest = path[0] choice = [dir for dir in board.moves() if board.rel(dir) == dest][0] tron.log(len(self.cachedPaths)) self.cachedPaths = self.prepareCachedPaths(dest) tron.log(len(self.cachedPaths)) tron.log("Survival algorithm took: " + str(time.clock() - timer)) return choice
def execute(self, board, spaceCount): timer = time.clock() path = self.longestPathFloodfill(board, spaceCount) tron.debug("Longestpath: " + str(len(path)) + "\n") dest = path[0] choice = [dir for dir in board.moves() if board.rel(dir) == dest][0] ''' tron.log(len(self.cachedPaths)) self.cachedPaths = self.prepareCachedPaths(dest) tron.log(len(self.cachedPaths)) ''' tron.log("Survival algorithm took: " + str(time.clock() - timer)) return choice
def execute(self, board, spaceCount, deadCorners): self.timer = time.clock() spaceUpperBound = max(spaceCount.values()) - len(deadCorners) path = self.longestPathFloodfill(board) tron.debug("Longestpath: " + str(len(path)) + "\n") dest = path[0] choice = None for dir in board.moves(): if board.rel(dir) == dest: choice = dir tron.log(len(self.allPaths)) self.allPaths = self.prepareQueue(dest) tron.log(len(self.allPaths)) tron.log("Optimize space algorithm time: " + str(time.clock() - self.timer)) self.timer = 0 return choice
def which_move(board): start = time.clock() if len(board.moves()) == 0: return tron.SOUTH enemyMoves = [ board.rel(dir, board.them()) for dir in board.moves(board.them()) ] enemyReachable = False 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.debug("Spacecount: " + str(spaceCount) + "\n") tron.debug("EnemyReachable: " + str(enemyReachable) + "\n") #enemyReachable = False if not enemyReachable: return survivalMode(board, spaceCount) enemySpaceCount = dict() for dir in board.moves(): enemySpaceCount[dir] = tron.floodfill.floodfillScore( board, board.them(), [board.rel(dir)]) #tron.log("EnemySpaceCount: " + str(enemySpaceCount)) shortestPath = attack_algorithms.AStar().execute(board) minimaxSpaceCount = tron.minimax.execute(board, spaceCount, enemySpaceCount) tron.log("minimaxspacecount: " + str(minimaxSpaceCount)) #tron.log(len(shortestPath)) #tron.log("Shortest path: " + str(shortestPath)) if len(shortestPath) >= 6: return farAwayMode(board, shortestPath, minimaxSpaceCount) return closeCombatMode(board, minimaxSpaceCount, shortestPath) '''
def which_move(board): start = time.clock() if len(board.moves()) == 0: return tron.SOUTH enemyMoves = [board.rel(dir, board.them()) for dir in board.moves(board.them())] enemyReachable = False 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.debug("Spacecount: " + str(spaceCount) + "\n") tron.debug("EnemyReachable: " + str(enemyReachable) + "\n") #enemyReachable = False if not enemyReachable: return survivalMode(board, spaceCount) enemySpaceCount = dict() for dir in board.moves(): enemySpaceCount[dir] = tron.floodfill.floodfillScore(board, board.them(), [board.rel(dir)]) #tron.log("EnemySpaceCount: " + str(enemySpaceCount)) shortestPath = attack_algorithms.AStar().execute(board) minimaxSpaceCount = tron.minimax.execute(board, spaceCount, enemySpaceCount) tron.log("minimaxspacecount: " + str(minimaxSpaceCount)) #tron.log(len(shortestPath)) #tron.log("Shortest path: " + str(shortestPath)) if len(shortestPath) >= 6: return farAwayMode(board, shortestPath, minimaxSpaceCount) return closeCombatMode(board, minimaxSpaceCount, shortestPath) '''
def floodfill(board, origin, exclude=[]): start = time.clock() visited = [] queue = deque() queue.append(origin) while len(queue) > 0: node = queue.popleft() if node in visited: continue west = continuouslyMoveDirection(board, node, tron.WEST, exclude) east = continuouslyMoveDirection(board, node, tron.EAST, exclude) westToEast = west northInterrupted = True southInterrupted = True while westToEast != board.rel(tron.EAST, east): north = board.rel(tron.NORTH, westToEast) if not northInterrupted and (not board.passable(north) or north in exclude or north in visited): northInterrupted = True elif northInterrupted and board.passable( north) and north not in exclude and north not in visited: queue.append(north) northInterrupted = False south = board.rel(tron.SOUTH, westToEast) if not southInterrupted and (not board.passable(south) or south in exclude or south in visited): southInterrupted = True elif southInterrupted and board.passable( south) and south not in exclude and south not in visited: queue.append(south) southInterrupted = False visited.append(westToEast) westToEast = board.rel(tron.EAST, westToEast) tron.debug("FLOODFILL TOOK: " + str(time.clock() - start) + "\n") return visited
def floodfill(board, origin, exclude=[]): start = time.clock() visited=[] queue = deque() queue.append(origin) while len(queue) > 0: node = queue.popleft() if node in visited: continue west = continuouslyMoveDirection(board, node, tron.WEST, exclude) east = continuouslyMoveDirection(board, node, tron.EAST, exclude) westToEast = west northInterrupted = True southInterrupted = True while westToEast != board.rel(tron.EAST, east): north = board.rel(tron.NORTH, westToEast) if not northInterrupted and (not board.passable(north) or north in exclude or north in visited): northInterrupted = True elif northInterrupted and board.passable(north) and north not in exclude and north not in visited: queue.append(north) northInterrupted = False south = board.rel(tron.SOUTH, westToEast) if not southInterrupted and (not board.passable(south) or south in exclude or south in visited): southInterrupted = True elif southInterrupted and board.passable(south) and south not in exclude and south not in visited: queue.append(south) southInterrupted = False visited.append(westToEast) westToEast = board.rel(tron.EAST, westToEast) tron.debug("FLOODFILL TOOK: " + str(time.clock() - start) + "\n") return visited
def which_move(board): start = time.clock() if len(board.moves()) == 0: return tron.SOUTH #if len(board.moves()) == 1: # tron.debug("Quickchoice: " + str(board.moves()[0]) + "\n\n") # return board.moves()[0] enemyMoves = [] for dir in board.moves(board.them()): enemyMoves.append(board.rel(dir, board.them())) deadCorners = [] enemyReachable = False spaceCount = {} for dir in board.moves(): dest = board.rel(dir) if dest in enemyMoves: #spaceCount[dir] = 1 enemyReachable = True visited = floodfill(board, dest) enemyReachable = enemyReachable or len(filter(lambda node : node in enemyMoves, visited)) != 0 deadCorners.extend([node for node in visited if len(board.adjacentImpassable(node)) == 3 and node not in deadCorners]) spaceCount[dir] = len(visited) tron.debug("Spacecount: " + str(spaceCount) + "\n") tron.debug("EnemyReachable: " + str(enemyReachable) + "\n") #enemyReachable = False if not enemyReachable: choice = tron.optimizeSpaceAlgorithm.execute(board, spaceCount, deadCorners) tron.debug("Choice: " + str(choice) + "\n") tron.debug("Turn took: " + str(time.clock() - start) + "\n\n") return choice enemySpaceCount = {} for dir in board.moves(): myPos = board.rel(dir) enemySpaceCount[dir] = len(floodfill(board, board.them(), [myPos])) tron.debug("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] bestchoices = findLongestPathDirections(board.me(), bestchoices) tron.debug("Bestchoices: " + str(bestchoices) + "\n") choice = random.choice(bestchoices) tron.debug("Choice: " + str(choice) + "\n") tron.debug("Turn took: " + str(time.clock() - start) + "\n\n") return choice
def which_move(board): start = time.clock() if len(board.moves()) == 0: return tron.SOUTH #if len(board.moves()) == 1: # tron.debug("Quickchoice: " + str(board.moves()[0]) + "\n\n") # return board.moves()[0] enemyMoves = [] for dir in board.moves(board.them()): enemyMoves.append(board.rel(dir, board.them())) deadCorners = [] enemyReachable = False spaceCount = {} for dir in board.moves(): dest = board.rel(dir) if dest in enemyMoves: #spaceCount[dir] = 1 enemyReachable = True visited = floodfill(board, dest) enemyReachable = enemyReachable or len( filter(lambda node: node in enemyMoves, visited)) != 0 deadCorners.extend([ node for node in visited if len(board.adjacentImpassable(node)) == 3 and node not in deadCorners ]) spaceCount[dir] = len(visited) tron.debug("Spacecount: " + str(spaceCount) + "\n") tron.debug("EnemyReachable: " + str(enemyReachable) + "\n") #enemyReachable = False if not enemyReachable: choice = tron.optimizeSpaceAlgorithm.execute(board, spaceCount, deadCorners) tron.debug("Choice: " + str(choice) + "\n") tron.debug("Turn took: " + str(time.clock() - start) + "\n\n") return choice enemySpaceCount = {} for dir in board.moves(): myPos = board.rel(dir) enemySpaceCount[dir] = len(floodfill(board, board.them(), [myPos])) tron.debug("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] bestchoices = findLongestPathDirections(board.me(), bestchoices) tron.debug("Bestchoices: " + str(bestchoices) + "\n") choice = random.choice(bestchoices) tron.debug("Choice: " + str(choice) + "\n") tron.debug("Turn took: " + str(time.clock() - start) + "\n\n") return choice