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 (2D 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 2D 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. E.g., how many live neighbors does cell (0,0) 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 grid[row][col] contains LIVE. That way, when counting the live neighbors of cell grid[0][0] you can safely ask about the "cell" at grid[-1][-1], 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 __init__(self, size): """Create the grid as a 2D list of dimensions size x size. Initially, each cell of the grid contains DEAD.""" pass def __str__(self): """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 getGridSize(self): """Return the size of the grid.""" pass def getCell(self, row, col): """Fetch the contents of the cell at coordinates (row, col). Assume that these are legal coordinates.""" pass def setCell(self, 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 isCellLive(self, 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 0 or 1 so we can also use this in counting live cells. Note that in a Boolean context, 1 and 0 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 8 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 2D 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!