Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {python:
- import random
- # Define the size of the Minesweeper board
- board_size = 10
- num_mines = 15
- # Initialize the board with empty spaces
- board = [['⬜️' for _ in range(board_size)] for _ in range(board_size)]
- # Place mines randomly on the board
- mines = set()
- while len(mines) < num_mines:
- x, y = random.randint(0, board_size - 1), random.randint(0, board_size - 1)
- mines.add((x, y))
- # Define the emojis for numbers
- number_emojis = ['0️⃣', '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣']
- # Calculate the number of adjacent mines for each cell
- for x in range(board_size):
- for y in range(board_size):
- if (x, y) in mines:
- board[x][y] = '💣'
- else:
- adjacent_mines = sum((i, j) in mines for i in range(x-1, x+2) for j in range(y-1, y+2) if 0 <= i < board_size and 0 <= j < board_size)
- board[x][y] = number_emojis[adjacent_mines]
- # Function to format the board for display
- def format_board(board):
- return '\n'.join(''.join(f'||{cell}||' for cell in row) for row in board)
- # Print the formatted board
- print(format_board(board))
- }
Advertisement
Add Comment
Please, Sign In to add comment