Paython task:(Python only) In this assignment you will be programming a simulati
ID: 3743618 • Letter: P
Question
Paython task:(Python only)
In this assignment you will be programming a simulation known as Conway’s Game of Life. As shown in Fig. 1, the simulation consists of a world defined by an N ×M pixel grid. In this world, pixels represent cells that can take on one of two different states: living cells are drawn as black pixels and dead cells are drawn as white pixels. The state of each cell within the world may change as the simulation runs forward in time. We determine if a particular cell will be alive or dead in the next frame by examining the current frame and applying an update rule. In Conway’s Game of Life, this update rule consists of inspecting the state of the eight cells immediately surrounding the cell being updated. Living cells with too many neighbors die due to starvation, living cells with too few neighbors die due to under population, and dead cells with neither too many nor too few neighbors come to life as a result of reproduction within a hospitable environment.
The Big Idea In this assignment you will be programming a simulation known as Conway's Game of Life. As shown in Fig. 1, the simulation consists of a world defined by an Nx M pixel grid. In this world, pixels represent cells that can take on one of two different states: living cells are drawn as black pixels and dead cells are drawn as white pixels. The state of We determine if a particular cell will be alive or dead in the next frame by examining the current frame and applying an update rule. In Conway's Game of Life, this update rule consists of inspecting the state of the eight cells immediately surrounding the cell being updated. Living cells with too many neighbors die due to starvation, living cells with too few neighbors die due to under population, and dead cells with neither too many nor too few neighbors come to life as a result of reproduction within a hospitable environment ach cell within the world may change as the simulation runs forward in time. (a) Frame n (b) Frame n 1 (c) Frame n 2 Figure 1: Three frames from a running simulation of Conway's Game of Life. Each pixel represents a living cell (black) or a dead cell (white). The number of living cells immediately adjacent to a cell determines if it is alive or dead in the next frame of the simulation. Each cell has eight immediately adjacent neighbors. The particular pattern shown here oscillates between two states These simple biologically inspired rules for determining if a single cell should be alive or dead lead to some pretty astounding results when simulated. You will notice that certain stable and oscillatory stable patterns that tend to behave like cellular organisms emerge from the chaos The process of building a world grid, implementing an update rule, and managing boundary conditions are all fundamental elements of designing and implementing a broad class of computer simulations you will encounter within engineering. This project will expose you to these concepts and their implementation.Explanation / Answer
gameoflife.py
#!/usr/bin/python
import patterns
import sys
import argparse
from matplotlib import pyplot as plt
from matplotlib import animation
from random import random
def generate_world(opts):
"""
Accepts: opts -- parsed command line options
Returns: world -- a list of lists that forms a 2D pixel buffer
Description: This function generates a 2D pixel buffer with dimensions
opts.cols x opts.rows (in pixels). The initial contents
of the generated world is determined by the value provided
by opts.world_type: either 'random' or 'empty' A 'random'
world has 10% 'living' pixels and 90% 'dead' pixels. An
'empty' world has 100% 'dead' pixels.
"""
world = []
## TASK 1 #############################################################
if opts.world_type=='empty':
world=[[0 for x in range(opts.rows)] for y in range(opts.cols)]
elif opts.world_type=='random':
world=[[0 for x in range(opts.rows)] for y in range(opts.cols)]
for i in range(0,opts.rows):
for j in range(0,opts.rows):
randnum=random()
if randnum < 0.10:
world[i][j]=1
#######################################################################
return world
def update_frame(frame_num, opts, world, img):
"""
Accepts: frame_num -- (automatically passed in) current frame number
opts -- a populated command line options instance
world -- the 2D world pixel buffer
img -- the plot image
"""
# set the current plot image to display the current 2D world matrix
img.set_array(world)
# Create a *copy* of 'world' called 'new_world' -- 'new_world' will be
# our offscreen drawing buffer. We will draw the next frame to
# 'new_world' so that we may maintain an in-tact copy of the current
# 'world' at the same time.
new_world = []
for row in world:
new_world.append(row[:])
## TASK 3 #############################################################
for j in range(opts.rows): #row
for i in range(opts.cols): #col
# Check for if current position is along the top, bottom, left,
# or right (and corners)
if i==0: #Top area
if j==0: #Top left corner
pos1=world[len(world)-1][len(world[0])-1]
pos2=world[len(world)-1][j]
pos3=world[i][len(world[0])-1]
pos4=world[i][j+1]
pos5=world[i+1][j]
pos6=world[i+1][j+1]
pos7=world[i+1][len(world[0])-1]
pos8=world[len(world)-1][j+1]
total=pos1+pos2+pos3+pos4+pos5+pos6+pos7+pos8
cur_point_val=world[i][j]
if cur_point_val==1:
if total< 2:
new_world[i][j]=0
elif total>3:
new_world[i][j]=0
else:
new_world[i][j]=world[i][j]
else:
if total==3:
new_world[i][j]=1
else:
new_world[i][j]=world[i][j]
elif j==(len(world[0])-1): #Top right corner
pos1=world[len(world)-1][j]
pos2=world[len(world)-1][j-j]
pos3=world[i][j-1]
pos4=world[i][j-j]
pos5=world[i+1][j-1]
pos6=world[i+1][j]
pos7=world[i+1][j-j]
pos8=world[len(world)-1][j-1]
cur_point_val=world[i][j]
total=pos1+pos2+pos3+pos4+pos5+pos6+pos7+pos8
if cur_point_val==1:
if total< 2:
new_world[i][j]=0
elif total>3:
new_world[i][j]=0
else:
new_world[i][j]=world[i][j]
else:
if total==3:
new_world[i][j]=1
else:
new_world[i][j]=world[i][j]
else: #Top
pos1=world[len(world)-1][j-1]
pos2=world[len(world)-1][j]
pos3=world[len(world)-1][j+1]
pos4=world[i][j-1]
pos5=world[i][j+1]
pos6=world[i+1][j-1]
pos7=world[i+1][j]
pos8=world[i+1][j+1]
cur_point_val=world[i][j]
total=pos1+pos2+pos3+pos4+pos5+pos6+pos7+pos8
if cur_point_val==1:
if total< 2:
new_world[i][j]=0
elif total>3:
new_world[i][j]=0
else:
new_world[i][j]=1
else:
if total==3:
new_world[i][j]=1
else:
new_world[i][j]=world[i][j]
elif i==(len(world)-1): #Bottom Area
if j==0: #Bottom left corner
pos1=world[i-1][j]
pos2=world[i-1][j+1]
pos3=world[i][len(world[0])-1]
pos4=world[i][j+1]
pos5=world[i-i][len(world[0])-1]
pos6=world[i-i][j]
pos7=world[i-1][len(world[0])-1]
pos8=world[i-i][j+1]
cur_point_val=world[i][j]
total=pos1+pos2+pos3+pos4+pos5+pos6+pos7+pos8
if cur_point_val==1:
if total< 2:
new_world[i][j]=0
elif total>3:
new_world[i][j]=0
else:
new_world[i][j]=world[i][j]
else:
if total==3:
new_world[i][j]=1
else:
new_world[i][j]=world[i][j]
elif j==(len(world[0])-1): #Bottom right corner
pos1=world[i-1][j-1]
pos2=world[i-1][j]
pos3=world[i][j-1]
pos4=world[i][j-j]
pos5=world[i-i][j]
pos6=world[i-i][j-j]
pos7=world[i-i][j-1]
pos8=world[i-1][j-j]
cur_point_val=world[i][j]
total=pos1+pos2+pos3+pos4+pos5+pos6+pos7+pos8
if cur_point_val==1:
if total< 2:
new_world[i][j]=0
elif total>3:
new_world[i][j]=0
else:
new_world[i][j]=world[i][j]
else:
if total==3:
new_world[i][j]=1
else:
new_world[i][j]=world[i][j]
else: #Bottom
pos1=world[i-1][j-1]
pos2=world[i-1][j]
pos3=world[i-1][j+1]
pos4=world[i][j-1]
pos5=world[i][j+1]
pos6=world[i-i][j-1]
pos7=world[i-i][j]
pos8=world[i-i][j+1]
cur_point_val=world[i][j]
total=pos1+pos2+pos3+pos4+pos5+pos6+pos7+pos8
if cur_point_val==1:
if total< 2:
new_world[i][j]=0
elif total>3:
new_world[i][j]=0
else:
new_world[i][j]=world[i][j]
else:
if total==3:
new_world[i][j]=1
else:
new_world[i][j]=world[i][j]
else:
if j==0: #Left side
pos1=world[i-1][len(world[0])-1]
pos2=world[i-1][j]
pos3=world[i-1][j+1]
pos4=world[i][len(world[0])-1]
pos5=world[i][j+1]
pos6=world[i+1][len(world[0])-1]
pos7=world[i+1][j]
pos8=world[i+1][j+1]
cur_point_val=world[i][j]
total=pos1+pos2+pos3+pos4+pos5+pos6+pos7+pos8
if cur_point_val==1:
if total< 2:
new_world[i][j]=0
elif total>3:
new_world[i][j]=0
else:
new_world[i][j]=world[i][j]
else:
if total==3:
new_world[i][j]=1
else:
new_world[i][j]=world[i][j]
elif j==(len(world[0])-1):#Right Side
pos1=world[i-1][j-1]
pos2=world[i-1][j]
pos3=world[i-1][j-j]
pos4=world[i][j-1]
pos5=world[i][j-j]
pos6=world[i+1][j-1]
pos7=world[i+1][j]
pos8=world[i+1][j-j]
cur_point_val=world[i][j]
total=pos1+pos2+pos3+pos4+pos5+pos6+pos7+pos8
if cur_point_val==1:
if total< 2:
new_world[i][j]=0
elif total>3:
new_world[i][j]=0
else:
new_world[i][j]=world[i][j]
else:
if total==3:
new_world[i][j]=1
else:
new_world[i][j]=world[i][j]
else: #Rest of the space
pos1=world[i-1][j-1]
pos2=world[i-1][j]
pos3=world[i-1][j+1]
pos4=world[i][j-1]
pos5=world[i][j+1]
pos6=world[i+1][j-1]
pos7=world[i+1][j]
pos8=world[i+1][j+1]
total=pos1+pos2+pos3+pos4+pos5+pos6+pos7+pos8
cur_point_val=world[i][j]
if cur_point_val==1:
if total< 2:
new_world[i][j]=0
elif total>3:
new_world[i][j]=0
else:
new_world[i][j]=world[i][j]
else:
if total==3:
new_world[i][j]=1
else:
new_world[i][j]=world[i][j]
#######################################################################
# Copy the contents of the new_world into the world
# (i.e. make the future the present)
world[:] = new_world[:]
return img,
def blit(world, sprite, x, y):
"""
Accepts: world -- a 2D world pixel buffer generated by generate_world()
sprite -- a 2D matrix containing a pattern of 1s and 0s
x -- x world coord where left edge of sprite will be placed
y -- y world coord where top edge of sprite will be placed
Returns: (Nothing)
Description: Copies a 2D pixel pattern (i.e sprite) into the larger 2D
world. The sprite will be copied into the 2D world with
its top left corner being located at world coordinate (x,y)
"""
## TASK 2 #############################################################
sprite_x=len(sprite[0])
sprite_y=len(sprite)
for yy in range(sprite_y):
for xx in range(sprite_x):
world[y+yy][x+xx]=sprite[yy][xx]
#######################################################################
def run_simulation(opts, world):
if not world:
print("The 'world' was never created. Exiting")
sys.exit()
fig = plt.figure()
img = plt.imshow(world, interpolation='none', cmap='Greys', vmax=1, vmin=0)
ani = animation.FuncAnimation(fig,
update_frame,
fargs=(opts, world, img),
interval=opts.framedelay)
plt.show()
def report_options(opts):
"""
Accepts: opts -- a populated command line options class instance
Returns: (Nothing)
Descrption: This function simply prints the parameters used to
start the 'Game of Life' simulation.
"""
print("Conway's Game of Life")
print("=====================")
print(" World Size: %i x %i") % (opts.rows, opts.cols)
print(" World Type: %s") % (opts.world_type)
print(" Frame Delay: %i (ms)") % (opts.framedelay)
def get_commandline_options():
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--rows',
help='set # of rows in the world',
action='store',
type=int,
dest='rows',
default=75)
parser.add_argument('-c', '--columns',
help='set # of columns in the world',
action='store',
type=int,
dest='cols',
default=50)
parser.add_argument('-w', '--world',
help='type of world to generate',
action='store',
type=str,
dest='world_type',
default='empty')
parser.add_argument('-d', '--framedelay',
help='time (in milliseconds) between frames',
action='store',
type=int,
dest='framedelay',
default=100)
opts = parser.parse_args()
return opts
def main():
"""
The main function -- everything starts here!
"""
opts = get_commandline_options()
world = generate_world(opts)
report_options(opts)
blit(world, patterns.glider,20,20)
run_simulation(opts, world)
if __name__ == '__main__':
main()
patterns.py
## Stills ###################################
block = [[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]]
beehive = [[0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 1, 0, 0, 1, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0]]
loaf = [[0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 1, 0, 0, 1, 0],
[0, 0, 1, 0, 1, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0]]
boat = [[0, 0, 0, 0, 0],
[0, 1, 1, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0]]
## Oscillators ##############################
blinker = [[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0]]
## Spaceships ###############################
glider = [[0, 0, 1],
[1, 0, 1],
[0, 1, 1]]
## Generators ###############################
gosper_gun = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.