Creat more patterns in Conways Game of Life Please help by u
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) Solution
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 <= x + hor < self.width and 0 <= y + ver < self.height)): count += self.state.board[(y + ver) % self.height][(x + hor) % self.width] return count def display(self): return self.state.display() class State(object): def __init__(self, positions, x, y, width, height): active_cells = [] for y, row in enumerate(positions.splitlines()): for x, cell in enumerate(row.strip()): if cell == \'o\': active_cells.append((x,y)) board = [[False] * width for row in range(height)] for cell in active_cells: board[cell[1] + y][cell[0] + x] = True self.board = board self.width = width self.height = height def display(self): output = \'\' for y, row in enumerate(self.board): for x, cell in enumerate(row): if self.board[y][x]: output += \' o\' else: output += \' .\' output += \'\ \' return output glider = \"\"\" oo. o.o o.. \"\"\" my_game = Game(State(glider, x = 2, y = 3, width = 10, height = 10)) print my_game.display() my_game.step(27) print my_game.display()