Hello I'm fairly new to python and I'm currently trying to construct a minesweeper game. I've made 2 lists, 1 is shown to the player (field) and 1 contains the mines (minefield). I ask the player to select coordinates and the program then checks whether there are mines around it or not and flood fills the map around the mines using this code:
def floodfill(field, minefield, x, y)
list1 = [(x, y)]
while True:
a, b = list1.pop()
field[b][a] = 'W'
list2 = [(a, b - 1), (a, b + 1), (a - 1, b), (a + 1, b)]
for element in list2:
x2 = element[0]
y2 = element[1]
if y2 >= len(field) or x2 >= len(field[0]) or y2 < 0 or x2 < 0:
continue
if minefield[y2][x2] == 'O':
list1.append(element)
if not list1:
break
return field
So basically I'm trying to check if there are mines around the chosen coordinates by looking at the 'minefield' list and then modifying the 'field' list according the 'minefield' list. The problem I have that the program jams if there are any non-mine blocks around the given coordinates.
for example when I run it with these nothing happens.
minefield = [['O', 'O'], ['x', 'x']] # field is the same without xs
x = 0
y = 0
But with these:
minefield = [['O', 'x'], ['x', 'x']] # field is the same without xs
x = 0
y = 0
It returns this:
field = [['W', 'O'], ['O', 'O']] # Os are covered mines