import random height = 10 width = 10 mines = 10 def get_empty_board(height, width): return [[None for x in range(width)] for y in range(height)] def get_board_dims(board): return len(board), len(board[0]) def lay_mines(board, mines): height, width = get_board_dims(board) mines_layed = 0 while mines_layed < mines: x = random.randint(0, width - 1) y = random.randint(0, height - 1) if board[y][x] is None: board[y][x] = True mines_layed += 1 return board def get_neighbours(board, cx, cy): height, width = get_board_dims(board) cells = [] for dx in range(-1, 2): x = cx + dx for dy in range(-1, 2): y = cy + dy if not (dx == 0 and dy == 0) and (y >= 0 and y < height) and (x >= 0 and x < width): cells.append(board[y][x]) return cells def rate_board(board): height, width = get_board_dims(board) for y in range(height): for x in range(width): if board[y][x] is not True: neighbours = get_neighbours(board, x, y) board[y][x] = len(filter(lambda x: x is True, neighbours)) return board def display_board(board): print '0123456789' print '=' * 9 for row in board: print ''.join('X' if cell is True else unicode(cell) for cell in row) display_board(rate_board(lay_mines(get_empty_board(height, width), mines)))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question