Last active
February 24, 2017 19:52
-
-
Save laughinghan/002ee4b82a7e0b7ec1b5a1a5b308eb7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import sys | |
def generate_board(width, height, number_of_mines, debug=False): | |
list_of_coords = [(x,y) for x in xrange(width) for y in xrange(height)] | |
list_of_mines = random.sample(list_of_coords, number_of_mines) | |
if debug: print 'mines:', list_of_mines | |
board = [[0] * height for _ in xrange(width)] | |
for x, y in list_of_mines: | |
board[x][y] = "*" | |
if debug: print board | |
for mine_x, mine_y in list_of_mines: | |
for dx in [-1, 0, 1]: | |
for dy in [-1, 0, 1]: | |
if dx == 0 and dy == 0: | |
continue | |
x, y = mine_x + dx, mine_y + dy | |
if not (0 <= x < width and 0 <= y < height): | |
continue | |
if board[x][y] == "*": | |
continue | |
board[x][y] += 1 | |
if debug: print board | |
for y in xrange(height): | |
for x in xrange(width): | |
sys.stdout.write(str(board[x][y])) | |
generate_board(7, 4, 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment