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


Python Table.linear_to_subscript方法代码示例

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


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

示例1: __init__

# 需要导入模块: from table import Table [as 别名]
# 或者: from table.Table import linear_to_subscript [as 别名]
class Game:
    """Playing field of the minesweeper game.
    The game provides three tables:
    1. The mines table indicate if a field contains a mine
    2. The flags table holds which fields are Unknown, Marked or Revealed
    3. The hints table holds a pre-computed number of surrounding mines for each field
    """

    def __init__(self, mines, flags=None):
        """Generates a Game from a given mine configuration.
        If flags is None, it will be initialized with Unknown.
        """
        if flags is None:
            flags = Table(mines.num_columns, mines.num_rows, Flags.Unknown)
        if mines.size() != flags.size():
            raise ValueError('Fields cannot have different sizes ({0} != {1})'.format(mines.size(), flags.size()))
        self.mines = mines
        self.flags = flags
        self.hints = Table(mines.num_columns, mines.num_rows, 0)

        for i, _ in enumerate(self.hints):
            self.hints[i] = self.hint(*self.hints.linear_to_subscript(i))

    def row_count(self):
        """Returns the vertical size of the field."""
        return self.mines.num_rows

    def column_count(self):
        """Returns the horizontal size of the field."""
        return self.mines.num_columns

    def hint(self, x, y):
        """Computes and returns the number of mines in the neighboring fields
        or -1 if the field itself is a mine."""
        if self.mines[x, y]:
            return -1
        else:
            h = 0
            for a, b in self.mines.neighbors(x, y):
                if(self.mines[a, b]):
                    h += 1
            return h

    def is_solved(self):
        """Returns true if all mines are flagged."""
        for m, f in zip(self.mines, self.flags):
            # if any mine is not marked, the game is not solved
            if m and f != Flags.Marked:
                return False
        return True

    def is_lost(self):
        """Returns true if a mine is revealed and the game is lost."""
        for m, f in zip(self.mines, self.flags):
            if m and f == Flags.Revealed:
                return True
        return False

    def auto_mark(self):
        """Marks all mines and reveals all fields but only if all non-mine fields are already revealed."""
        for m, f in zip(self.mines, self.flags):
            # check if all non-mine fields are revealed
            if not m and f != Flags.Revealed:
                return False

        # mark all mines and reveal remaining flields
        for i, m in enumerate(self.mines):
            if m:
                self.flags[i] = Flags.Marked
            else:
                self.flags[i] = Flags.Revealed

    def reveal_all(self):
        """Reveals all fields that are not marked."""
        for i, f in enumerate(self.flags):
            if f != Flags.Marked:
                self.flags[i] = Flags.Revealed

    def reveal(self, x, y, reveal_known=True):
        """Reveals a fields.
        Returns False if the revealed field was a mine field, True otherwise.
        If the revealed field has no neighboring mines all neighboring fields are revealed recursively.
        Revealing a Marked field resets it to Unknown again.
        If reveal_known is set, the field is already revealed and the number of
        flagged neighbors is equal to the hint, all non-flagged fields around
        the field are revealed as well.
        """
        if self.flags[x, y] == Flags.Marked:
            self.flags[x, y] = Flags.Unknown
        elif self.flags[x, y] == Flags.Revealed:
            if self.hints[x, y] <= 0 or not reveal_known:
                return True
            neighbors = list(self.mines.neighbors(x, y))
            neighbor_mines = [(nx, ny) for nx, ny in neighbors if self.flags[nx, ny] == Flags.Marked]
            if len(neighbor_mines) == self.hints[x, y]:
                for nx, ny in neighbors:
                    if (nx, ny) not in neighbor_mines:
                        # don't reveal fields recursively
                        self.reveal(nx, ny, reveal_known=False)
        else:
#.........这里部分代码省略.........
开发者ID:M-Mueller,项目名称:Minesweeper,代码行数:103,代码来源:minesweeper.py


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