Пример #1
0
    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
Пример #2
0
    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)
Пример #3
0
    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
Пример #4
0
    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
Пример #5
0
    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
Пример #6
0
    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]
Пример #7
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]
Пример #8
0
    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
Пример #9
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
Пример #10
0
    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