示例#1
0
    def act(self, game: Game, snake_idx: int):
        snake = game.get_snake(snake_idx)
        possible_actions = snake.possible_actions()

        if possible_actions is None:
            return None

        grid_map = game.get_grid()

        busy_action = self.feel_busy(snake, game, grid_map)
        if busy_action is not None:
            return busy_action

        return np.random.choice(possible_actions)
示例#2
0
    def act(self, game: Game, snake_idx: int) -> Optional[Direction]:
        snake = game.get_snake(snake_idx)
        all_snacks = game.snakes
        grid_map = game.get_grid()
        opponent = None

        for i, s in enumerate(all_snacks):
            if i == snake_idx:
                continue
            opponent = s

        head = snake.get_head()
        # print('position of head [%d, %d] and direction: %s' % (head.x, head.y, head.direction))
        fruits = game.get_fruits()
        direction = None
        for fruit in fruits:
            cost, path = KILabAgent.a_star_search(game, head, head.direction, fruit, grid_map)
            field = path[0]
            if not opponent.is_dead():
                head_op = opponent.get_head()
                op_to_fruit = abs(head_op.x - fruit.x) + abs(head_op.y - fruit.y)
                to_fruit = abs(head.x - fruit.x) + abs(head.y - fruit.y)
                if snake.length() < opponent.length():
                    if to_fruit > op_to_fruit:
                        f = grid_map.get_object_at(int(game.width/2), int(game.height/2))
                        while game.is_obstacle(f):
                            f = grid_map.get_object_at(f.x + 1, f.y + 1)
                        field = f

            delta_x = head.x - field.x
            delta_y = head.y - field.y
            if delta_y == 0:
                if delta_x > 0:
                    direction = Direction.LEFT
                else:
                    direction = Direction.RIGHT
            if delta_x == 0:
                if delta_y > 0:
                    direction = Direction.UP
                else:
                    direction = Direction.DOWN

        return direction