def chooseAction(self, gameState: GameState) -> str: food = self.getOpponentFood(gameState).asList() myPos = gameState.getAgentPosition(self.index) def got_it(): if self.target is None: return False return myPos[0] == self.target.pos.x and myPos[1] == self.target.pos.y if self.target is None or got_it(): targets = [Target(Position(x, y), self.getMazeDistance( (x, y), myPos)) for x, y in food] if got_it(): self.target = self.go_home( Position(myPos[0], myPos[1]), gameState) else: self.target: Target = min( targets, key=lambda target: target.dist) self.path = self.convertPath(self.getPath( myPos, (self.target.pos.x, self.target.pos.y), gameState.data.layout)) if len(self.path) <= 0: return "Stop" # sys.exit() if gameState.isOnRedTeam(self.index): print("path: ", self.path) move = self.path[0] self.path = self.path[1:] return move
def go_home(self, pos: Position, gameState: GameState): w = gameState.getWalls().width // 2 h = gameState.getWalls().height - 2 return Target(Position(1, 1), 0) if gameState.isOnRedTeam(self.index) else Target(Position(w - 1, 1), 0) x = w if gameState.isOnRedTeam(self.index) else w + 1 possible_dst = [Target(Position(x, y), self.getMazeDistance( (x, y), (pos.x, pos.y))) for y in range(h-1)] return min(possible_dst, key=lambda x: x[1])
def chooseAction(self, gameState: GameState) -> str: """ Picks among legal actions randomly. """ actions = gameState.getLegalActions(self.index) return random.choice(actions)
def getMostProbableManhattanDistance(yourPosition: Tuple[int, int], ennemyIndex: int, gamestate: GameState) -> List[int]: agentDistance = gamestate.getAgentDistances()[ennemyIndex] proba = 0.0 distances = [] bounce = 0 while proba < 0.75: currentDistance = agentDistance + bounce proba += gamestate.getDistanceProb(currentDistance, agentDistance) if (currentDistance not in distances): distances.append(currentDistance) currentDistance = agentDistance - bounce proba += gamestate.getDistanceProb(currentDistance, agentDistance) if (currentDistance not in distances): distances.append(currentDistance) bounce += 1 return distances
def chooseAction(self, gameState: GameState) -> str: myPos = gameState.getAgentPosition(self.index) if self.target is None or (myPos[0] == self.target.pos.x and myPos[1] == self.target.pos.y): pos = self.getClosestOpenBorderByEnemyAveragePosition(gameState) self.target = Target(pos, 0) self.path = self.convertPath(self.getPath( myPos, (self.target.pos.x, self.target.pos.y), gameState.data.layout)) if len(self.path) <= 0: return "Stop" if gameState.isOnRedTeam(self.index): print("path: ", self.path) move = self.path[0] self.path = self.path[1:] return move
def registerInitialState(self, gameState: GameState): self.gridWall = gameState.getWalls() self.initialPosition = gameState.getAgentPosition(self.index) self.currPosition = self.initialPosition self.home = findHome(self.gridWall, self.initialPosition) if (gameState.getAgentPosition(self.index)[0] > (self.gridWall.width - 1) // 2): self.mapMiddlePoint = (round( (self.gridWall.width - 1) * 0.65), self.gridWall.height // 2) else: self.mapMiddlePoint = (round( (self.gridWall.width - 1) * 0.35), self.gridWall.height // 2) self.goingUp = True self.firstPatrolDone = True self.topChokePoint = self.mapMiddlePoint self.bottomChokePoint = self.mapMiddlePoint CaptureAgent.registerInitialState(self, gameState)
def registerInitialState(self, gameState: GameState): """ This method handles the initial setup of the agent to populate useful fields (such as what team we're on). A distanceCalculator instance caches the maze distances between each pair of positions, so your agents can use: self.distancer.getDistance(p1, p2) IMPORTANT: This method may run for at most 5 seconds. """ ''' Make sure you do not delete the following line. If you would like to use Manhattan distances instead of maze distances in order to save on initialization time, please take a look at CaptureAgent.registerInitialState in captureAgents.py. ''' CaptureAgent.registerInitialState(self, gameState) ''' Your initialization code goes here, if you need any. ''' self.minFoodxy = (0, 0) self.gridWall = gameState.getWalls() self.home = findHome(self.gridWall, gameState.getAgentPosition(self.index)) if (self.index in gameState.getRedTeamIndices()): # left side self.mapMiddlePoint = (self.gridWall.width // 2 - 2, self.gridWall.height // 2) self.foodInMouth = 0 else: # right side self.mapMiddlePoint = (self.gridWall.width // 2 + 2, self.gridWall.height // 2) self.foodInMouth = 0
def getDirectionAndDistance(fromPoint: Tuple[int, int], toPoint: Tuple[int, int], gamestate: GameState) -> Tuple[int, str]: grid = gamestate.getWalls() myDict = {(toPoint[0], toPoint[1]): 0} toCheck = [(toPoint[0], toPoint[1])] distance = 0 while fromPoint not in myDict: AddToQueue(toCheck[distance], toCheck, myDict, grid) distance += 1 isInList(myDict, fromPoint) return (distance, findDirection(myDict, fromPoint))
def chooseAction(self, gameState: GameState) -> str: ownIndex = self.index ownPosition = gameState.getAgentPosition(ownIndex) if self.firstPatrolDone: possibleChokePoints = [] grid = gameState.getWalls() for i in range(len(grid[0]) - 1): if not gameState.hasWall(self.mapMiddlePoint[0], i): possibleChokePoints.append((self.mapMiddlePoint[0], i)) self.topChokePoint = possibleChokePoints[0] self.bottomChokePoint = possibleChokePoints[ len(possibleChokePoints) - 1] direction = getDirectionAndDistance(ownPosition, self.topChokePoint, gameState)[1] self.firstPatrolDone = False else: if self.goingUp: direction = getDirectionAndDistance(ownPosition, self.topChokePoint, gameState)[1] distance = getDirectionAndDistance(ownPosition, self.topChokePoint, gameState)[0] if distance == 0: self.goingUp = False else: direction = getDirectionAndDistance(ownPosition, self.bottomChokePoint, gameState)[1] distance = getDirectionAndDistance(ownPosition, self.bottomChokePoint, gameState)[0] if distance == 0: self.goingUp = True return direction
def getClosestOpenBorderByEnemyAveragePosition(self, gameState: GameState): enemiesAveragePosition = [] for enemyIndex in self.enemiesIndices: position = gameState.getAgentPosition(enemyIndex) enemiesAveragePosition.append(Position(position[0], position[1])) closestOpenBorderToClosestEnemy = self.openBorder[0] smallestDistance = 999999 for enemyPosition in enemiesAveragePosition: for dest in self.openBorder: distance = self.distancer.getDistance((enemyPosition.x, enemyPosition.y), (dest.x, dest.y)) if (distance < smallestDistance): closestOpenBorderToClosestEnemy = dest smallestDistance = distance return closestOpenBorderToClosestEnemy
def chooseAction(self, gameState: GameState) -> str: capMultiplier = 3 if self.red: capsules = gameState.getRedCapsules() else: capsules = gameState.getBlueCapsules() foodList = list(self.getFood(gameState)) actions = gameState.getLegalActions(self.index) meanX = 0 meanY = 0 for i in range(len(foodList)): meanX = meanX + foodList[i][0] meanY = meanY + foodList[i][1] for cap in capsules: meanX = capMultiplier * meanX + cap[0] meanY = capMultiplier * meanY + cap[1] meanX = int(meanX / len(foodList)) meanY = int(meanY / len(foodList)) myState = gameState.getAgentState(self.index) myPos = myState.getPosition() curDistance = abs(myPos[0] - meanX) + abs(myPos[1] - meanY) '''if meanX > myPos[0]: if actions.__contains__('East'): return 'East' if meanX <= myPos[0] if actions.__contains__('West'): return 'West' if meanY > myPos[1]: if actions.__contains__('East'): return 'East' if meanY <= myPos[1] if actions.__contains__('West'): return 'West' ''' for action in actions: gameState.generateSuccessor(self.index, action) newPos = myState.getPosition() dist = abs(newPos[0] - meanX) + abs(newPos[1] - meanY) if dist < curDistance: return action return random.choice(actions)
def chooseAction(self, gameState: GameState) -> str: pathFinder = Pathfinder() food = self.getOpponentFood(gameState).asList() myPos = gameState.getAgentPosition(self.index) if self.target is None or (myPos[0] == self.target.x and myPos[1] == self.target.y): targets = [Target(Position(x, y), self.getMazeDistance( (x, y), myPos)) for x, y in food] target: Target = min(targets, key=lambda target: target.dist) print(myPos, target) path = pathFinder.uniformCostSearch( myPos, target, gameState, self.index) if len(path) <= 0: sys.exit() print("path: ", path) return path[0]
def findClosestFoodDirection(self, grid, gameState: GameState) -> str: minFood = -1 minFoodxy = (0, 0) ownPosition = gameState.getAgentPosition(self.index) for i in range(grid.width): for j in range(grid.height): if grid[i][j]: if minFood == -1: minFood = getDirectionAndDistance( ownPosition, (i, j), gameState) minFoodxy = (i, j) elif minFood[0] > getDirectionAndDistance( ownPosition, (i, j), gameState)[0]: minFood = getDirectionAndDistance( ownPosition, (i, j), gameState) minFoodxy = (i, j) self.minFoodxy = minFoodxy if type(minFood) is int: return Directions.NORTH return minFood[1]
def chooseAction(self, gameState: GameState) -> str: """ Picks among legal actions randomly. """ ownIndex = self.index ownPosition = gameState.getAgentPosition(ownIndex) if (ownIndex in gameState.getBlueTeamIndices()): if (self.minFoodxy == gameState.getAgentPosition(self.index)): self.foodInMouth += 1 if self.foodInMouth < 8 and self.findNbFoodLeft( gameState.getRedFood(), gameState) > 0: direction = self.findClosestFoodDirection( gameState.getRedFood(), gameState) else: direction = getDirectionAndDistance(ownPosition, self.home, gameState)[1] if ownPosition == self.home: self.foodInMouth = 0 else: if (self.minFoodxy == gameState.getAgentPosition(self.index)): self.foodInMouth += 1 if self.foodInMouth < 8 and self.findNbFoodLeft( gameState.getRedFood(), gameState) > 0: direction = self.findClosestFoodDirection( gameState.getBlueFood(), gameState) else: direction = getDirectionAndDistance(ownPosition, self.home, gameState)[1] if ownPosition == self.home: self.foodInMouth = 0 return direction
def registerInitialState(self, gameState: GameState): walls = gameState.getWalls() maxX, maxY = walls.asList()[-1] limitRegion = math.ceil(maxX/2) + 1 if gameState.isOnRedTeam(self.index): limitRegion -= 3 for y in range(0, maxY): if not gameState.hasWall(limitRegion, y): self.openBorder.append(Position(limitRegion, y)) self.enemiesIndices = gameState.getRedTeamIndices() if gameState.isOnRedTeam(self.index): self.enemiesIndices = gameState.getBlueTeamIndices() # for pos in self.openBorder: # print(pos) # print("\n") CaptureAgent.registerInitialState(self, gameState)
def uniformCostSearch(self, intial_pos: tuple, dest: tuple, gameState: GameState, agentIndex: int): states = util.PriorityQueue() visited = set() states.push((intial_pos, [], 0), 0) while states: pos, path, cost = states.pop() if pos == dest: return path if pos not in visited: visited.add(pos) for next_action in ["North", "South", "East", "West"]: newPos = self.getPos(next_action, pos) newCost = self.getCost(next_action) totalCost = cost + newCost if (newPos not in visited) and (newPos is not None) and (newPos not in gameState.getWalls()): states.push( (newPos, path + [next_action], totalCost), totalCost)
def chooseAction(self, gameState: GameState) -> str: actions = gameState.getLegalActions(self.index) food = self.getOpponentFood(gameState).asList() myPos = gameState.getAgentPosition(self.index) if self.target is not None: self.target.dist = self.getMazeDistance( (self.target.pos.x, self.target.pos.y), myPos ) targets = [Target(Position(x, y), self.getMazeDistance( (x, y), myPos)) for x, y in food] target: Target = min(targets, key=lambda target: target.dist) if self.target is None or target.dist < self.target.dist or len(self.path) == 0 or self.path is None: self.target = target maze = Maze(gameState.getWalls()) path = [] pos = myPos while True: directions = [ (pos[0], pos[1] + 1, "North"), (pos[0], pos[1] - 1, "South"), (pos[0] + 1, pos[1], "East"), (pos[0] - 1, pos[1], "West") ] maze[pos[0]][pos[1]].visited = True possibleMoves = [d for d in directions if not maze[d[0]][d[1]].isWall and not maze[d[0]][d[1]].visited] # print("path", path) # print("pos", pos) # print("possibleMoves", possibleMoves) if len(possibleMoves) == 0: if len(path) == 0: print("failed") sys.exit(-1) lastMove = path.pop() if lastMove == "North": pos = (pos[0], pos[1] - 1) if lastMove == "South": pos = (pos[0], pos[1] + 1) if lastMove == "East": pos = (pos[0] - 1, pos[1]) if lastMove == "West": pos = (pos[0] + 1, pos[1]) continue d = possibleMoves[0] path.append(d[2]) maze[d[0]][d[1]].visited = True pos = (d[0], d[1]) # but did we win? if pos[0] == self.target.pos.x and pos[1] == self.target.pos.y: # print("yay") # print("path", path) self.path = path break if gameState.isOnRedTeam(self.index): print(self.target, self.path) move = self.path[0] self.path = self.path[1:] return move
def getMyFood(self, gameState: GameState): if gameState.isOnRedTeam(self.index): return gameState.getRedFood() return gameState.getBlueFood()
def chooseAction(self, gameState: GameState) -> str: actions = gameState.getLegalActions(self.index) return random.choice(actions)
def chooseAction(self, gameState: GameState) -> str: actions = gameState.getLegalActions(self.index) return Directions.STOP