Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firemark solution #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 101 additions & 4 deletions procedural_pixel_art/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,109 @@
import array
from datetime import datetime
from random import choice
from itertools import product

WIDTH = 5
HEIGHT = 10
X = -1

def generate_random_grid():
MOORE = [
(dx, dy)
for dx, dy in product([-1, 0, 1], repeat=2)
if (dx, dy) != (0, 0)
]

NEUMANN = [
(-1, 0),
( 1, 0),
( 0, 1),
( 0, -1),
]


def generate_x_row():
return [[X] * WIDTH]


def generate_random_row():
return [X] + [choice([0, 1]) for _ in range(WIDTH - 1)]


def generate_random_grid(generate_row=generate_random_row):
rows = [generate_row() for _ in range(HEIGHT - 2)]
return generate_x_row() + rows + generate_x_row()


def do_step(grid):
return [
[0, 1],
[1, 0],
[
change_cell_for_do_step(x, y, grid)
for x in range(WIDTH)
]
for y in range(HEIGHT)
]


def change_cell_for_do_step(x, y, grid):
cell = get_cell_from_grid(x, y, grid)
if cell == X:
return X

alive_neighbors = sum(
get_cell_from_grid(x + dx, y + dy, grid) == 1
for dx, dy in MOORE
)

if cell == 1 and alive_neighbors in (2, 3):
return 1
elif cell == 0 and alive_neighbors in (0, 1):
return 1
else:
return 0


def get_cell_from_grid(x, y, grid):
#if x < 0 or x >= WIDTH:
if x < 0 or x >= len(grid[0]):
return 0
#if y < 0 or y >= HEIGHT:
if y < 0 or y >= len(grid):
return 0
return grid[y][x]


def make_mirror(grid):
return [mirror_row(row) for row in grid]


def mirror_row(row):
left_row = row
right_row = row[::-1] # magic python here
return left_row + right_row


def make_border(grid):
return [
[
make_border_for_cell(cell, x, y, grid)
for x, cell in enumerate(row)
]
for y, row in enumerate(grid)
]


def make_border_for_cell(cell, x, y, grid):
if cell == 1:
return 1

any_neighbor_is_alive = any(
get_cell_from_grid(x + dx, y + dy, grid) == 1
for dx, dy in NEUMANN
)

return 2 if any_neighbor_is_alive else 0


def grid_to_array(grid):
"""
Used in save_grid function
Expand All @@ -17,7 +112,9 @@ def grid_to_array(grid):
for row in grid:
for cell in row:
if cell == 1:
yield from [0, 0, 255] # blue color
yield from [255, 0, 0] # red color
elif cell == 2:
yield from [255, 255, 0] # yellow color
else:
yield from [0, 0, 0] # black color

Expand Down
10 changes: 7 additions & 3 deletions procedural_pixel_art/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

# this code will execute on running
from procedural_pixel_art import generate_random_grid, save_grid
import procedural_pixel_art

grid = generate_random_grid()
save_grid(grid)
grid = procedural_pixel_art.generate_random_grid()
grid = procedural_pixel_art.do_step(grid)
grid = procedural_pixel_art.do_step(grid)
grid = procedural_pixel_art.make_mirror(grid)
grid = procedural_pixel_art.make_border(grid)
procedural_pixel_art.save_grid(grid)
98 changes: 98 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,109 @@
from procedural_pixel_art import (
generate_random_grid,
save_grid,
do_step,
make_mirror,
make_border,
X,
)


def test__generate_random_grid():
grid = generate_random_grid()

assert len(grid) == 10
assert len(grid[0]) == 5
assert any(0 in row for row in grid)
assert any(1 in row for row in grid)
assert all(
isinstance(val, int)
for row in grid
for val in row
)
assert _col(grid, 0) == [X] * 5
assert _col(grid, 9) == [X] * 5
assert _row(grid, 0) == [X] * 10


def test__do_step__still_alive_condition():
grid = generate_random_grid(_empty_row)

grid[3][3] = 1 # cell to be alive
grid[3][4] = 1 # two alive neigbors
grid[4][3] = 1

new_grid = do_step(grid)

# I'M STILL ALIVE
# I'M STILL ALIVE
assert new_grid[3][3] == 1 # AH AH AH STILL ALIIIVEEEE


def test__do_step__regeneration_condition():
grid = generate_random_grid(_empty_row)

grid[3][3] = 0 # dead cell
grid[3][4] = 1 # one alive neighbor

new_grid = do_step(grid)

assert new_grid[3][3] == 1


def test__do_step__still_dead_condition():
grid = generate_random_grid(_empty_row)

grid[3][3] = 0 # dead cell
grid[3][4] = 1 # two alive neigbors
grid[4][3] = 1

new_grid = do_step(grid)

assert new_grid[3][3] == 0 # two neighbors, still dead


def test__mirror():
grid = generate_random_grid(_empty_row)

# row 2, one alive cell
grid[2][3] = 1

# row 3, two alive cells
grid[3][2] = 1
grid[3][3] = 1

new_grid = make_mirror(grid)

assert all(len(row) == 10 for row in new_grid)

assert _col(new_grid, 0) == [X] * 10
assert _col(new_grid, 1) == [X] + [0] * 8 + [X]
assert _col(new_grid, 2) == [X, 0, 0, 1, 0, 0, 1, 0, 0, X]
assert _col(new_grid, 3) == [X, 0, 1, 1, 0, 0, 1, 1, 0, X]


def test__make_border():
grid = generate_random_grid(_empty_row)

grid[3][3] = 1

new_grid = make_border(grid)

assert grid[3][3] == 1 # center

assert new_grid[2][3] == 2
assert new_grid[3][2] == 2
assert new_grid[4][3] == 2
assert new_grid[3][4] == 2


def _empty_row():
return [X, 0, 0, 0, 0]


def _col(grid, x):
return grid[x]


def _row(grid, x):
return [r[x] for r in grid]