Skip to content

Instantly share code, notes, and snippets.

@kyonmm
Created July 6, 2019 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyonmm/6b8e6c9f181882de50abbf0208175ef7 to your computer and use it in GitHub Desktop.
Save kyonmm/6b8e6c9f181882de50abbf0208175ef7 to your computer and use it in GitHub Desktop.
class LifeGame:
def __init__(self, width, hight):
rows = []
for x in range(width):
row = []
for y in range(hight):
row.append(0)
rows.append(row)
self.cells = rows
def print_cells(self):
for row in self.cells:
print(row)
def set_live(self,x,y):
self.cells[x][y] = 1
def next(self):
new_cells = self.cells
for x in range(len(self.cells)):
for y in range(len(self.cells[x])):
count = 0
if not ((x-1 >= len(self.cells) or x-1 < 0)or (y-1 >= len(self.cells[x-1]) or y-1 < 0)):
if self.cells[x-1][y-1]==1:
count =+ 1
if not ((x-1 >= len(self.cells) or x-1 < 0)or (y >= len(self.cells[x-1]) or y < 0)):
if self.cells[x-1][y]==1:
count =+ 1
if not ((x-1 >= len(self.cells) or x-1 < 0)or (y+1 >= len(self.cells[x-1]) or y+1 < 0)):
if self.cells[x-1][y+1]==1:
count =+ 1
if not ((x >= len(self.cells) or x < 0)or (y-1 >= len(self.cells[x]) or y-1 < 0)):
if self.cells[x][y-1]==1:
count =+ 1
if not ((x >= len(self.cells) or x < 0)or (y+1 >= len(self.cells[x]) or y+1 < 0)):
if self.cells[x][y+1]==1:
count =+ 1
if not ((x+1 >= len(self.cells) or x+1 < 0)or (y-1 >= len(self.cells[x+1]) or y-1 < 0)):
if self.cells[x+1][y-1]==1:
count =+ 1
if not ((x+1 >= len(self.cells) or x+1 < 0)or (y >= len(self.cells[x+1]) or y < 0)):
if self.cells[x+1][y]==1:
count =+ 1
if not ((x+1 >= len(self.cells) or x+1 < 0)or (y+1 >= len(self.cells[x+1]) or y+1 < 0)):
if self.cells[x+1][y+1]==1:
count =+ 1
if self.cells[x][y] == 0 and count==3:
new_cells[x][y] = 1
if self.cells[x][y] == 1 and (count == 3 or count == 2):
new_cells[x][y] = 0
if self.cells[x][y] == 1 and count<=1:
new_cells[x][y] = 0
lg = LifeGame(5,5)
lg.print_cells()
lg.set_live(1,2)
lg.set_live(2,2)
lg.set_live(3,2)
lg.print_cells()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment