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 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 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): 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 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 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 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