当前位置: 首页>>代码示例>>Python>>正文


Python World.my_ants方法代码示例

本文整理汇总了Python中World.World.my_ants方法的典型用法代码示例。如果您正苦于以下问题:Python World.my_ants方法的具体用法?Python World.my_ants怎么用?Python World.my_ants使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在World.World的用法示例。


在下文中一共展示了World.my_ants方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MyBot

# 需要导入模块: from World import World [as 别名]
# 或者: from World.World import my_ants [as 别名]
class MyBot(object):
    """
    The Bot is charged with recieving commands from the server,
    updating the internal world state, and sending commands
    back to the game server.
    """
    def __init__(self, stdin, stdout, stderr):
        """
        This method enters the input/output loop and never returns.
        """
        self.stdin = stdin
        self.stdout = stdout
        self.stderr = stderr
        self.state = None

        game_params = self.get_game_params(stdin)
        self.world = World(game_params)
        self.finish_turn()
        self.take_turns(stdin)


    @token_loop
    def take_turns(self, tokens):
        " Take turns, when we're told to go. "
        if tokens[0] == 'go':
            self.world.update(self.state['map_data'])
            self.do_turn()
            self.state['map_data'] = []
        else:
            self.state['map_data'].append(tokens)

    @token_loop
    def get_game_params(self, tokens):
        """
        Get the game's initial parameters
        Currently all values are 32bit signed integers,
              except player_seed is a 64bit signed integer.
        """
        if tokens[0] == 'ready':
            return self.state
        else:
            key, val = tokens
            self.state[key] = int(val)
    
    def issue_order(self, order):
        'issue an order by writing the proper ant location and direction'
        (row, col), direction = order
        self.stdout.write('o %s %s %s\n' % (row, col, direction))
        self.stdout.flush()
        
    def finish_turn(self):
        'finish the turn by writing the go line'
        self.stdout.write('go\n')
        self.stdout.flush()
    
    
    def do_turn(self):
        """
        do turn is run once per turn
        """
        # loop through all my ants and try to give them orders
        # the ant_loc is an ant location tuple in (row, col) form
        for ant_loc in self.world.my_ants():
            # try all directions in given order
            directions = ('n', 'e', 's', 'w')
            for direction in directions:
                # the destination method will wrap around the map properly
                # and give us a new (row, col) tuple
                new_loc = self.world.map.destination(ant_loc, direction)
                # passable returns true if the location is land
                if (self.world.map.passable(new_loc)):
                    # an order is the location of a current ant and a direction
                    self.issue_order((ant_loc, direction))
                    # stop now, don't give 1 ant multiple orders
                    break
            # check if we still have time left to calculate more orders
            if self.world.time_remaining() < 10:
                break
        self.finish_turn()
开发者ID:bukzor,项目名称:bukzor_aichallenge_py,代码行数:81,代码来源:MyBot.py


注:本文中的World.World.my_ants方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。