Advertisement
wa12rior

python spaer board

Feb 26th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. import random
  2.  
  3. def get_number(a, b, text):
  4.     while True:
  5.         number = int(input(text))
  6.         if number in range(a, b + 1):
  7.             return number
  8.         else:
  9.             print(f"Liczba poza zakresem od {a} do {b}")
  10.  
  11. def lay_mines(mine_count, rows, columns):
  12.     mines = set()
  13.     while len(mines) < mine_count:
  14.         i = random.randrange(rows)
  15.         j = random.randrange(columns)
  16.         mines.add((i, j))
  17.  
  18.     return mines
  19.  
  20. def number_of_neighbouring_mines(field, mines, rows, columns):
  21.     i = field[0]
  22.     j = field[1]
  23.  
  24.     number_of_mines = 0
  25.  
  26.     for m, n in [(i-1, j-1), (i-1, j), (i-1, j+1), (i, j-1), (i, j+1), (i+1, j-1), (i+1, j), (i+1, j+1)]:
  27.         if 0 <= m < rows and 0 <= n < columns and (m, n) in mines:
  28.             number_of_mines += 1
  29.  
  30.     return number_of_mines
  31.  
  32. def create_board(mines, rows, columns, mine = '*'):
  33.     board = []
  34.     for i in range(rows):
  35.         row = []
  36.         for j in range(columns):
  37.             if (i,j) in mines:
  38.                 row.append(mine)
  39.             else:
  40.                 row.append(number_of_neighbouring_mines((i,j), mines, rows, columns))
  41.         board.append(row)
  42.  
  43.     return board
  44.  
  45. def print_board(printable, board, rows, columns, print_all = False):
  46.     print("   ", end="")
  47.     for i in range(columns):
  48.         print(f"{i:^3}", end="")
  49.     print()
  50.     for i in range(rows):
  51.         print(f"{i}  |", end="")
  52.         for j in range(columns):
  53.             if (i,j) in printable or print_all:
  54.                 print(f" {board[i][j]} |", end="")
  55.             else:
  56.                 print(" # |", end="")
  57.         print()
  58.  
  59. def reveal_fields(field, printable, board, rows, columns):
  60.     i = field[0]
  61.     j = field[1]
  62.     if not (0 <= i < rows and 0 <= j < columns) or (i,j) in printable:
  63.         return
  64.     printable.add((i, j))
  65.     if board[i][j] != 0:
  66.         return
  67.     for m, n in [(i - 1, j - 1), (i - 1, j), (i - 1, j + 1), (i, j - 1), (i, j + 1), (i + 1, j - 1), (i + 1, j),
  68.                  (i + 1, j + 1)]:
  69.         reveal_fields((m,n), printable, board, rows, columns)
  70.  
  71. rows = 10
  72. columns = 10
  73. number_of_mines = 10
  74. mines = lay_mines(number_of_mines, rows, columns)
  75.  
  76. board = create_board(mines, rows, columns)
  77.  
  78. printable = set()
  79.  
  80. print_board(printable, board, rows, columns, True)
  81. #
  82. # for row in board:
  83. #     for el in row:
  84. #         print(f'{el:^3}|', end='')
  85. #     print('\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement