I can\'t figure out part 3 and 4. I think I drew my grid wrong in part 2 because
ID: 3913623 • Letter: I
Question
I can't figure out part 3 and 4. I think I drew my grid wrong in part 2 because it does not return a list. I'm completely stuck. Please help.
I'm using Python
********* Here's my code so far********
#create control panel
from graphics import *
def makeControl():
win_control=GraphWin('Control Panel', 300,250)
win_control.setBackground("light gray")
header=Rectangle(Point(0,15), Point(300,35))
header.setFill("black")
header.draw(win_control)
label=Text(Point(150,25), 'GENERATIONS')
label.setFill("white")
label.draw(win_control)
#create start button
start=Rectangle(Point(25,40), Point(125,70))
start.setFill("green")
start.draw(win_control)
t2=Text(Point(75,55),"START")
t2.draw(win_control)
#create pause button
pause=Rectangle(Point(180,40), Point(280,70))
pause.setFill("orange")
pause.draw(win_control)
t3=Text(Point(230,55),"PAUSE")
t3.draw(win_control)
#create quit button
q=Rectangle(Point(180,90), Point(280,120))
q.setFill("red")
q.draw(win_control)
t4=Text(Point(230,105),"QUIT")
t4.draw(win_control)
#create reset button
reset=Rectangle(Point(25,90), Point(125,120))
reset.setFill("light blue")
reset.draw(win_control)
t5=Text(Point(75,105),"RESET")
t5.draw(win_control)
#create living cells count
t6=Text(Point(100, 150), "Living Cells: ") #living cell count
t6.draw(win_control)
#display generation number
t7=Text(Point(100, 200), "Generation Num: ") #generation count
t7.draw(win_control)
makeControl()
def makeGrid():
win_grid=GraphWin('Generations Grid', 400,400)
win_grid.setCoords(0,0,400,400)
row = Line(Point(x*10,0),Point(x*10,400))
column = Line(Point(0,x*10),Point(400,x*10))
row.draw(win_grid)
column.draw(win_grid)
makeGrid()
Explanation / Answer
#create control panel
from graphics import *
from random import random
def makeControl():
win_control=GraphWin('Control Panel', 300,250)
win_control.setBackground("light gray")
header=Rectangle(Point(0,15), Point(300,35))
header.setFill("black")
header.draw(win_control)
label=Text(Point(150,25), 'GENERATIONS')
label.setFill("white")
label.draw(win_control)
#create start button
start=Rectangle(Point(25,40), Point(125,70))
start.setFill("green")
start.draw(win_control)
t2=Text(Point(75,55),"START")
t2.draw(win_control)
#create pause button
pause=Rectangle(Point(180,40), Point(280,70))
pause.setFill("orange")
pause.draw(win_control)
t3=Text(Point(230,55),"PAUSE")
t3.draw(win_control)
#create quit button
q=Rectangle(Point(180,90), Point(280,120))
q.setFill("red")
q.draw(win_control)
t4=Text(Point(230,105),"QUIT")
t4.draw(win_control)
#create reset button
reset=Rectangle(Point(25,90), Point(125,120))
reset.setFill("light blue")
reset.draw(win_control)
t5=Text(Point(75,105),"RESET")
t5.draw(win_control)
#create living cells count
t6=Text(Point(100, 150), "Living Cells: ") #living cell count
t6.draw(win_control)
#display generation number
t7=Text(Point(100, 200), "Generation Num: ") #generation count
t7.draw(win_control)
makeControl()
def makeGrid():
win_grid=GraphWin('Generations Grid', 400,400)
win_grid.setCoords(0, 0, 400, 400)
rectangles = [] # list to store the cell rectangles
for x in range(0, 401, 10): # x coordinate of cell rectange's upper left
for y in range(0,401,10): # y coordinate of cell rectange's upper left
rectangle = Rectangle(Point(x, y), Point(x + 10, y + 10)) # create cell's rectangle
rectangles.append(rectangle) # add cell to the list
rectangle.draw(win_grid) # draw the cell
return rectangles # return the list of cells
cells = makeGrid()
def life():
for cell in cells: # iterate through each cell
if random() <= 0.18: # if probability is less than 18%
cell.setFill('red') # cell is alive
else:
cell.setFill('white') # cell is dead
life()
def cycle():
new_state = {} # stores the new state (dead-False or alive-True) for each cell
for i in range(len(cells)): # iterate over the number of cells
current_cell = cells[i] # stores the current cell
x = i / 40 # cell number along x-axis
y=i%40 # cell number along y-axis
current_cell_state = current_cell.config['fill'] == 'red' # current state of the cell
living_neighbours = 0 # keeps a count of number of living neighbours of the current cell
for dx in range(-1, 2): # iterate over the x-axis for the neighbours
if x + dx < 0 or x + dx >= 40: # if neighbour lies outside x-axis bounds
continue # skip
for dy in range(-1, 2): # iterate over the y-axis for the neighbours
if y + dy < 0 or y + dy >= 40: # if neighbour lies outside y-axis bounds
continue # skip
if dx == 0 and dy == 0: # if cell is the current cell itself
continue # skip
j = (x + dx) * 40 + (y + dy) # cell index of neighbour
living_neighbours += int(cells[j].config['fill'] == 'red') # increment neighbour living count is neighbour is alive
if current_cell_state == True: # if current cell is alive
if living_neighbours < 2: # underpopulation case
new_state[i] = False # set new state of current cell to dead
elif living_neighbours == 2 or living_neighbours == 3: # neutral case
new_state[i] = True # set new state of current cell to alive
else: # overpopulation case
new_state[i] = False # set new state of current cell to dead
else: # if current cell is dead
if living_neighbours == 3: # reproduction case
new_state[i] = False # set new state of current cell to alive
else:
new_state[i] = False # set new state of current cell to dead
for i in new_state: # for each index in new_state
if new_state[i] == True: # if that cell's new state is living
cells[i].setFill('red') # make the cell alive
else: # if that cell's new state is dead
cells[i].setFill('white') # make the cell dead
cycle()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.