Question
Creat more patterns in Conway's Game of Life Please help by using python 2.7x copy A Each of updating functions so far creates a new set of cells without regard to an old generation that it might depend on. Conway's game of life, on the other hand, follows a set of cells by changing one generation into the next. To see why copy( A commands: is a crucial helper function for this process, try the following >>> oldA-createBoard (2,2) # create a 2x2 empty board >>>printBoard(oldA) # show it # create a false ("shallow") copy # show it >>printBoard (newA) >>> OldA [ 0 ] [ 0 ] -1 >> printBoard (oldA) 10 # set OldA's upper left corner to 1 # the upper left will be 1 >> printBoard (newA) 10 # but newA has changed, too!! Here we have made a copy of oldA, where we have named the copy newA. However, newA is simply a copy to the reference to the original data in oldA! As a result, when oldA's data changes, so does newA's data, even though we never touched newA's data! The above example shows shallow copying: the copying of a reference to data, rather than making a full copy of all of the data. Making a full copy of all of the data is called deep copying. For this part of the assignment, write a function named copy A, which will make a deep copy of the 2d array A. Thus, copy wl take in a 2d array A and will output a new 2d array of data that has the same pattern as the input array. To make sure you output new data, use createBoard to get a brand new array of the data in the computer's memory, but it will have a deep copy of that data. Make sure your copy function is working properly with this example >> oldAcreateBoard(2,2) >> printBoard (oldA) >>> newA = copy ( oldA ) >> printBoard (newA) >>> OldA [ 0 ] [0] -1 >> printBoard (oldA) 10 >> printBoard (newA)
Explanation / Answer
Code: class Game(object): def __init__(self, state, infinite_board = True): self.state = state self.width = state.width self.height = state.height self.infinite_board = infinite_board def step(self, count = 1): for generation in range(count): new_board = [[False] * self.width for row in range(self.height)] for y, row in enumerate(self.state.board): for x, cell in enumerate(row): neighbours = self.neighbours(x, y) previous_state = self.state.board[y][x] should_live = neighbours == 3 or (neighbours == 2 and previous_state == True) new_board[y][x] = should_live self.state.board = new_board def neighbours(self, x, y): count = 0 for hor in [-1, 0, 1]: for ver in [-1, 0, 1]: if not hor == ver == 0 and (self.infinite_board == True or (0