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


Python Engine.start_round方法代码示例

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


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

示例1: __init__

# 需要导入模块: from engine import Engine [as 别名]
# 或者: from engine.Engine import start_round [as 别名]
class Controller:
    def __init__(self, connection):
        self._connection = connection
        self._engine = Engine()
        self._cmd_timestamps = deque([0] * 9, 5) # max 9 commands per second
        self._direction = Direction.STOP
        self._score = defaultdict(int)

    def start(self, botname, duelpartner = None):
        log.info("Started controller")
        if duelpartner == None:
            self._join_game(botname)
        else:
            self._request_duel(botname, duelpartner)
        self._mainloop()
        
    def _mainloop(self):   
        response_handlers = {
                'joined': self._game_joined,
                'gameStarted': self._game_started,
                'gameIsOn': self._make_move,
                'gameIsOver': self._game_over
                }
        response = ""
        while True:
            try:
                response = self._connection.receive()
                msg_type, data = response['msgType'], response['data']
                if response_handlers.has_key(msg_type):
                    response_handlers[msg_type](data)
                else:
                    log.warning('Invalid Message received: %s' % response) 
            except:
                log.exception('Json data: %s' % response) 
    
    def _game_joined(self, data):
        log.info('Game visualization url: %s' % data)

    def _game_started(self, data):
        log.info('Game started: %s vs. %s' % (data[0], data[1]))
        self._engine.start_round()

    def _game_over(self, data):
        log.info('Game ended. Winner: %s' % data)
        self._score[data] += 1
        log.info('Score: %s' % self._score)

    def _make_move(self, data):
        dataentry = DataEntry(data)
        direction = self._engine.check_direction(dataentry)   
        self._engine.update(dataentry)
        if direction != self._engine.direction:
            self._set_direction(self._engine.direction)
            
    def _join_game(self, botname):
        log.info('Joining game...')
        self._connection.send({'msgType': 'join', 'data': botname})
        
    def _request_duel(self, botname, duelpartner):
        log.info('Challenging %s...' % duelpartner)
        self._connection.send({'msgType': 'requestDuel', 'data': [botname, duelpartner]})
 
    def _set_direction(self, direction):
        if time.time() - self._cmd_timestamps[0] < Direction.DOWN:
            raise Exception("Discard message. Too many requests sent in one second!")
        self._cmd_timestamps.append(time.time())
        self._connection.send({'msgType': 'changeDir', 'data': direction})
开发者ID:dratini,项目名称:hwo2012-pingpongbot,代码行数:69,代码来源:pingpongbot.py


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