Skip to content

Instantly share code, notes, and snippets.

@bobmurder
Created October 31, 2012 07:43
Show Gist options
  • Save bobmurder/3985673 to your computer and use it in GitHub Desktop.
Save bobmurder/3985673 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from collections import defaultdict
from itertools import repeat
from pprint import pprint
from random import choice
def grid(height, width):
return [['0' for n in range(width)] for n in range(height)]
def place_bombs(grid, n_bombs):
height, width, left = len(grid), len(grid[0]), n_bombs
bombs = []
while left > 0:
x, y = choice(range(height)), choice(range(width))
if grid[x][y] == '0':
grid[x][y] = 'X'
bombs.append((x, y))
left -= 1
return grid, bombs
# spaces to check
#
# -1, -1 | 0, -1 | 1, -1
# -1, 0 | point | 1, 0
# -1, 1 | 0, 1 | 1, 1
#
# dict keys are as follows:
# 1 | 2 | 3
# 4 | | 5
# 6 | 7 | 8
def calc_table():
# another ugly hardcoded table
table_keys = range(1, 9)
return dict(zip(table_keys,
[(-1, -1), (0, -1), (1, -1), # 1 to 3
(-1, 0), (1, 0), # 4 and 5
(-1, 1), (0, 1), (1, 1)] # 6 to 8
))
top_cells = (4, 5, 6, 7, 8)
bottom_cells = (1, 2, 3, 4, 5)
left_cells = (2, 3, 5, 7, 8)
right_cells = (1, 2, 4, 6, 7)
# boundary conditions:
#---------------------
# if coord = (0, 0), (0, 14), (14, 0), (14, 14)
# only three checks are needed.
# each corner checks the 3 coords opposite itself in the chart above
# if point is upper left, check the spaces to the right, lower-right, and below
# those the the 3 spaces opposite the upper left corner (-1, -1) above.
def corners(height, width):
# hardcode this shit for now
height, width = map(lambda x: x - 1, [height, width])
return dict([
((0, 0), [(0, 1), (1, 0), (1, 1)]), # top left
((width, 0), [(-1, 0), (-1, 1), (0, 1)]), # top right
((0, height), [(0, -1), (1, -1), (1, 0)]), # bottom left
((width, height), [(-1, 0), (-1, -1), (0, -1)]) # bottom right
])
# if coord is in range:
# (1, 0) - (13, 0) or
# (0, 1) - (0, 13) or
# (14, 1), (14, 13) or
# (1, 14), (13, 14)
# only 5 checks are necessary
def edges(height, width):
return dict([
('top', [(n, 0) for n in range(1, width)]),
('bottom', [(n, 14) for n in range(1, width)]),
('left', [(0, n) for n in range(1, height)]),
('right', [(14, n) for n in range(1, height)])
])
def get_coord(bomb_coord, movement):
# both args are tuples.
# bomb_coord is from the bombs list
# movement is a tuple from calc_table().values()
x, y = bomb_coord
move_x, move_y = movement
return (x + move_x, y + move_y)
# all other coords require all 8 checks.
# there is no need to make a data structure of the rest.
# to check surrounding spaces, do this:
# 1. check if the space is a bomb
# 2. if it is a bomb, return False
# 3. if it isn't a bomb, return True
def check_space(grid, coord):
# grid is a list of lists, coord is a tuple in the form (x, y)
# checking for a bomb instead of '0' allows us to change when we insert
# values into the grid. as of now, the grid is only populated with
# 'X' or '0' until all spaces are checked.
if grid[coord[0]][coord[1]] == '0':
return True
return False
def increment(grid, bomb, cells, movements):
for movement in movements:
cell = tuple(get_coord(bomb, movement))
if check_space(grid, cell):
cells[cell] += 1
grid = grid(15, 15)
grid, bombs = place_bombs(grid, 20)
calc_table = calc_table()
corners = corners(15, 15)
edges = edges(15, 15)
number_cells = defaultdict(int)
def get_flags(grid, bombs, cells):
for bomb in bombs:
if bomb in corners:
movements = corners[bomb]
elif bomb in edges['top']:
movements = [calc_table[n] for n in top_cells]
elif bomb in edges['bottom']:
movements = [calc_table[n] for n in bottom_cells]
elif bomb in edges['left']:
movements = [calc_table[n] for n in left_cells]
elif bomb in edges['right']:
movements = [calc_table[n] for n in right_cells]
else:
movements = calc_table.values()
increment(grid, bomb, cells, movements)
return cells
cells = get_flags(grid, bombs, number_cells)
def write_flags(cells):
for k, v in cells.iteritems():
grid[k[0]][k[1]] = str(v)
return grid
for n in range(1000):
write_flags(cells)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment