Hackbright Code Challenges

Minesweeper!: Solution

Minesweeper!: Solution

Problem

Minesweeper!

Challenge

Harder

Concepts

Object Orientation, Reading Code

Download

minesweeper-solution.zip


class Cell(object):  # ...
    def reveal_and_check_neighbors(self):
        """Reveal this cell and all of its neighbor cells, recursively.

        If a cell is not a mine, reveal it and then do the same for all of its
        neighbors. Repeat until there are no more cells found to check.
        """

        # START SOLUTION

        to_check = {self}
        seen = set()

        while to_check:
            cell = to_check.pop()
            seen.add(cell)

            if not cell.mine and not cell.revealed:
                cell.revealed = True
                self.board.left -= 1

                if cell.number == 0:
                    to_check.update(set(cell.neighbors()) - seen)