Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def calculate_mine_count(minefield: list) -> list:
- """
- For each cell in minefield, calculate how many mines are nearby.
- This function cannot modify the original list.
- So, the result should be a new list (or copy of original).
- ....
- ..x.
- X.X.
- x..X
- =>
- 0111
- 13x2
- X3X3
- x32X
- :param minefield:
- :return:
- """
- new_minefield = deepcopy(minefield)
- x = "x" or "X"
- for index, element in enumerate(new_minefield):
- for second_index, i in enumerate(element):
- count = 0
- if i != element[0]:
- if new_minefield[index][second_index - 1] == x:
- count += 1
- if i != element[-1]:
- if new_minefield[index][second_index + 1] == x:
- count += 1
- if element != new_minefield[0] and i != element[0]:
- if new_minefield[index - 1][second_index - 1] == x:
- count += 1
- if element != new_minefield[0]:
- if new_minefield[index - 1][second_index] == x:
- count += 1
- if element != new_minefield[0] and i != element[-1]:
- if new_minefield[index - 1][second_index + 1] == x:
- count += 1
- if element != new_minefield[-1] and i != element[0]:
- if new_minefield[index + 1][second_index - 1] == x:
- count += 1
- if element != new_minefield[-1]:
- if new_minefield[index + 1][second_index] == x:
- count += 1
- if element != new_minefield[-1] and i != element[-1]:
- if new_minefield[index + 1][second_index + 1] == x:
- count += 1
- new_minefield[index][second_index] = str(count)
- if i == x:
- new_minefield[index][second_index] = i
- return new_minefield
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement