The proposed solution consists of three parts:First we need a function valor_celdawhich recovers the value of a given cell (fila, column). If the cell doesn't exist, it returns zero. This function is responsible for the problem of seeing whether or not a cell exists.Second. With that function we can build the function cuenta_minasthat counts the mines around a row, column.Third, we went through the board counting the mines around each cell.The beginning is called "dividing to reign"The code is:tablero = [["A",0,0,0,0,8,0,0,0,0,0,8,0,0,0,0],
["B",0,0,0,0,0,0,0,0,0,0,8,0,0,0,0],
["C",8,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
["D",0,0,0,0,0,0,0,0,0,0,8,0,0,0,0],
["E",0,0,0,0,0,8,0,0,0,0,0,0,0,0,0],
["F",0,0,8,0,0,0,0,0,0,0,0,0,0,0,0],
["G",0,0,0,0,0,0,0,0,0,8,0,0,0,0,0],
["H",0,0,0,0,0,8,0,0,0,0,0,0,0,0,0],
["I",0,8,0,8,0,0,0,0,0,0,8,0,0,0,0],
["J",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
["K",0,0,0,0,0,0,0,0,0,8,0,8,0,0,0],
["L",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
["M",0,0,0,8,0,0,0,0,0,0,0,0,0,0,0],
["N",0,0,0,0,0,0,0,0,0,8,0,0,0,0,0],
["O",0,0,0,0,0,8,0,0,0,0,0,0,0,0,0]]
n_filas = len(tablero)
n_columnas = len(tablero[0])
def valor_celda(fila, columna):
""" Recupera el valor de la celda en fila, columna.
Si la celda no existe, retorna cero.
"""
valor = 0
if fila >= 0 and fila < n_filas:
if columna > 0 and columna < n_columnas:
valor = tablero[fila][columna]
return valor
def cuenta_minas(fila, columna):
""" Cuenta las minas alrededor de fila, columna.
Actualiza la cuenta directamente en el tablero.
"""
if tablero[fila][columna] == 0:
for row in range(fila - 1, fila + 2):
for col in range(columna - 1, columna + 2):
if valor_celda(row, col) == 8:
tablero[fila][columna] += 1
Recorrer el tablero contando las minas.
for fila in range(n_filas):
for columna in range(1, n_columnas):
cuenta_minas(fila, columna)
for fila in tablero:
print(fila)
It produces:['A', 0, 0, 0, 1, 8, 1, 0, 0, 0, 2, 8, 2, 0, 0, 0]
['B', 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 8, 2, 0, 0, 0]
['C', 8, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0]
['D', 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 8, 1, 0, 0, 0]
['E', 0, 1, 1, 1, 1, 8, 1, 0, 0, 1, 1, 1, 0, 0, 0]
['F', 0, 1, 8, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0]
['G', 0, 1, 1, 1, 1, 1, 1, 0, 1, 8, 1, 0, 0, 0, 0]
['H', 1, 1, 2, 1, 2, 8, 1, 0, 1, 2, 2, 1, 0, 0, 0]
['I', 1, 8, 2, 8, 2, 1, 1, 0, 0, 1, 8, 1, 0, 0, 0]
['J', 1, 1, 2, 1, 1, 0, 0, 0, 1, 2, 3, 2, 1, 0, 0]
['K', 0, 0, 0, 0, 0, 0, 0, 0, 1, 8, 2, 8, 1, 0, 0]
['L', 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 2, 1, 1, 0, 0]
['M', 0, 0, 1, 8, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0]
['N', 0, 0, 1, 1, 2, 1, 1, 0, 1, 8, 1, 0, 0, 0, 0]
['O', 0, 0, 0, 0, 1, 8, 1, 0, 1, 1, 1, 0, 0, 0, 0]