Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

def generate_world(opts): \"\"\" Accepts: opts -- parsed command line options Re

ID: 3744865 • Letter: D

Question

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 #############################################################

    #creating a world with all 'empty' pixels first

    #here, I assume dead pixels are represented by 0 and living by 1

    for i in range(opts.rows):

        world.append([0 for x in range(opts.cols)])

    #now checking the world_type variable to see if it is 'random'.

    if opts.world_type=='random':

        #finding the total pixels count (rows*columns)

        total_pixel_count=opts.rows*opts.cols

        #finding the 10% of the above value

        living_pixels_count=int(total_pixel_count*0.10)

        #temporarily importing random module

        import random

        c=0 #a loop controller variable

        #loop until a random 10% pixels are made living

        while c<living_pixels_count:

            #generating two random row, col values

            row=random.randint(0,opts.rows-1)

            col=random.randint(0,opts.cols-1)

            #checking if the cell is empty

            if world[row][col]==0:

                #making it living

                world[row][col]=1

                #incrementing the count

                c+=1

    #######################################################################

    return world

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 os
             x      -- x world coord where left edge of sprite will be placed
    """
    i = 0
    j = 0
    for k in range(x,len(sprite)):
        j = 0
        for l in range(y,len(sprite[k])):
            world[k][l] = sprite[i][j]
            j = j + 1
        i = i + 1

Task 3: Implementing the Update Rule Once the world has been generated and you have added a pattern or two using the blitter, you are ready to start the simulation this is done by calling run.simulation) run simulation (opts, vorld) This function creates a figure using matplotlib and plots each pixel in the world grid using the imshow function. Subsequently, FuncAnimation) is called fig ing ani pit. figure() plt, imshow(world, interpolation«,none, cmaP",Greys, vmax-1, vmin-o) = animation FuncAnimation(fig, undate-frama fargs-(opts, world, ing), interval-opts.framedelay) pltO The fourth argument supplied to FuncAnimation () specifies how frequently the plot should be automatically updated (in milliseconds. As you can see, this is set on the command line using the framedelay option (See Section 3 and get.commandline.options) in gameoflife.py). The second argument supplied to FuncAnimation) specifies the function that is called for each frame update. The third argument allows you to pass function parameters to the update function in the form of a tuple. In this case we are passing the opts, world, and img parameters to the update.frame ) function. This means that all you need to do to implement the update rule is implement the function update.frame() i def update frame (frame num, opts, orld, img): Accepts: frame num(automatically passed in) current frame number a populated command line options instance world -the 2D world p12el buffer img the plot image img. set array (world) new world 0 for row in world newworld. append (rou:) I YOUR CODE GOES HERE # Copy the contents of the new-world gnto the uorld # (i.e. make the future the present) worldl:] nw world: return img, Here, world contains the current world state, and nev.world will be used to hold the solution to applying If you look at the definition of the update function update.frame), you will notice that it accepts 4 parameters instead of 3. This is because the frame number is automatically passed in as the first argument when the update function is called. Any arguments passed in as a tuple through the third argument of FuncAnination) are passed in after the frame number argument

Explanation / Answer

C#:

using System;

public class ImplementingUpdateRule{

public static void Main()
{
int M = 10, N = 10;

int[,] grid = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 1, 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, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

Console.WriteLine("Original Generation");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (grid[i,j] == 0)
Console.Write(".");
else
Console.Write("*");
}
Console.WriteLine();
}
Console.WriteLine();
nextGeneration(grid, M, N);
}

static void nextGeneration(int [,]grid,
int M, int N)
{
int[,] future = new int[M,N];


for (int l = 1; l < M - 1; l++)
{
for (int m = 1; m < N - 1; m++)
{

int aliveNeighbours = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
aliveNeighbours +=
grid[l + i,m + j];

aliveNeighbours -= grid[l,m];



if ((grid[l,m] == 1) &&
(aliveNeighbours < 2))
future[l,m] = 0;


else if ((grid[l,m] == 1) &&
(aliveNeighbours > 3))
future[l,m] = 0;


else if ((grid[l,m] == 0) &&
(aliveNeighbours == 3))
future[l,m] = 1;


else
future[l,m] = grid[l,m];
}
}

Console.WriteLine("Next Generation");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (future[i,j] == 0)
Console.Write(".");
else
Console.Write("*");
}
Console.WriteLine();
}
}
}