Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def get_number(a, b, text):
- while True:
- number = int(input(text))
- if number in range(a, b + 1):
- return number
- else:
- print(f"Liczba poza zakresem od {a} do {b}")
- def lay_mines(mine_count, rows, columns):
- mines = set()
- while len(mines) < mine_count:
- i = random.randrange(rows)
- j = random.randrange(columns)
- mines.add((i, j))
- return mines
- def number_of_neighbouring_mines(field, mines, rows, columns):
- i = field[0]
- j = field[1]
- number_of_mines = 0
- 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)]:
- if 0 <= m < rows and 0 <= n < columns and (m, n) in mines:
- number_of_mines += 1
- return number_of_mines
- def create_board(mines, rows, columns, mine = '*'):
- board = []
- for i in range(rows):
- row = []
- for j in range(columns):
- if (i,j) in mines:
- row.append(mine)
- else:
- row.append(number_of_neighbouring_mines((i,j), mines, rows, columns))
- board.append(row)
- return board
- def print_board(printable, board, rows, columns, print_all = False):
- print(" ", end="")
- for i in range(columns):
- print(f"{i:^3}", end="")
- print()
- for i in range(rows):
- print(f"{i} |", end="")
- for j in range(columns):
- if (i,j) in printable or print_all:
- print(f" {board[i][j]} |", end="")
- else:
- print(" # |", end="")
- print()
- def reveal_fields(field, printable, board, rows, columns):
- i = field[0]
- j = field[1]
- if not (0 <= i < rows and 0 <= j < columns) or (i,j) in printable:
- return
- printable.add((i, j))
- if board[i][j] != 0:
- return
- 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)]:
- reveal_fields((m,n), printable, board, rows, columns)
- rows = 10
- columns = 10
- number_of_mines = 10
- mines = lay_mines(number_of_mines, rows, columns)
- board = create_board(mines, rows, columns)
- printable = set()
- print_board(printable, board, rows, columns, True)
- #
- # for row in board:
- # for el in row:
- # print(f'{el:^3}|', end='')
- # print('\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement