Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Background The Game of Life was invented by John Conway, who is currently a prof

ID: 3740907 • Letter: B

Question

Background
The Game of Life was invented by John Conway, who is currently a professor of mathematics at Princeton. It’s not a game in the traditional sense, but rather a grid of cells that changes over time according to a few simple rules.

At a given point in time, each cell in the grid is either “alive” (represented by a value of 1) or “dead” (represented by a value of 0). The neighbors of a cell are the cells that immediately surround it in the grid.

Over time, the grid is repeatedly updated according to the following five rules:

All cells on the outer boundary of the grid remain fixed at 0.

An inner cell that has fewer than 2 alive neighbors dies (because of loneliness).

An inner cell that has more than 3 alive neighbors dies (because of overcrowding).

An inner cell that is dead and has exactly 3 alive neighbors comes to life.

All other cells maintain their state.

Although these rules seem simple, they give rise to complex and interesting patterns! You can find more information and a number of interesting patterns here.

Getting Started
In the ps7twoD folder that you used for Problem 3, you should find a file named ps7pr4.py. (Note that this is not the same file that you used for Problem 3.) Open ps7pr4.py in IDLE, and put your work for this problem in that file. Once again, you should not move any of the files out of the ps7twoD folder.

IMPORTANT: We have included an import statement at the top of ps7pr4.py that imports all of the functions from your ps7pr3.py file. Therefore, you will be able to call any of the functions that you wrote for Problem 3.

Your tasks

Write a function count_neighbors(cellr, cellc, grid) that returns the number of alive neighbors of the cell at position [cellr][cellc] in the specified grid. You may assume that the indices cellr and cellc will always represent one of the inner cells of grid, and thus the cell will always have eight neighbors.

Here are two possible approaches that you could take to this problem:

first approach: You could use nested loops to compute the necessary cumulative sum. Set the ranges of the two loops so that you end up examining only the specified cell and its eight neighbors. And although these loops will end up including the specified cell, you should make sure that you don’t actually count the cell itself as one of its own neighbors!

OR

second approach: You could write eight separate if statements to check each of the eight neighbors. If you take this approach, you will not need to use any loops.

To test your function, run the file in IDLE and enter the following from the Shell:

Now let’s implement the rules of the Game of Life!

A given configuration of the grid in the Game of Life is known as a generation of cells. Write a function next_gen(grid) that takes a 2-D list called grid that represents the current generation of cells, and that uses the rules of the Game of Life (see above) to create and return a new 2-D list representing the next generation of cells.

Notes/hints:

Begin by creating a copy of grid (call it new_grid). Use one of the functions that you wrote for the previous problem!

Limit your loops so that they never consider the outer boundary of cells. You did this already in one of the functions that you wrote for the previous problem.

You can use the count_neighbors() function that you just wrote. Make sure that you count the neighbors in the current generation (grid) and not the new one (new_grid).

When updating a cell, make sure to change the appropriate element of new_grid and not the element of grid.

Testing next_gen
Here are two patterns that you might want to take advantage of when testing your function:

If a 3x1 line of alive cells is isolated in the center of a 5x5 grid, the line will oscillate from vertical to horizontal and back again, as shown below:

and so on!

In a 4x4 grid, if the inner cells are all alive, they should remain alive over time:

Optional (but encouraged): graphical life!
We have given you functions that you can use to run a graphical version of the Game of Life. Here’s an example of how to use it with a random starting configuration:

The following key presses can be used to control the simulation:

Enter (or Return): begin/resume the simulation

P: pause the simulation

Spacebar: clear the grid

Q: quit the simulation

When the simulation is paused, you should be able to change the state of a cell by clicking on it.

Trying other patterns
The Life Lexicon is a website that includes many examples of interesting starting patterns. They use a different representation for the cells in a grid: a dot . character for a dead cell and an O character for an alive cell. For example, here is a well-known pattern known as the Gosper glider gun:

You can easily try a pattern that is specified in Life Lexicon form by using the read_patternfunction. It takes 2 inputs specifying the amount of padding (i.e., the number of empty rows and empty columns) that should be added around a pattern that is entered by the user. When you call this function, it waits for you to enter a pattern in the form found in the Life Lexicon, and it converts it into a 2-D list of the form that our functions use.

For example:

When you are prompted to enter the pattern, copy and paste it into the Shell window and hit Enter. (It’s not a problem if you have extra spaces at the start of some of the lines when you paste the pattern.) You can then run the pattern graphically as indicated above.

Colors
You can also change the colors used for the alive and dead cells. For example, to invert the default color scheme–producing something that is more obviously Terrier-themed!–you can do the following from the Shell before calling show_graphics:

A full list of color names is available here.

Explanation / Answer

Code

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote