Conway\'s Game of Life USE PYTHON CODE TO WRITE! In this game, you have a grid w
ID: 648587 • Letter: C
Question
Conway's Game of Life
USE PYTHON CODE TO WRITE!
In this game, you have a grid where pixels can either be on or off, and simple rules that govern whether those pixels will be on or off (dead or alive) at the next timestep. The rules are as follows:
Any live cell with fewer than two live neighbours dies
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies.
Any dead cell with exactly three live neighbours becomes a live cell.
You will ask the user for the size of the game board, and any cells they'd like on to begin with. Then ask the user how many iterations they'd like to run, and display each one of those.
Please store your board in a 2D list, and have a function called nextIteration(board) that returns a new board with the next iteration, and a function called printBoard(board) that prints out the board.
Explanation / Answer
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 (0Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.