python code for conway\'s game of life For this project, you will be coding a si
ID: 3760882 • Letter: P
Question
python code for conway's game of life
For this project, you will be coding a simple cellular automata game, called Conway's Game of Life. In this game, you have a grid where pixels can either be on or off (dead or alive). In the game, as time marches on, there are simple rules that govern whether each pixel will be on or off (dead or alive) at the next time step. These rules are as follows: Any live cell with fewer than two live neighbors dies. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies. Any dead cell with exactly three live neighbors becomes a live cell.
To begin, you will ask the user for the size of the game board (rows first, then columns). Next prompt them for any cells they would like to be “alive” when the game begins. Finally, ask the user how many iterations of the game (number of time steps) they would like to see run. You should then display these iterations. “Live” cells are to be represented with the character “A” (capital ‘a’), and “dead” cells are to be represented with the character “.” (a period).
Explanation / Answer
import os
import time
def conwaygame():
gameboard[12][11] = "A"
gameboard[12][12] = "A"
gameboard[12][13] = "A"
def gamedisplay():
for row in gameboard:
print("".join(row))
def conwayupdate():
for row in range(0, len(gameboard)):
for column in range(0, len(gameboard[row])):
neighbors11 = 0
for r11 in range(-1, 2):
for c11 in range(-1, 2):
if (r11 != 0) or (c11 != 0):
try:
if gameboard[r11][c11] == "A":
neighbors11 += 1
except IndexError:
pass
if gameboard[row][column] == ".":
if neighbors11 == 3:
temp_board[row][column] = "A"
elif gameboard[row][column] == "A":
if neighbors11 < 2:
temp_board[row][column] = ". "
elif neighbors11 == 2 or neighbors11 == 3:
pass
elif neighbors11 > 3:
temp_board[row][column] = ". "
HEIGHT = 26
WIDTH = 26
gameboard = [[" " for column in range(0, WIDTH)] for row in range(0, HEIGHT)]
temp_board = list(gameboard)
conwaygame()
while True:
gamedisplay()
conwayupdate()
gameboard = list(temp_board)
time.sleep(1)
os.system("cls")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.