Simple as the Game of Life is - Conway chose the rules specifically with keeping them as simple as possible while satisfying other conditions in mind - it has proven to be an enduringly fascinating framework, basically launching the field of cellular automatons. What happens if you change random.randint(0, 1) on line 28 to random.How the earlier initial arrangement plays out over successive turns.What happens if you delete or comment out print('\n' * 50) on line 36?.What happens when you change WIDTH = 79 on line 10 to WIDTH = 7?.Experiment with some modifications to the code and rerun the program to see what effect the changes have. Try to find the answers to the following questions. Add the ability to read in the initial state from a text file, so the user can edit the starting cell states manually.Adjust the percentage of cells that start as living, instead of always using 50 percent.On your own, you can also try to figure out how to do the following: The comments marked with (!) have suggestions for small changes you can make. exit () # When Ctrl-C is pressed, end the program.Īfter entering the source code and running it a few times, try making experimental changes to it. sleep ( 1 ) # Add a 1 second pause to reduce flickering. # Everything else dies or stays dead: 85. # Dead cells with 3 neighbors become alive: 82. elif cells = DEAD and numNeighbors = 3 : 81. # Living cells with 2 or 3 neighbors stay alive: 79. if cells = ALIVE and ( numNeighbors = 2 77. # Set cell based on Conway's Game of Life rules: 76. numNeighbors = 1 # Bottom-right neighbor is alive. numNeighbors = 1 # Bottom neighbor is alive. numNeighbors = 1 # Bottom-left neighbor is alive. numNeighbors = 1 # Right neighbor is alive. numNeighbors = 1 # Left neighbor is alive. numNeighbors = 1 # Top-right neighbor is alive. numNeighbors = 1 # Top neighbor is alive. numNeighbors = 1 # Top-left neighbor is alive. # Count the number of living neighbors: 57. # Get the neighboring coordinates of (x, y), even if they 50. # Calculate the next step's cells based on current step's cells: 47. print () # Print a newline at the end of the row. print ( cells, end = '' ) # Print the # or space. print ( ' \n ' * 50 ) # Separate each step with newlines. # Each iteration of this loop is a step of the simulation. # 50/50 chance for starting cells being alive or dead. for y in range ( HEIGHT ): # Loop over every possible row. for x in range ( WIDTH ): # Loop over every possible column. # Put random dead and alive cells into nextCells: 25. # Their keys are (x, y) tuples and their values are one of the ALIVE 22. # The cells and nextCells are dictionaries for the state of the game. # (!) Try changing ALIVE to '|' and DEAD to '-'. DEAD = ' ' # The character representing a dead cell. # (!) Try changing DEAD to '.' or another character: 16. ALIVE = 'O' # The character representing a living cell. # (!) Try changing ALIVE to '#' or another character: 14. HEIGHT = 20 # The height of the cell grid. The classic cellular automata simulation. """Conway's Game of Life, by Al Sweigart 2. The cells variable’s dictionary represents the current state of the cells, while nextCells stores the dictionary for the cells in the next step in the simulation.ġ. Lines 40 to 44 are set up to print a representation of these dictionaries onto the screen. Both dictionaries have (x, y) tuples for keys (where x and y are integers), 'O' for living cells, and ' ' for dead cells. The state of the cells is stored in dictionaries in the cells and nextCells variables. When you run conwaysgameoflife.py, the output will look like this: O O OO O O More information about Conway’s Game of Life can be found at, and more information about Martin Gardner at. Tragically, Professor Conway passed away of complications from COVID-19 in April 2020. There is a large body of research regarding the patterns that these simple rules produce. The cells don’t “remember” any older states. The living or dead state of the cells in the next step of the simulation depends entirely on their current state. Any other cell dies or stays dead in the next step of the simulation.Dead cells with exactly three neighbors become alive in the next step of the simulation.Living cells with two or three neighbors stay alive in the next step of the simulation.Today, it’s a favorite among programmers and computer scientists, though it’s more an interesting visualization than a true “game.” The two-dimensional board has a grid of “cells,” each of which follows three simple rules: It was invented by mathematician John Conway in 1970 and popularized by Martin Gardner’s “Mathematical Games” column in Scientific American. Conway’s Game of Life is a cellular automata simulation that follows simple rules to create interesting patterns.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |