CSci 150: Foundations of computer science I
Home Syllabus Assignments Tests

Assignment 9: Starting code

[Assignment description]

from graphics import *
import random

grid_size = 15    # number of rows/columns in grid
square_size = 10  # width/height of each individual square

def run():
    size = grid_size * square_size
    window = GraphWin('SimMushroom', size, size)
    grid = create_grid(window)
    plant_spores(grid, 4)
    while not window.isClosed():
        window.getMouse()
        do_spore_phase(grid)
        do_advance_phase(grid)

def do_spore_phase(grid):
    pass # replace this line with your implementation

def do_advance_phase(grid):
    pass # replace this line with your implementation

#
# You should modify anything below this point.
#


def create_grid(win):
    grid = (grid_size ** 2) * [0]
    for i in range(len(grid)):
        row = i // grid_size
        col = i % grid_size
        if row == 0 or row == grid_size - 1 or col == 0 or col == grid_size - 1:
            color = 'black'
        else:
            color = 'green'
        sq_top_left = Point(col * square_size, row * square_size)
        sq_bot_right = Point((col + 1) * square_size, (row + 1) * square_size)
        sq = Rectangle(sq_top_left, sq_bot_right)
        sq.setOutline(color)
        sq.setFill(color)
        sq.draw(win)
        grid[i] = [sq, color]
    return grid

def plant_spores(grid, spore_count):
    spores_to_plant = spore_count
    while spores_to_plant > 0:
        index = random.randrange(len(grid))
        if get_grid(grid, index) == 'green':
            set_grid(grid, index, 'pink')
            spores_to_plant = spores_to_plant - 1

def get_grid(grid, index):
    return grid[index][1]

def set_grid(grid, index, color_name):
    grid[index][1] = color_name
    grid[index][0].setOutline(color_name)
    grid[index][0].setFill(color_name)

run()