I am cooding a minesweeper and I have to make a function that will look through all the squares around the starting square. It should then open these squares and keep checking around the newly opened squares until there are no more safe squares in contact with the safe ones. What I have until now is
safe = [(x, y)]
while safe != []:
k, c = safe.pop(-1)
field[c][k] = "1"
for i in range(k - 1, k + 2):
for j in range(c - 1, c + 2):
if 0 <= i < len(field[0]) and 0 <= j < len(field):
if field[j][i] == "1":
pass
elif field[j][i] != "x":
field[j][i] = "1"
safe.append((i, j))
This does open the squares but It does so to squares in contact diagonally aswell giving me a result such as
The field I'm using is this
field = [
[" ", " ", " ", "x", " ", " ", " ", " ", " ", " ", " ", "x", " "],
[" ", " ", "x", "x", " ", " ", " ", "x", " ", " ", " ", "x", " "],
[" ", "x", "x", " ", " ", " ", " ", "x", " ", " ", "x", "x", " "],
["x", "x", "x", "x", "x", " ", " ", "x", " ", "x", " ", " ", " "],
["x", "x", "x", "x", " ", " ", " ", " ", "x", " ", "x", " ", " "],
[" ", " ", "x", " ", " ", " ", " ", " ", " ", "x", " ", " ", " "]]
What should I do to stop this from happening? Also I know the safe squares are all marked as 1 but this is an excerise in a course that will help me code the game fully later so It's not meant to give correct number of mines yet.