Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def is_coord_alive(coord):
cell = self.get(*coord, default=Dead())
return int(cell.is_alive)
@classmethod
def dead_grid(cls, *, height=None, width=None):
return [
[Dead() for _cols in range(width)]
for _rows in range(height)
]
@classmethod
def from_str(cls, string):
if string == Live.string_form:
return Live()
return Dead()
def next_state(self, neighbor_count):
if neighbor_count in [2, 3]:
return Live()
return Dead()
def run_string_example(
*,
seed_string=None,
seed_name=None,
num_gens=10
):
seed_game = GameOfLife.from_str(seed_string)
if seed_name is None:
seed_name = f'A {seed_game.height}x{seed_game.width} grid'
print(dedent(f'''
=========================
| Conway's Game of Life |
{'':=^50}
| {f'Starting with seed: "{seed_name:.10}"': <46.46} |
| {f'Running for {str(num_gens):1.3} generations.': <46.46} |
{'':=^50}
'''))
latest_generation = seed_game
for gen_num in range(1, num_gens + 1):
print(f'Generation {gen_num}:')
print(str(latest_generation))
latest_generation = latest_generation.next_generation()
print('Done')
@classmethod
def from_str(cls, string):
non_empty_lines = (
line for line in string.splitlines()
if len(line) > 0
)
parsed_grid = [
[Cell.from_str(char) for char in line]
for line in non_empty_lines
]
return cls(grid=parsed_grid)
if is_within_rows and is_within_cols:
return self.grid[row][col]
return default
class Cell:
@classmethod
def from_str(cls, string):
if string == Live.string_form:
return Live()
return Dead()
def __str__(self):
return self.string_form
class Dead(Cell):
string_form = '·'
is_alive = False
def next_state(self, neighbor_count):
if neighbor_count == 3:
return Live()
return Dead()
class Live(Cell):
string_form = '0'
is_alive = True
def next_state(self, neighbor_count):
if neighbor_count in [2, 3]:
return Live()
return Dead()
return Live()
return Dead()
def __str__(self):
return self.string_form
class Dead(Cell):
string_form = '·'
is_alive = False
def next_state(self, neighbor_count):
if neighbor_count == 3:
return Live()
return Dead()
class Live(Cell):
string_form = '0'
is_alive = True
def next_state(self, neighbor_count):
if neighbor_count in [2, 3]:
return Live()
return Dead()
from textwrap import dedent
def run_string_example(
*,
seed_string=None,
seed_name=None,
num_gens=10
@classmethod
def from_str(cls, string):
if string == Live.string_form:
return Live()
return Dead()
@classmethod
def from_str(cls, string):
if string == Live.string_form:
return Live()
return Dead()