https://research.cs.queensu.ca/home/burtonma/CISC121/A2/A2.html

1 answer below »
https://research.cs.queensu.ca/home/burtonma/CISC121/A2/A2.html
Answered Same DayMar 08, 2021

Answer To: https://research.cs.queensu.ca/home/burtonma/CISC121/A2/A2.html

Vibhav answered on Mar 09 2021
138 Votes
Life game assignment/1.JPG
Life game assignment/2.JPG
Life game assignment/3.JPG
Life game assignment/life1.py
'''
This module provides functions for the 1-dimensional Game of Life.
The 1-dimensional Game of Life occurs on a list of cells where each
cell is either alive or dead. The population of cells
evolves from
one generation to the next according to three rules:
1. Any live cell survives into the next generation if it has 2 or 4
living neighbours in its 2-neighborhood.
2. Any dead cell becomes alive in the next generation if it has 2 or 3
living neighbours in its 2-neighborhood.
3. All other live cells die and all other dead cells remain dead.
The rules are applied to each cell simultaneously to compute the
next generation of cells.
The 2-neighborhood of a cell consists of the cell itself and its
two neighbors to the left and two neighbors to the right of the cell
(if those neighbors exist).
'''
def make_cells(n, val):
'''
Return a list filled with a specified value.
Parameters
----------
n : int
The number of elements in the returned list.
val : bool
The element to appear repeatedly in the returned list.
Returns
-------
list
A list of `n` copies of `val`
Raises
------
ValueError
If `n` is less than zero.
'''

if n < 0:
raise ValueError('make_cells() number of elements less than zero, n = ' + str(n))
a = [val] * n
return a
def print_cells(cells):
'''
Returns: Nothing
Parameter: cells is a list of boolean values

'''
for cell in cells:
if cell:
print('#', end='')
else:
print('-', end='')
print()
def neighborhood(cells, index):
'''
Returns: List of neighbors for index
Parameter:
cells: List of boolean values
index: Index for which neighbor is sought

'''
left_index = index - 2
right_index = index + 2
#Check for left or right bounds
if left_index < 0:
left_index = 0
if right_index >= len(cells):
right_index = len(cells)-1

neighbors = []
for i in range(left_index, right_index+1):
neighbors.append(cells[i])
return neighbors
def evolve(cells):
'''
Returns: Nothing
Parameter: List of boolean values indicating live/dead cells

'''
#Make a copy of cells
copy_cells = [b for b in cells]
for i in range(len(copy_cells)):
neighbors = neighborhood(copy_cells, i)
live_count = 0
for n in neighbors:
if n:
live_count += 1

#For live cell
if copy_cells[i]==True:
if live_count==3 or live_count==5:
#It will remain live, do nothing
pass
else:
#It will become dead
cells[i] = False
#For dead cell
else:
if live_count==2 or live_count==3:
#It will become...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here