| 92 | return num_neighboring_bombs |
| 93 | |
| 94 | def dig(self, row, col): |
| 95 | # dig at that location! |
| 96 | # return True if successful dig, False if bomb dug |
| 97 | |
| 98 | # a few scenarios: |
| 99 | # hit a bomb -> game over |
| 100 | # dig at location with neighboring bombs -> finish dig |
| 101 | # dig at location with no neighboring bombs -> recursively dig neighbors! |
| 102 | |
| 103 | self.dug.add((row, col)) # keep track that we dug here |
| 104 | |
| 105 | if self.board[row][col] == "*": |
| 106 | return False |
| 107 | elif self.board[row][col] > 0: |
| 108 | return True |
| 109 | |
| 110 | # self.board[row][col] == 0 |
| 111 | for r in range(max(0, row - 1), min(self.dim_size - 1, row + 1) + 1): |
| 112 | for c in range(max(0, col - 1), min(self.dim_size - 1, col + 1) + 1): |
| 113 | if (r, c) in self.dug: |
| 114 | continue # don't dig where you've already dug |
| 115 | self.dig(r, c) |
| 116 | |
| 117 | # if our initial dig didn't hit a bomb, we *shouldn't* hit a bomb here |
| 118 | return True |
| 119 | |
| 120 | def __str__(self): |
| 121 | # this is a magic function where if you call print on this object, |