Beispiel #1
0
class SimpleBot(Bot):
    def __init__(self):
        self.customer = None
        self.customer_loc = None
        self.game = None
        self.next_state = self.init
        self.state_before_heal = self.init
        self.fries_loc = None
        self.burger_loc = None
        self.drink_loc = None
        self.pathfinder = None
        self.min_heal = 30
        self.num_death = 0
        self.hero_health = 100
        self.hero_calories = 0

    def init(self):
        self.next_state = self.get_fries
        self.state_before_heal = self.get_fries
        self.customer = None
        self.customer_loc = None
        self.fries_loc = None
        self.drink_loc = None
        self.pathfinder = None

        return 'Stay'

    def exec(self, state):
        self.game = Game(state)
        self.pathfinder = Pathfinder(self.game)

        # Choix d'un nouveau client
        if self.customer is None:
            print("Selecting customer")
            _, self.customer_loc = self.pathfinder.get_closest_customer(
                get_hero_pos(self.game))
            self.customer = get_customer_by_pos(self.customer_loc, self.game)

        if (self.num_death >= 5) and (self.min_heal <= 45):
            self.min_heal += 5
            self.num_death = 0

        destination = self.next_state()

        self.hero_health = get_hero_life(self.game)
        self.hero_calories = get_our_hero(self.game).calories

        # Reset de mort et check de healing
        if self.hero_health == 0:
            self.num_death += 1
            self.next_state = self.init
        elif (self.hero_calories > 30) and (self.hero_health < self.min_heal):
            if self.next_state != self.heal:
                self.state_before_heal = self.next_state
            self.next_state = self.heal

        return destination

    def get_fries(self):

        if self.fries_loc is None:
            print("Choosing new fries")
            fries_tile, self.fries_loc = self.pathfinder.get_closest_fries(
                get_hero_pos(self.game))

        client_fries = self.customer.french_fries
        hero_fries = get_hero_fries(self.game)

        direction = get_direction(self.game, self.fries_loc)
        hero_loc = get_hero_pos(self.game)

        if self.pathfinder.get_distance(self.fries_loc, hero_loc) <= 1:
            print("Fries acquired")
            self.fries_loc = None

        if hero_fries >= client_fries:
            self.next_state = self.get_burgers
        else:
            self.next_state = self.get_fries

        return direction

    def get_burgers(self):

        if self.burger_loc is None:
            print("Choosing new burger")
            burger_tile, self.burger_loc = \
                self.pathfinder.get_closest_burger(get_hero_pos(self.game))

        client_burgers = self.customer.burger
        hero_burgers = get_hero_burgers(self.game)
        hero_loc = get_hero_pos(self.game)

        direction = get_direction(self.game, self.burger_loc)

        if self.pathfinder.get_distance(self.burger_loc, hero_loc) <= 1:
            print("Burger acquired")
            self.burger_loc = None

        if hero_burgers >= client_burgers:
            if get_hero_fries(self.game) >= self.customer.french_fries:
                self.next_state = self.goto_customer
            else:
                self.next_state = self.get_fries
        else:
            self.next_state = self.get_burgers

        return direction

    def heal(self):
        # Check si on a les calories pour guerir
        if self.hero_calories <= 30:
            self.next_state = self.state_before_heal
            return 'STAY'

        hero_loc = get_hero_pos(self.game)
        if self.drink_loc is None:
            drink_tile, self.drink_loc = self.pathfinder.get_closest_drink(
                hero_loc)
        direction = get_direction(self.game, self.drink_loc)

        print("Healing time! drink pos: {}".format(self.drink_loc))
        if (self.pathfinder.get_distance(self.drink_loc, hero_loc) <= 1) \
                or (self.hero_health == 100):
            print("drink acquired")
            self.drink_loc = None
            self.min_heal -= (100 - self.hero_health)
            if self.min_heal < 30:
                self.min_heal = 30
            self.next_state = self.state_before_heal

        return direction

    def goto_customer(self):
        direction = get_direction(self.game, self.customer_loc)
        hero_loc = get_hero_pos(self.game)
        if self.pathfinder.get_distance(self.customer_loc, hero_loc) <= 1:
            self.customer = None
            self.customer_loc = None
            self.next_state = self.get_fries

        return direction

    def opportunity(self):
        direction = self.check_for_opportunity('Stay')

        # Transition d'etat
        if get_hero_fries(self.game) < self.customer.french_fries:
            self.next_state = self.get_fries
        elif get_hero_burgers(self.game):
            self.next_state = self.get_burgers
        else:
            self.next_state = self.goto_customer

        # Retourne mouvement
        if direction:
            return direction
        else:
            return 'Stay'

    def check_for_opportunity(self, direction):
        cardinal_directions = ['WEST', 'EAST', 'NORTH', 'SOUTH']
        hero_loc = get_hero_pos(self.game)

        for dir in cardinal_directions:
            destination = self.game.board.to(hero_loc, direction)
            row, col = destination
            tile = self.game.board.tiles[row][col]

            print("Direction choisi: {} -- opportunite: {}".format(
                type(direction), type(dir)))
            if direction != dir and (isinstance(tile, FriesTile)
                                     or isinstance(tile, BurgerTile)):

                owner_id = tile.hero_id
                if owner_id == '-':
                    owner_id = '-1'
                if int(owner_id) != get_our_hero_id(self.game):
                    return dir