2019-05-29 16:54:26 -07:00
|
|
|
def annotate(minefield):
|
2021-11-01 11:39:06 -07:00
|
|
|
if not minefield:
|
2017-11-13 05:36:02 -08:00
|
|
|
return []
|
2019-05-29 16:54:26 -07:00
|
|
|
verify_board(minefield)
|
|
|
|
|
row_len = len(minefield[0])
|
|
|
|
|
col_len = len(minefield)
|
2019-05-29 16:59:08 -07:00
|
|
|
board = [list(row) for row in minefield]
|
2019-05-29 16:54:26 -07:00
|
|
|
|
|
|
|
|
for index1 in range(col_len):
|
|
|
|
|
for index2 in range(row_len):
|
|
|
|
|
if board[index1][index2] != ' ':
|
2014-03-28 22:19:26 -03:00
|
|
|
continue
|
2019-05-29 16:54:26 -07:00
|
|
|
|
|
|
|
|
low = max(index2 - 1, 0)
|
|
|
|
|
high = min(index2 + 2, row_len + 2)
|
|
|
|
|
counts = minefield[index1][low:high].count('*')
|
|
|
|
|
|
|
|
|
|
if(index1 > 0):
|
|
|
|
|
counts += minefield[index1 - 1][low:high].count('*')
|
|
|
|
|
if(index1 < col_len - 1):
|
|
|
|
|
counts += minefield[index1 + 1][low:high].count('*')
|
|
|
|
|
if counts == 0:
|
2014-03-28 22:19:26 -03:00
|
|
|
continue
|
2015-11-08 09:07:52 +01:00
|
|
|
|
2019-05-29 16:54:26 -07:00
|
|
|
board[index1][index2] = str(counts)
|
2019-05-29 16:59:08 -07:00
|
|
|
return ["".join(row) for row in board]
|
2015-11-08 09:07:52 +01:00
|
|
|
|
2019-05-29 16:54:26 -07:00
|
|
|
|
|
|
|
|
def verify_board(minefield):
|
2014-03-28 22:19:26 -03:00
|
|
|
# Rows with different lengths
|
2019-05-29 16:54:26 -07:00
|
|
|
row_len = len(minefield[0])
|
|
|
|
|
if not all(len(row) == row_len for row in minefield):
|
2021-11-01 11:39:06 -07:00
|
|
|
raise ValueError("The board is invalid with current input.")
|
2019-05-29 16:54:26 -07:00
|
|
|
|
2014-03-28 22:19:26 -03:00
|
|
|
# Unknown character in board
|
2019-05-29 16:54:26 -07:00
|
|
|
character_set = set()
|
|
|
|
|
for row in minefield:
|
|
|
|
|
character_set.update(row)
|
|
|
|
|
if character_set - set(' *'):
|
2021-11-01 11:39:06 -07:00
|
|
|
raise ValueError("The board is invalid with current input.")
|