# Conway’s Game of Life

For this assignment, you will write a program to simulate one tick in a restricted version of Conway’s Gameof Life.
In Conway’s Game of Life, cells in a grid are used to simulate biological cells. Each cell is considered to beeither alive or dead. At each step of the simulation, each cell’s current status and number of living neighbors isused to determine the status of the cell during the following step of the simulation.
In the one-dimensional version of the Game that you will implement, there are N cells, numbered 0 throughN-1 – where N is input by the user. The user also inputs the initial status of each cell: 1 if the cell is alive and0 if the cell is dead.
The number of cells does not change at any point in the simulation. Each cell i is adjacent to cells i-1 and i+1.
Here, the indices are taken modulo N meaning cells 0 and N-1 are also adjacent to each other. At each step ofthe simulation, the following cell status transition rules apply:
– Cells with exactly one living neighbor change their status (alive cells become dead, dead cells becomealive)
– Cells with both neighbors in the same status keep their status (alive cells remain alive, dead cells
For example, consider the following status for a populationwith 8 cells:
– Cells 0 and 6 have two living neighbors, so they don’t change their status (= remain dead).
– Cells 5 and 7 have no living neighbors, so they don’t change their status either (= remain alive).
– Cells 1 and 2 are alive (1) and have one living neighbor, so they die.
– Cells 3 and 4 are dead (0) and have one living neighbor, so they become live cells.
Thus, at the next step of the simulation, the status of thecell population will be:
From the initial cell population status, input by the user, the first generation is created by applying the aboverules simultaneously to every cell – births and deaths occur simultaneously, and the discrete moment at whichthis happens is called a tick. In other words, each generation is a pure function of the preceding one. Therules continue to be applied repeatedly to create further generations.
Your task for this assignment is to simulate one single tick of the restricted Game of Life. Here are the bigparts of the program:
1. Ask the user to input the cell population size; this should be a number between 6 and 10 (both
inclusive). If the number input is not in this range, then the program prints “Invalid population size” onthe console and ends right there (use the return statement to end the program).

 0 1 1 0

0 1 0 1
Index 0 1 2 3 4 5 6 7

 0 0 0 1

1 1 0 1
Index 0 1 2 3 4 5 6 7
2. Use a loop statement to ask the user to input the status of each cell in the population (0 for dead,
and 1 for alive). Create a list of integers with the status of each cell in the population. For the example onthe previous page, your program should create the list [0, 1, 1, 0, 0, 1, 0, 1].
3. Print the list showing the status of the cells in this original population.
4. Execute one tick of the Game of Life for the list of cells that you built so far. For this, create a loopthat:
– For each cell status in the list, it checks the status of its neighbor cells and determines if that cell’s
status should change or not; the status of the cells in the next generation should be recorded into a
new list.
– Keeps a count of how many cells transition to a new status and of how many keep their original
status. In the example on the previous page, 4 cells changed their status (the ones highlighted), and 4
remain in the same status.
5. Print the list showing the status of the cells in this new population.
6. Print the number of cells that changed status and the number of cells that preserved their status.
7. Programming style

Here are some pictures from one run of the program:

Solution

#A Game of Life simulation

#importing sys to exit via a return in case of a wrong size
import sys

#Gets the size of the population from the user and if it is not between 6 and 10 (inclusive)
# it returns a sys.exit with the error message “Invalid population size”
def size ():
showInformation(“You will now enter the size of the cell populaton you want (between 6 and 10 inclusive)”)
x = int(requestNumber(“Enter the size of the cell populaton you want (between 6 and 10 inclusive)”))
if x >= 6 and x <= 10:
return x
else:
return sys.exit(“Invalid population size”)

#size: the size of the population to be entered
#Takes input of 0 or 1 from the user for every cell
#0 indicating a dead cell
#1 indicating an alive cell
#Returns a list with a status for every cell
ls = []
for i in range(size):
ls.append(int(requestIntegerInRange(“Enter 0 to express that the cell number “+ str(i) +    “is dead or 1 to express that the cell number ” + str(i) + ” is alive”,0,1)))
return ls

#oldCells:the list containing the old cells
#checks the status of the neighbors of every cell
#(special handling for when it reaches the last item in the list)
#if the status one of them is 1 (alive) (True) and the other is 0 (dead) (False)
#it adds a 1 to to the list “new”,otherwise adds 0.
#Returns the list “new” which is binary (1’s indicating change, 0’s indicating no change)
def change(oldCells):
new = []
for i in range(len(oldCells)):
try:
if (oldCells[i+1] and not oldCells[i-1]) or (oldCells[i-1] and not oldCells[i+1]):
new.append(1)
else:
new.append(0)
except:
if (oldCells[0] and not oldCells[i-1]) or (oldCells[i-1] and not oldCells[0]):
new.append(1)
else:
new.append(0)
return new

#oldCells:a list of the original population’s status
#new:a list of 0’s and 1’s showing which cells to be changed in the oldCells
#Does a single tick of Game of Life
#it checks for every number in the list “new”
#for every number in “new” that is 1 (will change) (True)
#it inverts corresponding number in the original population list (from 0 to one and viseversa)
#Returns the updated old cells
def game(oldCells,new):
old = oldCells
for i in range(len(new)):
if new[i]:
old[i] = not oldCells[i]
return old

#tests the program