I'm doing codefight's challange: minesweeper.
The description:
My code is as follows:
def minesweeper(matrix):
for x in range(len(matrix)):
matrix[x].insert(0, "x")
#matrix[x].insert(len(matrix)+2, "x")
frame = ["x" for i in range(len(matrix[0]))]
matrix.insert(0, frame)
matrix.insert(len(matrix), frame)
output_matrix = [[0 for j in range(len(matrix[0]))] for i in range(len(matrix))]
for i in range(0,len(matrix[0])-1):
for j in range(0,len(matrix)-1):
if matrix[i][j] == True:
output_matrix[i][j+1] += 1 # one right
output_matrix[i+1][j] += 1 # one down
output_matrix[i][j-1] += 1 # one left
output_matrix[i-1][j] += 1 # one up
output_matrix[i+1][j+1] += 1 # one down, one right
output_matrix[i+1][j-1] += 1 # one down, one right
output_matrix[i-1][j+1] += 1 # one up, one right
output_matrix[i-1][j-1] +=1 # one up, one left
output_matrix.pop(0)
output_matrix.pop(len(output_matrix)-1)
for y in range(len(output_matrix)):
output_matrix[y].pop(0)
#output_matrix[y].pop(len(output_matrix))
return output_matrix
The border created by "x", as suggested by codefight's user, is to ensure that if mine is at the border of matrix, bomb count won't transfer to the other side.
This code works fine until bomb is in the last column of the matrix, for example:
If input:
[[False, False, True],
[False, False, False],
[False, False, False]]
Output is:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
Could anyone explain clearly why that's happening?
I'd appreciate if someone could suggest a better approach to this task.
Thank you in advance.