Advertisement
Sichanov

02. Minesweeper Generator

Jan 30th, 2022
1,367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. size = int(input())
  2.  
  3. matrix = []
  4.  
  5. for _ in range(size):
  6.     line = '_' * size
  7.     matrix.append(list(line))
  8.  
  9. bombs = int(input())
  10. bomb_list = []
  11. for _ in range(bombs):
  12.     bomb_row, bomb_col = [int(i) for i in input().strip('()').split(', ')]
  13.     bomb_list.append([bomb_row, bomb_col])
  14.  
  15. for row in range(size):
  16.     for col in range(size):
  17.         if [row, col] in bomb_list:
  18.             matrix[row][col] = '*'
  19.  
  20.  
  21. def check_around(size, row, col):
  22.     result = 0
  23.     # up row - 1, col
  24.     if 0 <= row - 1 < size and 0 <= col < size and matrix[row - 1][col] == '*':
  25.         result += 1
  26.     # down row + 1, col
  27.     if 0 <= row + 1 < size and 0 <= col < size and matrix[row + 1][col] == '*':
  28.         result += 1
  29.     # left row, col - 1
  30.     if 0 <= row < size and 0 <= col - 1 < size and matrix[row][col - 1] == '*':
  31.         result += 1
  32.     # right row, col + 1
  33.     if 0 <= row < size and 0 <= col + 1 < size and matrix[row][col + 1] == '*':
  34.         result += 1
  35.     # up_left_corner row - 1, col - 1
  36.     if 0 <= row - 1 < size and 0 <= col - 1 < size and matrix[row - 1][col - 1] == '*':
  37.         result += 1
  38.     # down_left_corner row + 1, col - 1
  39.     if 0 <= row + 1 < size and 0 <= col - 1 < size and matrix[row + 1][col - 1] == '*':
  40.         result += 1
  41.     # up_right_corner row - 1, col + 1
  42.     if 0 <= row - 1 < size and 0 <= col + 1 < size and matrix[row - 1][col + 1] == '*':
  43.         result += 1
  44.     # down_right_corner row + 1, col + 1
  45.     if 0 <= row + 1 < size and 0 <= col + 1 < size and matrix[row + 1][col +  1] == '*':
  46.         result += 1
  47.     return result
  48.  
  49.  
  50. for i in range(size):
  51.     for j in range(size):
  52.         if matrix[i][j] == '*':
  53.             continue
  54.         matrix[i][j] = check_around(size, i, j)
  55.  
  56. for i in matrix:
  57.     print(*i)
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement