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

MATLAB code COGS 104 Assignment 8: The Game Life Clear all variables Set the (sq

ID: 3210271 • Letter: M

Question

MATLAB code

COGS 104
Assignment 8: The Game Life

Clear all variables
Set the (square) grid dimensions to 50, and call it n.
Make a nXn matrix, called GRID, with a .6 probability of any given cell having a 1 (active) in it; otherwise, 0 (inactive). This will make 60% of the cells in the matrix into 1s and 40% into 0s.

Make a vector that goes from 2 to n and then ends with a 1, call it up.
Make a vector that starts with n and then goes from 1 to n-1, call it down.
(these vectors will be used as indices for GRID, and they allow screen wrap-around)

Make the colormap gray, with a range of 2.

Start a for loop where i goes from 1 to 1000

Now make a matrix called neighbors that will sum up the number of active neighbors (out of a possible 8) that each cell has.
You do this by adding 8 versions of GRID together, where you use your “up” and “down” vectors as the indices.

For example:
GRID(up,:) checks for neighbors where the row index has been increased by 1 but the column index has not been changed.
GRID(:,down) checks for neighbors where the row index has not been changed but the column index has been decreased by 1.
GRID(up,up) checks for neighbors where both the row and column indices have been increased by 1.
Add up all 8 of these versions of GRID into your neighbors matrix

Now, rather then doing a bunch of if-then clauses to find whether the right number of neighbors of a cell are on or off, you can simply update GRID via a logic (True/False) based updating equation.
For example, when you write “neighbors==3”, rather then changing that variable to now equal the number three (as would happen if there were only one equals sign), it returns a matrix with the same dimensions as neighbors in which there are 1s (True) where the logical function was satisfied (because the cell had a 3 in it), and 0s (False) where the logical function was not satisfied (because the cell did not have 3 in it).

So, have the new state of GRID = the truth value of the (inclusive) logical disjunction of neighbors equaling three OR (both GRID already true AND neighbors equaling 2).

Now make an image of GRID (multiplied by 2 to accommodate the grayscale range) And add a pause of a couple hundred milliseconds for each update.
End your for loop

Clear all variables Set the (square) grid dimensions to 50, and call it n. Make a nXn matrix, called GRID, with a .6 probability of any given cell having a 1 (active) in it; otherwise, O (inactive). This will make 60% of the cells in the matrix into ls and 40% into 0s. Make a vector that goes from 2 to n and then ends with a l, call it up. Make a vector that starts with n and then goes from 1 ton- 1, call it down. (these vectors will be used as indices for GRID, and they allow screen wrap-around Make the colormap gray, with a range of 2. Start a for loop where i goes from 1 to 1000 Now make a matrix called neighbors that will sum up the number of active neighbors (out of a possible 8) that each cell has. You do this by adding 8 versions of GRID together, where you use your "up" and "down" vectors as the indices. For example: GRID(up.:) checks for neighbors where the row index has been increased by 1 but the column index has not been changed. GRID ,down) checks for neighbors where the row index has not been changed but the column index has been decreased by GRID(up.up) checks for neighbors where both the row and column indices have been increased by 1 Add up all 8 of these versions of GRID into your neighbors matrix Now, rather then doing a bunch of if-then clauses to find whether the right number of neighbors of a cell are on or off, you can simply update GRID via a logic (True/False based updating equation For example, when you write "neighbors3", rather then changing that variable to now equal the number three (as would happen if there were only one equals sign), it returns a matrix with the same dimensions as neighbors in which there are 1s (True) where the logical function was satisfied (because the cell had a 3 in it), and Os (False) where the logical function was not satisfied (because the cell did not have 3 in it) So, have the new state of GRID the truth value of the (inclusive) logical disjunction of neighbors equaling three OR (both GRID already true AND neighbors equaling 2). Now make an image of GRID (multiplied by 2 to accommodate the grayscale range) And add a pause of a

Explanation / Answer

The code is given below. Please specify the neighbours matrix dimensions for a better refined solution.

clc

clear


n = 50;
GRID = zeros(n);

for i=1:n
for j =1:n
if(rand<=0.6)
GRID(i,j)=1;
else
GRID(i,j)=0;
end
end
end

up = 2:n;
up(1,n) = 1;

down(1) = n;
down(2:n) = 1:n-1;

colormap(gray(2));