Question: Your task is to implement a slightly simplified version of the Game of Life. The only real simplification is that the grid will be of
Your task is to implement a slightly simplified version of the Game of Life. The only real simplification is that the grid will be of a fixed finite size rather than infinite. Of course, it's impossible to implement an infinite grid, so we'll use a square grid D list of size x size, with size provided as a parameter to your class constructor. I use various values in the sample output below just to save space in this document. You must implement your grid as an instance of a class. Then you will write a function that updates the grid for one step according the Game of Life rules and another function that updates the grid n successive steps. Defining the Grid Class: Each cell of the grid will contain either DEAD or LIVE. Your Grid class will have at least the methods shown in the template below. Implement your grid as a D list of strings where DEAD is the string and LIVE is the string X That way when you print the grid you can easily see where the cells are and see patterns of live cells. In my str function, I added an extra space between cells in each row so that the grid appears more square when printed. See the sample output below. One of the trickiest things about playing Game of Life on a finite grid is dealing with cells on the border. Eg how many live neighbors does cell have? It's possible to do that by programming in a bunch of special cases. But, as with many problems in programming, there's a much easier and clever way to do it Cells outside the grid border should be considered DEAD, but if you write your code carelessly it will crash as you try to access cells that aren't defined. My suggestion is to ask first if either or both of a cell's coordinates would make it fall outside the grid; if so call it DEAD. Otherwise, ask if gridrowcol contains LIVE. That way, when counting the live neighbors of cell grid you can safely ask about the "cell" at grid for example, without crashing your program. Think about this when you program the class method isCellLive. A template for the Grid class is below. For each method, replace pass with your code. LIVE X DEAD class Grid: This defines the playing surface for Conway's Game of Life.""" def initself size: Create the grid as a D list of dimensions size x size. Initially, each cell of the grid contains DEAD.""" pass def strself: Return a string to display when printing the grid. I put in an extra space between columns so that it would appear more square. Don't forget to include a newline after each row.""" pass def getGridSizeself: Return the size of the grid.""" pass def getCellself row, col: Fetch the contents of the cell at coordinates row col Assume that these are legal coordinates.""" pass def setCellself row, col, value: Set the contents of the cell at coordinates row col to value presumably this should be either DEAD or LIVE. Assume that the coordinates are legal.""" pass def isCellLiveself row, col: A cell is live if it's within the grid and contains LIVE. Otherwise, it's considered DEAD. Rather than returning a boolean, this returns or so we can also use this in counting live cells. Note that in a Boolean context, and work just as well as True and False.""" pass def countLiveNeighbors self, row, col : Count the live neighbors of the cell with coordinates row col To do this we just need to ask of each for the possible neighbors whether it's alive and count those that are. Return the count.""" pass Other Functions: In addition to the class described above, you must define several other functions that makes use of the class. These are the functions that update the grid according to the rules of the game, along with a function that makes it more convenient to set up the initial seed. These functions should be defined outside of the Grid class, but in the same file. Feel free to define auxiliary functions if you like, but you must have at least the following functions: def makeCellsLive grid, cellsList : Given a list of coordinate pairs x y make each of these LIVE in the grid.""" pass def stepGameOfLife grid : This implements one step in the Game of Life. Given a grid, update it following the rules of the games. I.e for each cell in the grid, count its live neighbors and decide whether the cell is LIVE or DEAD in the next generation. Then update the grid to reflect the changes.""" pass def playGameOfLife grid, n : Given an initial grid, take n steps in the Game of Life.""" pass Notice that in stepGameOfLife you need to define another D array to hold the results temporarily. You can't just mod
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
