Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
itertools.product(*iterables) solution in Clear category for Minesweeper by David_Jones
from itertools import product
EMPTY = -1
MINE = 9
ROWS = COLS = 10
GRADIENTS = ((-1,-1), (-1,0), (-1,1),
( 0,-1), ( 0,1),
( 1,-1), ( 1,0), ( 1,1))
def checkio(field):
for (x,y) in product(range(ROWS), range(COLS)):
if 0 <= field[x][y] < 9:
unopened_neighbors = []
mines = 0
for (dx,dy) in GRADIENTS:
if 0 <= x+dx < ROWS and 0 <= y+dy < COLS:
if field[x+dx][y+dy] is EMPTY:
unopened_neighbors.append((x+dx,y+dy))
elif field[x+dx][y+dy] is MINE:
mines += 1
if unopened_neighbors:
(i,j) = unopened_neighbors[0]
if field[x][y] == mines:
return (False, i, j)
if field[x][y] == len(unopened_neighbors) + mines:
return (True, i, j)
if field[0][0] is EMPTY:
return (False, 0, 0)
June 29, 2019
Comments: