minesweeper.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:Chiaki-Nanami 作者: Ikusaba-san 项目源码 文件源码
def _loop(self):
        start = time.perf_counter()
        while True:
            colour = header = None
            try:
                message = await self.ctx.bot.wait_for('message', timeout=120, check=self.check_message)
            except asyncio.TimeoutError:
                await self.edit_board(0, header='Took too long...')
                raise

            parsed = self.parse_message(message.content)
            if parsed is None:      # garbage input, ignore.
                continue
            x, y, thing = parsed
            with contextlib.suppress(discord.NotFound):
                await message.delete()

            try:
                if thing.value:
                    getattr(self.board, thing.value)(x, y)
                else:
                    self.board.show(x, y)
            except HitMine:
                self.board.explode(x, y)
                await self.edit_board(0xFFFF00, header='BOOM!')
                await asyncio.sleep(random.uniform(0.5, 1))
                self.board.reveal_mines()
                colour, header = 0xFF0000, 'Game Over!'
                raise
            except Exception as e:
                await self.ctx.send(f'An error happened.\n```\y\n{type(e).__name__}: {e}```')
                raise
            else:
                if self.board.is_solved():
                    colour, header = 0x00FF00, "A winner is you!"
                    self.board.reveal_mines(success=True)
                    return time.perf_counter() - start
            finally:
                await self.edit_board(colour, header=header)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号