-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameoflife.py
78 lines (69 loc) · 3.44 KB
/
gameoflife.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#This is an implementation of Conway's Game of Life (CGoL) by Copeland Corley.
from random import choice
class Cell:
def __init__(self):
self.isAlive = choice([True, False])
self.neighbor = {}
#spawn cell and increment its neighbors' number of living neighbors
def spawn(self):
for cell in self.neighbor:
self.neighbor[cell].livingNeighbors += 1
self.isAlive = True
#kill cell and decrement its neighbors' number of living neighbors
def die(self):
for cell in self.neighbor:
self.neighbor[cell].livingNeighbors -= 1
self.isAlive = False
class GameOfLife:
def __init__(self, size):
self.size = size
self.initCells()
#initialize array of cells
def initCells(self):
self.cells = [Cell() for x in range(self.size ** 2)]
cells = self.cells
size = self.size
for i in range(size ** 2):
thisCell = self.cells[i]
#establish top, left, bottom, and right neighbor relationships between cells using neighbor-finding expressions
thisCell.neighbor['top'] = cells[(size*(size-1)+i) % (size*size)]
thisCell.neighbor['left'] = cells[size*((i//size)+1) - (size-i%size)%size -1]
thisCell.neighbor['bottom'] = cells[(i+size)%(size*size)]
thisCell.neighbor['right'] = cells[size*(i//size) + (i%size+1)%size]
#start counting living neighbors
thisCell.livingNeighbors = thisCell.neighbor['top'].isAlive + thisCell.neighbor['left'].isAlive + \
thisCell.neighbor['bottom'].isAlive + thisCell.neighbor['right'].isAlive
for i in range(size ** 2):
thisCell = cells[i]
#establish topleft, bottomleft, bottomright, and topright neighbor relationship between cells
thisCell.neighbor['topleft'] = thisCell.neighbor['left'].neighbor['top']
thisCell.neighbor['bottomleft'] = thisCell.neighbor['left'].neighbor['bottom']
thisCell.neighbor['bottomright'] = thisCell.neighbor['right'].neighbor['bottom']
thisCell.neighbor['topright'] = thisCell.neighbor['right'].neighbor['top']
#finish counting living neighbors
thisCell.livingNeighbors += thisCell.neighbor['topleft'].isAlive + thisCell.neighbor['bottomleft'].isAlive + \
thisCell.neighbor['bottomright'].isAlive + thisCell.neighbor['topright'].isAlive
#move one step forward in the simulation
def step(self):
neighborsCopy = [i.livingNeighbors for i in self.cells]
for i in range(self.size ** 2):
thisCell = self.cells[i]
livingNeighbors = neighborsCopy[i]
if not (2 <= livingNeighbors <= 3) and thisCell.isAlive:
thisCell.die()
elif livingNeighbors == 3 and not thisCell.isAlive:
thisCell.spawn()
#return string representing grid of cells in simulation, o = live cell, _ = dead cell
def toString(self):
cellsString = ""
for i in range(self.size ** 2):
cellsString += "o " if self.cells[i].isAlive else "_ "
if((i + 1) % self.size == 0):
cellsString += "\n"
return cellsString
###uncomment to test
##x = GameOfLife(25)
##print(x.toString())
##for i in range(100):
## x.step()
## print(x.toString())