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