Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Minesweeper by Kurush
def checkio(field):
n = len(field)
for i in range(0, n):
for j in range(0, n):
if field[i][j] in range(1, 9):
none_list, is_mine = check_digit(field, i, j)
for i, j in none_list:
if field[i][j] == -1:
return [is_mine, i, j]
return [False, 0, 0]
def check_digit(field, i, j):
none_list = []
number_of_nones = 0
number_of_mines = 0
for k in range (-1, 2):
for l in range (-1, 2):
if i + k >= len(field) or i + k < 0: continue
if j + l >= len(field) or j + l < 0: continue
if field[i + k][j + l] == -1:
number_of_nones += 1
none_list.append([i + k, j + l])
if field[i + k][j + l] == 9:
number_of_mines +=1
if number_of_nones <= field[i][j] - number_of_mines:
return none_list, True
elif field[i][j] - number_of_mines == 0: return none_list, False
else: return [], True
#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"
June 26, 2014