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
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();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.