Advertisement
pacho_the_python

mines

Jun 14th, 2022
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. def possible_cells(r, c, num):
  5.     cells = [
  6.         [r - 1, c - 1],
  7.         [r - 1, c],
  8.         [r - 1, c + 1],
  9.         [r, c - 1],
  10.         [r, c + 1],
  11.         [r + 1, c - 1],
  12.         [r + 1, c],
  13.         [r + 1, c + 1]
  14.     ]
  15.     surrounding_cells = []
  16.     for x, y in cells:
  17.         if 0 <= x < num and 0 <= y < num:
  18.             surrounding_cells.append([x, y])
  19.     return surrounding_cells
  20.  
  21.  
  22. size = int(input())
  23.  
  24. mine = "*"
  25. save = "-"
  26.  
  27. combination = mine + save
  28.  
  29. field = []
  30.  
  31. for i in range(size):
  32.     current_row = random.choices(combination, weights=[15, 35], k=size)
  33.     field.append(current_row)
  34.  
  35. for row in range(size):
  36.     for col in range(size):
  37.         if field[row][col] == "*":
  38.             continue
  39.         bomb_counter = 0
  40.         for i in possible_cells(row, col, size):
  41.             a, b = i
  42.             if field[a][b] == "*":
  43.                 bomb_counter += 1
  44.         # if bomb_counter == 0:
  45.         #     field[row][col] = " "
  46.         # else:
  47.         #     field[row][col] = str(bomb_counter)
  48.         field[row][col] = str(bomb_counter)
  49. for k in field:
  50.     print(*k, sep=" ")
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement