Last active
March 25, 2019 19:36
-
-
Save Joseph94m/a638fe44b30b513901789519703872d8 to your computer and use it in GitHub Desktop.
Sparse representation of the game of life
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
class SparseSetState(State): | |
def __init__(self, grid): | |
self.grid = grid | |
def copy(self): | |
return SparseSetState(copy(self.grid)) | |
def get_neighbours(self, elem, max_size): | |
#Returns the neighbours of a live cell if they lie within the bounds of the grid specified by max_size | |
l = [] | |
if elem[0]-1 >= 0: | |
l.append((elem[0]-1, elem[1])) | |
if elem[0]-1 >= 0 and elem[1]-1 >= 0: | |
l.append((elem[0]-1, elem[1]-1)) | |
if elem[0]-1 >= 0 and elem[1]+1 < max_size: | |
l.append((elem[0]-1, elem[1]+1)) | |
if elem[1]-1 >= 0: | |
l.append((elem[0], elem[1]-1)) | |
if elem[1]-1 >= 0 and elem[0]+1 < max_size: | |
l.append((elem[0]+1, elem[1]-1)) | |
if elem[1]+1 < max_size: | |
l.append((elem[0], elem[1]+1)) | |
if elem[0]+1 < max_size: | |
l.append((elem[0]+1, elem[1])) | |
if elem[1]+1 < max_size and elem[0]+1 < max_size: | |
l.append((elem[0]+1, elem[1]+1)) | |
return l | |
def equals(self, other): | |
if other is None: | |
return False | |
return self.grid == other.grid | |
def apply_rules(self, rules, max_size): | |
#Calls the actual rules and provides them with the grid and the neighbour function | |
self.grid = rules.apply_rules(self.grid, max_size,self.get_neighbours) | |
return self |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment