-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife.py
108 lines (95 loc) · 4.54 KB
/
life.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python3
# -*- Coding: utf-8 -*-
# Author - Heera Hemanth Bylla < heerahemanth1@icloud.com >
# Implements the LifeGrid ADT for use with the Game of Life
from marray import Array2D
class LifeGrid:
""" The LifeGrid is an Abstract Data Type(ADT) for the implementation
of the GameOfLife. It handles the game grid and it's functions.
This class is imported into the GameOfLife and used directly.
"""
# Defines constants to represents the cell states.
DEAD_CELL = 0
LIVE_CELL = 1
# Creates the game grid and initializes the cells to dead.
def __init__(self, numRows, numCols):
# Allcoate the 2D Array for the grid.
self._grid = Array2D(numRows, numCols)
# Clear the grid and set all values to dead.
self.configure(list())
# Returns the number of rows in the grid.
def numRows(self):
return self._grid.numRows()
# Returns the number of columns in the grid.
def numCols(self):
return self._grid.numCols()
# Configures the grid to contain the given live cells.
def configure(self, coordList):
# Clear the game grid.
for i in range(self.numRows()):
for j in range(self.numCols()):
self.clearCell(i, j)
# Set the indicated cells to be live.
for coord in coordList:
self.setCell(coord[0], coord[1])
# Does the indicated cell contain a live organism?
def isLiveCell(self, row, col):
return self._grid[row, col] == LifeGrid.LIVE_CELL
# Clears the indicated cell by setting it to dead.
def clearCell(self, row, col):
self._grid[row, col] = LifeGrid.DEAD_CELL
# Sets the indicated cell to be alive.
def setCell(self, row, col):
self._grid[row, col] = LifeGrid.LIVE_CELL
# Returns the number of live neighbors for a given cell.
def numLiveNeighbors(self, row, col):
if row==0 and col==0:
live_neighbors_sum = self._grid[row, col+1]\
+ self._grid[row+1, col]\
+ self._grid[row+1, col+1]
elif row==self.numRows()-1 and col==0:
live_neighbors_sum = self._grid[row-1, col]\
+ self._grid[row-1, col+1]\
+ self._grid[row, col+1]
elif col == 0:
live_neighbors_sum = self._grid[row-1, col]\
+ self._grid[row-1, col+1]\
+ self._grid[row, col+1]\
+ self._grid[row+1, col]\
+ self._grid[row+1, col+1]
elif row==0 and col==self.numCols()-1:
live_neighbors_sum = self._grid[row, col-1]\
+ self._grid[row+1, col-1]\
+ self._grid[row+1, col]
elif row==self.numRows()-1 and col==self.numCols()-1:
live_neighbors_sum = self._grid[row-1, col-1]\
+ self._grid[row-1, col]\
+ self._grid[row, col-1]
elif col == self.numCols()-1:
live_neighbors_sum = self._grid[row-1, col-1]\
+ self._grid[row-1, col]\
+ self._grid[row, col-1]\
+ self._grid[row+1, col-1]\
+ self._grid[row+1, col]
elif row == 0:
live_neighbors_sum = self._grid[row, col-1]\
+ self._grid[row, col+1]\
+ self._grid[row+1, col-1]\
+ self._grid[row+1, col]\
+ self._grid[row+1, col+1]
elif row == self.numRows()-1:
live_neighbors_sum = self._grid[row-1, col-1]\
+ self._grid[row, col+1]\
+ self._grid[row, col-1]\
+ self._grid[row-1, col]\
+ self._grid[row-1, col+1]
else:
live_neighbors_sum = self._grid[row-1, col-1]\
+ self._grid[row-1, col]\
+ self._grid[row-1, col+1]\
+ self._grid[row, col-1]\
+ self._grid[row, col+1]\
+ self._grid[row+1, col-1]\
+ self._grid[row+1, col]\
+ self._grid[row+1, col+1]
return live_neighbors_sum