Last active
May 18, 2022 14:52
-
-
Save holmberd/22c55e9ae7e6507bfb9af002a639dc55 to your computer and use it in GitHub Desktop.
Game of life Python [1]
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
CELL_ALIVE_CHAR = '*' | |
CELL_DEAD_CHAR = '.' | |
class Location: | |
def __init__(self, row_index, col_index): | |
self.row = row_index | |
self.col = col_index | |
class Cell: | |
def __init__(self, state): | |
self.state = state | |
def isAlive(self): | |
return self.state | |
class World: | |
def __init__(self, board = list()): | |
self.validate_board(board) | |
self.board = board | |
self.rows = len(board) | |
self.cols = len(board[0]) | |
self.generation = 0 | |
self.rule = { | |
'born': [3], | |
'survival': [2, 3], | |
} | |
def validate_board(self, board): | |
if not len(board): | |
raise ValueError('Board must contain cells') | |
row_length_set = set(map(lambda row: len(row), board)) | |
if len(row_length_set) > 1: | |
raise ValueError('Rows must be of the same length.') | |
def get_cell(self, location): | |
return self.board[location.row][location.col] | |
def evolve(self): | |
self.board = self.evolve_board(self.board) | |
self.generation += 1 | |
return self | |
def evolve_board(self, board): | |
return list(map(self.evolve_row, enumerate(board))) | |
def evolve_row(self, args): | |
(row_index, row) = args | |
return [self.evolve_cell(Location(row_index, col_index), cell) for col_index, cell in enumerate(row)] | |
def evolve_cell(self, location, cell): | |
alive_neighbours = self.count_alive_neighbours(location) | |
if cell.isAlive(): | |
return Cell(any([survive_count == alive_neighbours for survive_count in self.rule['survival']])) | |
return Cell(any([born_count == alive_neighbours for born_count in self.rule['born']])) | |
def count_alive_neighbours(self, location): | |
cell_neighbour_positions = [[0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1]] | |
count = 0 | |
for [rowPos, colPos] in cell_neighbour_positions: | |
neighbour_location = Location(location.row + rowPos, location.col + colPos) | |
if (self.in_bounds(neighbour_location) and self.get_cell(neighbour_location).isAlive()): | |
count += 1 | |
return count | |
def in_bounds(self, location): | |
if location.row > (self.rows - 1) or location.row < 0: | |
return False | |
if location.col > (self.cols - 1) or location.col < 0: | |
return False | |
return True | |
def create_world(board_scheme): | |
board = create_board(board_scheme) | |
return World(board) | |
def create_board(board_scheme): | |
board = get_board_rows(board_scheme) | |
return list(map(create_row_cells, board)) | |
def get_board_rows(board_scheme): | |
return board_scheme.split('\n')[:-1] | |
def create_row_cells(row): | |
return list(map(convert_char_to_cell, list(row))) | |
def convert_char_to_cell(char): | |
return Cell(char == CELL_ALIVE_CHAR) | |
def print_world(world): | |
s = '' | |
for row in world.board: | |
for cell in row: | |
s += CELL_ALIVE_CHAR if cell.isAlive() else CELL_DEAD_CHAR | |
s += '\n'; | |
print(s) | |
def test(): | |
board_scheme = '........\n' + '........\n' + '...***..\n' + '........\n' + '........\n' + '........\n' | |
world = create_world(board_scheme); | |
print_world(world) | |
world.evolve() | |
print_world(world) | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment