minefield.py 文件源码

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

项目:defuse_division 作者: lelandbatey 项目源码 文件源码
def _populate_mines(self):
        """
        Method _populate_mines populates the cells of this minefield with
        mines. Applies a random selection of simple constraints to where mines
        may be placed, though about half of the mines are purely randomly
        placed.
        """
        if self.mine_count is None:
            self.mine_count = int(0.15 * (self.height * self.width))
        count = self.mine_count
        selectionfuncs = [
            lambda y: not (y % 2),
            lambda y: bool(y % 2),
            lambda y: not (y % 3),
        ]
        yconstraint, xconstraint = random.sample(selectionfuncs * 2, 2)
        for x in range(count):
            while True:
                rx, ry = random.randint(0, self.width - 1), random.randint(
                    0, self.height - 1)

                if random.randint(0, 1):
                    if yconstraint(ry):
                        continue
                    if xconstraint(rx):
                        continue
                c = self.board[rx][ry]
                if c.contents == Contents.empty:
                    c.contents = Contents.mine
                    break
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号