Game of life python code9/28/2023 ![]() Should the class/function be used elsewhere, they can be accessed with the help() builtin function of Python, and allow users to read on how to use your work without reading actual code.Ĭomments should provide why things are done a certain way, if it is unclear. Now there are just a few more things left to address.įirst is documentation: while the code is simple and mostly self-documenting, it is good practice to document the code using docstrings and comments, if necessary.ĭocstrings should describe the purpose of classes and functions, and how to use them. Implementing a class, the code now looks something like this: class GameOfLife: The class should hold the game state, provide ways to act upon it and to access information about it. One way to deal with that is to encapsulate the game state into a class. You could argue that cells is still technically a global variable, as it is defined at the root level of the code. One way to fix that is to pass the variable as arguments to functions: cells = initialize() Global variables are considered bad practice for a number of good reasons, mainly because it makes it hard to keep track where in the code they are acted upon, and thus what it's value is supposed to be at a given point in the code, especially once you break the code into multiple functions. In your code, you use a global variable called nextCells. It also shows that it needs a way to keep track of the current state of the cell grid. If you want to try another way to display, for example with a graphic display instead of characters on console, you can define another function and replace just one line in the main loop to try it out. Now, if you want to work on how to display the cells, you can easily go to the relevant function's definition and work on just that. In this case, the main loop is very simple and easy to follow. Consider the following pseudocode: initialize() This is bad, as it makes the code harder to read and follow along, less maintainable and less reusable.īreaking the code into functions allows you to focus on each aspect of the code independently. What your code lacks is structure: everything happens at the top level, with no use of functions or classes. Of course, there is always room for improvement. It also mostly follows PEP8 style conventions, which is nice, although this can be improved, as it was already mentioned in the other answer. Your code works and is readable, which is nice. Time.sleep(1) # Add a 1-second pause to reduce flickering. # Dead cells with 3 neighbors become alive: # Living cells with 2 or 3 neighbors stay alive:Įlif currentCells = ' ' and numNeighbors = 3: If currentCells = '#' and (numNeighbors = 2 or # Set cell based on Conway's Game of Life rules: 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. # `% WIDTH` ensures leftCoord is always between 0 and WIDTH - 1 # Calculate the next step's cells based on current step's cells: ![]() Print() # Print a newline at the end of the row. Print(currentCells, end='') # Print the # or space. Print('\n\n\n\n\n') # Separate each step with newlines. NextCells.append(column) # nextCells is a list of column lists. Rules apply to all cells at the same time. Survival: Each live cell with either two or three live neighbors will remain alive for the next generation.Death by overcrowding: Each live cell with four or more live neighbors will die in the next generation.Death by isolation: Each live cell with one or fewer live neighbors will die in the next generation.Births: Each dead cell adjacent to exactly three live neighbors will become live in the next generation.It all depends on how many of that cell's neighbors are alive. At the heart of this game are four rules that determine if a cell is live or dead. Like any game, there are rules! Here's a quick overview. Any recommendations for cleaning this code up with maybe some additional functions? I eventually want to eliminate the keyboard interrupt and improve this code with maybe a try and except function to have the game show a message stating that it is over. Currently, this program takes about 70 lines of code in Python to return the functionality of the game, which can be simplified to fewer lines of code and it ends when a keyboard interrupt occurs. This query is a part of Conway’s game of life.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |