Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Minesweeper by dagger126
def checkio(field):
if field[0][0] == -1:
return [False, 0, 0]
def uncover_cell(y, x):
grids = [(y+n, x+m) for n in range(-1, 2) for m in range(-1, 2)]
unknown, mines = [], []
for n, m in grids:
try:
assert n > -1 and m > -1
if field[n][m] == -1:
unknown.append((n, m))
elif field[n][m] == 9:
mines.append((n, m))
except (AssertionError, IndexError):
pass
if unknown:
if field[y][x] == len(mines):
return False, unknown[0][0], unknown[0][1]
elif field[y][x] == len(mines)+len(unknown):
return True, unknown[0][0], unknown[0][1]
for j in range(10):
for i in range(10):
if field[j][i] in range(1, 9):
if uncover_cell(j, i):
return uncover_cell(j, i)
#This part is using only for self-testing
if __name__ == '__main__':
def check_is_win_referee(input_map):
unopened = [1 for x in range(10) for y in range(10) if input_map[x][y] == -1]
return not unopened
def build_map(input_map, mine_map, row, col):
opened = [(row, col)]
while opened:
i, j = opened.pop(0)
neighs = [(i + x, j + y) for x, y in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
if 0 <= i + x < 10 and 0 <= j + y < 10]
value = sum([mine_map[k][l] for k, l in neighs])
input_map[i][j] = value
if not value:
for k, l in neighs:
if input_map[k][l] == -1 and (k, l) not in opened:
opened.append((k, l))
return input_map
def check_solution(func, mine_map):
input_map = [[-1] * 10 for _ in range(10)]
while True:
is_mine, row, col = func([row[:] for row in input_map]) # using copy
if input_map[row][col] != -1:
print("You tried to uncover or mark already opened cell.")
return False
if is_mine and not mine_map[row][col]:
print("You marked the wrong cell.")
return False
if not is_mine and mine_map[row][col]:
print("You uncovered a mine. BANG!")
return False
if is_mine:
input_map[row][col] = 9
else:
build_map(input_map, mine_map, row, col)
if check_is_win_referee(input_map):
return True
return False
assert check_solution(checkio, [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), "Simple"
assert check_solution(checkio, [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), "Gate"
assert check_solution(checkio, [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 1, 1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]]), "Various"
July 19, 2014