Created
October 30, 2012 19:55
-
-
Save dtuominen/3982599 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
from collections import defaultdict | |
import random | |
w, h, m = 15, 15, 20 | |
coords = lambda x,y: [(i, n) for i in xrange(h) for n in xrange(w)] | |
mine = lambda m: [random.choice([c for c in coords(h,w)]) for i in xrange(m)] | |
def create_board(w, h, m): | |
d = defaultdict(int) | |
for c in coords(w, h): | |
d[c] = 0 | |
for (k,v) in mine(m): | |
d[(k,v)] = 9 | |
for location in [(k-1, v-1), | |
(k-1, v+1), | |
(k-1, v), | |
(k+1, v), | |
(k+1, v+1), | |
(k+1, v-1), | |
(k, v+1), | |
(k, v-1)]: | |
if location in sorted(d.keys()): | |
d[location] += 1 if d[location] != 9 else 0 | |
return d | |
def create_rows(board, h): | |
rows = [] | |
for i in xrange(h+1): | |
row = [] | |
for k,v in (sorted(board.keys())): | |
if k == i: | |
row.append(board[(k,v)]) | |
rows.append(row) | |
return rows | |
if __name__ == '__main__': | |
board = create_board(w, h, m) | |
rows = create_rows(board, h) | |
for row in rows: | |
print '|'.join(map(str, [r for r in row])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment