viligen

minesweeper_generator

Feb 6th, 2022
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. def is_in_field(r, c, size_):
  2.     if 0 <= r < size_ and 0 <= c < size_:
  3.         return True
  4.     return False
  5.  
  6.  
  7. def check_field(r, c, field_):
  8.     current_value = 0
  9.     if is_in_field(r-1, c, len(field_)):
  10.         if field_[r-1][c] == '*':
  11.             current_value += 1
  12.     if is_in_field(r-1, c - 1, len(field_)):
  13.         if field_[r-1][c - 1] == '*':
  14.             current_value += 1
  15.     if is_in_field(r-1, c + 1, len(field_)):
  16.         if field_[r-1][c + 1] == '*':
  17.             current_value += 1
  18.     if is_in_field(r, c - 1, len(field_)):
  19.         if field_[r][c - 1] == '*':
  20.             current_value += 1
  21.     if is_in_field(r, c + 1, len(field_)):
  22.         if field_[r][c + 1] == '*':
  23.             current_value += 1
  24.     if is_in_field(r + 1, c, len(field_)):
  25.         if field_[r + 1][c] == '*':
  26.             current_value += 1
  27.     if is_in_field(r + 1, c - 1, len(field_)):
  28.         if field_[r + 1][c - 1] == '*':
  29.             current_value += 1
  30.     if is_in_field(r + 1, c + 1, len(field_)):
  31.         if field_[r + 1][c + 1] == '*':
  32.             current_value += 1
  33.     return current_value
  34.  
  35.  
  36. size = int(input())
  37. bombs_number = int(input())
  38. field = []
  39. for _ in range(size):
  40.     field.append([None]*size)
  41.  
  42. for _ in range(bombs_number):
  43.     coordinates = input().split(', ')
  44.     bomb_row = int(coordinates[0].lstrip('('))
  45.     bomb_col = int(coordinates[1].rstrip(')'))
  46.     if is_in_field(bomb_row, bomb_col, size):
  47.         field[bomb_row][bomb_col] = '*'
  48.  
  49. for row in range(size):
  50.     for col in range(size):
  51.         if not field[row][col]:
  52.             value = check_field(row, col, field)
  53.             field[row][col] = value
  54.  
  55. for r_ in field:
  56.     print(*r_)
  57.  
Advertisement
Add Comment
Please, Sign In to add comment