Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help coding this in python, any explanations would be welcomed. Conway's Game of Life John Conway was a brilliant mathematician who spent much of

Need help coding this in python, any explanations would be welcomed.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Conway's Game of Life John Conway was a brilliant mathematician who spent much of his career at Princeton University. One of his (many) contributions to was to create a "game" called Life. In reality though, it isn't a game but a cellular automaton, an artificial life simulation. The simulation takes place on a grid, and each space on the grid can be turned on off (considered "alive" or "dead"). There are many cellular automaton; Conway's operates by the following rules: - if a cell has less than 2 neighbors it dies from underpopulation - if a cell has more than 3 neighbors it dies from overpopulation - if a cell is dead but has exactly 3 neighbors, it is "born", or brought back to life. In this project we will recreate Conway's game, but with a few added twists: - a cell is dead if no light emits from it - i.e. its color code is (0,0,0) using the RGB scale (see note below if you don't know what this is) - any color other than (0,0,0) is alive - when a cell is "born" from having exactly three neighbors, it is created as the average color of its neighbors - 1% of the time (when all cells update, not particularly when one is born), a random mutation will occur whereby some random cell on the board becomes a random color Note: The RGB system for colors represents colors as a combination of red, green, and blue light values. Color values range from 0255, with (0,0,0) being no light (completely dark black), and (255,255,255) being completely bright white. GVSU blue for instance, is (0,101,164). Objectives This project's objectives are intended to show you are ready for 163 . In particular, this project will cover: - creating a new project in your IDE - adding existing code to your project - creating a simple class - understanding instance variables - understanding methods including constructors - working with lists - writing simple conditional statements You will be given partial code for this project and will need to complete it to make the simulation work. The following steps will guide you through this process. This is the simulation you will be creating. The GUI is ready to go; you will need to create the class that handles the world. The world is represented by the grid of squares. A black cell is dead, while a cell with any color in it is alive. Completion Steps Here is the basic outline of what you need to complete. In the next section we will provide hints and tips. 1. Create a new project in PyCharm. 2. Add the libraries pygame and pygame-gui to the project by using the "Python Packages" tab. 3. Add the theme.json, main.py, and game.py files from Blackboard to your project directory. 4. The game.py file already includes a class called Game. This class will handle the display. Look over it, but do not change it. 5. Add a new class, Board to the game.py file. Again, be careful not to modify the Game class. The Board class needs the following data and methods: A constructor that takes a size parameter (an int), and creates a size X size (two-dimensional) instance variable called_board, and initializes each list element to the tuple (0,0,0). Create another instance variable named prior and set it equal to copy. deepcopy (self._board) . A method get_board that will return the instance of board. This will be used by the GUI to know what colors to use when drawing the cells. A method change_color (self, i, j). The parameters i and j are the row and column of a cell on the board. This method should use the random. randint function to pick a new random color by picking three random integers from 0-255. Store the values in the board as a tuple, i.e. board [row] [column ]=(r,g,b), where r,g, and b are the random integers chosen. If you don't recall what a tuple is, see "Hints, Tips, and Review" section of this document. A method called count_neighbors (self, i, j). This method takes as a parameter a cell location and counts the number of neighbors that cell has. A cell may have up to 8 neighbors. In addition to counting the number of neighbors, this function must determine the average color of the neighbors. The function will return the number of neighbors and the average neighbor color. For instance, if your variables are called num_neighbors and average_color, the last line of your function would be return num_neighbors, average_color. Be careful! When computing the average, if the cell has no neighbors we don't want to attempt to divide by 0. A method called update. This method is the core of the world simulation, and will update each cell based on the rules described at the top of this document. The very first thing we want to do in this function is to copy our board; we do this because changes to a cell should be entirely decided by the state of the last generation. If we modify our current board and also use it to determine the state of our new generation, we won't accurately create or kill cells. This is the purpose of the prior variable; we set it equal to the last generation while we compute the new generation of cells. Use the code copy.deepcopy (self._board) to copy the current state of the board into_prior. Then, loop through all the cells of prior and set each cell of board according to our rules. The structure of this function will have the copy, then a loop in a loop that looks over each cell. For each cell we will call the count neighbors function and store the returned number and average color. Then, we check if the cell should live or die based on our rules. Outside of the loop, we will simulate a mutation by picking a random integer between 1 and 100 . If that number comes out to be 42, our code will pick a random row and column value within the bounds of the board and call the change_color function with those values passed in. Hints, Tips, and Review This project is very small, but relies upon a solid foundation from 162. Here are some things you might need to review, remember, or maybe even see for the first time (perhaps you missed a class in the past). Never Start Programming Immediately The most common mistake we see with newer programmers is they want to jump straight into coding. This is dumb. You wouldn't build a house without a plan first, you wouldn't try to fix a car without diagnosing the problem first, and you wouldn't start cooking without a recipe. There is no reason to start coding a project until you completely understand the problem you are trying to solve. In general, you should spent about 90% of your time understanding the problem and 10% of your time coding the solution. Draw pictures, write pseudocode, print out the assignment and make notes, write down questions you have before you come for help. If you feel helpless and confused about a project it does not mean you aren't cutout for coding - it means you aren't applying the correct problem solving methods. Python Return Statements Most programming languages only allow returning a single object from a function. Python allows us to simulate sending multiple objects. In reality all Python does it pack our multiple objects together into a tuple. In the code that calls the function with "multiple" return values, Python allows us to easily unpack: import math def get_circle_props(radius) : perim =2 * math.pi * radius aarea = math.pi * radius * radius return perim, area perimeter, area = get_circle_props (10) So now we have both values stored - each in its own variable. If we didn't want to use one of the values we could have used the underscore to ignore it, i.e. _ area = get_circle_props (20) would ignore the perimeter value. Tuples In our code we will need to see if a cell is alive or dead when we are counting neighbors. A dead cell merely has the color value (0,0,0). We can directly check this in a conditional statment: if self._board[row] [col]==(0,0,0) : ... do something ... Remember that working with a tuple is just like working with a list - with the exception that tuples can't be changed once you make them. So we can check the individual values: my_tuple =(0,127,255) if my_tuple [1]== my_tuple[2] //2 : do something ... Multidimensional Loops Sometimes we need a loop inside of a loop. For instance, let's say we are trying to make a multiplication table: \# Create a blank list mult table = [] for row in range(1, 13): \# Add a new empty row mult table. append([]) for col in range(1, 13 ): Addthemultipliedvaluetotherow. Notewehavetouserow-1sincelistsareindexedfrom0 mult table [row - 1 ] append(row * col) Now, if we wished to print the board we could do either of the following: for row in mult table: for val in row: print (str(val), end \t) print() Or, if we needed to keep track of the row and column number, we could use: for row in range (0,12): for col in range (0, 12) : print (mult_table [row] [col], end=' \t) print()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Shouhong Wang, Hai Wang

1st Edition

1612330150, 978-1612330150

More Books

Students also viewed these Databases questions

Question

Explain the steps involved in training programmes.

Answered: 1 week ago

Question

What are the need and importance of training ?

Answered: 1 week ago

Question

9. Describe the characteristics of power.

Answered: 1 week ago

Question

10. Describe the relationship between communication and power.

Answered: 1 week ago