I'm trying to write a Minesweeper program using Python. Everything is working fine except for my cascading reveal. Here is what I have:
def rippleEffect(self, r, c):
if self.ActualBoard[r][c] == 0:
self.GameBoard[r][c] = self.ActualBoard[r][c]
rcchar = str(sum(1
for rr in (r-1, r, r+1)
for cc in (c-1, c, c+1)))
for rr in (r-1, r, r+1):
for cc in (c-1, c, c+1):
if self.ActualBoard[rr][cc] != 'M' and self.ActualBoard[rr][cc] > 0:
self.GameBoard[rr][cc] = self.ActualBoard[rr][cc]
else:
try:
if (rr,cc) != (r,c) and self.GameBoard[rr][cc] == 'H':
self.rippleEffect(rr, cc)
except IndexError:
pass
ActualBoard consists of list of lists with everything revealed, ie:
[[0, 1, 1, 1], [0, 1, M, 1], [0, 1, 1, 1], [0, 0, 0, 0]]
for a 4x4 board with one mine.
GameBoard consists of a list of lists as well but when the game starts it would look like so:
[['H','H', 'H', 'H'], ['H','H', 'H', 'H'], ['H','H', 'H', 'H'], ['H','H', 'H', 'H']]
Think about this: if r == 0
and c == 0
, what will self.GameBoard[r-1][c-1]
be? More specifically, would it raise an IndexError?