Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Application 9: Game of Life GUI ENGR 1221 Designed to test skills with: GUIs, user-defined functions. In this exercise, you will write a Matlab program with a full GUI to implement a classic cellular...

1 answer below »
Application 9: Game of Life GUI        ENGR 1221
Designed to test skills with: GUIs, user-defined functions.
In this exercise, you will write a Matlab program with a full GUI to implement a classic cellular automaton, Conway’s Game of Life.
General guidelines for all assignments: (1) complete using a script file unless otherwise noted; (2) submit all your work [e.g. script, command window, function files, figures]  
Introduction
Mathematicians and computer scientists have long studied the problem of how complex, global behavior can arise from simple, local rules. For example, each neuron in a
ain makes a local decision to fire or not based only on its local environment, but the collective actions of these cells leads to complex behavior by the organism.
One approach to modeling this type of phenomenon is called “cellular automata”. In the most basic type of CA algorithm, the “universe” or “organism” is represented by a grid of logical values (1 or 0). Each cell of the grid repeatedly transitions from its cu
ent state to its next state based on a simple set of rules involving only itself and its nearest neighbors.
An example of a CA algorithm can be seen in a simple model of traffic flow. The road acts like the “organism” and is split into finite cells. Each cell is either occupied by a vehicle (a logical 1) or unoccupied (a 0). In each time step, a car advances if and only if the cell ahead of it is unoccupied. The rules for updating cells are as follow (the kth cell at time t):
· If cell(k, t) = 1 and cell(k+1, t) = 1, then cell(k, t+1) = 1 (the car stays where it is)
· If cell(k, t) = 1 and cell(k+1, 0) = 0, then cell(k, t+1) = 0 (the car moves to the next cell)
· If cell(k, t) = 1 and cell(k+1, t) = 0 and cell(k-1, t) = 1, then cell(k, t+1) = 1 (The car moves to the next cell, and the car in the previous cell moves into the cell)
· If cell(k, t) = 0 and cell(k-1, t) = 1, then cell(k, t+1) = 1
· If cell(k, t) = 0 and cell(k-1, t) = 0, then cell(k, t+1) = 0
· Etc.
This set of rules can be summarized by the following logical expression:
Typically, all cells are updated simultaneously. This process is illustrated in figure 1.
Figure 1: Updating of traffic model.
As simplified as this model is, it can capture some important general features of traffic flow. For example, the flow rate (number of cars passing a given point per unit time) as a function of traffic density can be computed. The result will be as shown in figure 2. In light traffic, adding more cars to the road increases the traffic flow, but once a critical density is reached, adding more cars decreases the traffic flow (due to congestion).
Figure 2: Traffic flow vs car density, computed by CA algorithm.
Conway’s Game of Life
One of the most famous applications of cellular automata is Conway’s Game of Life. In this model, the universe is a 2-dimensional grid in which each cell is regarded as “alive” (1) or “dead” (0). The cells are updated based on how many of its 8 nearest neighbors are alive according to the following rules:
    Number of Live Neighbors
    Cu
ent State of Cell
    Next State of Cell
    0-1
    0, 1
    0
    2
    0
    0
    2
    1
    1
    3
    0, 1
    1
    4-8
    0, 1
    0
You should have already written this algorithm in problem 3 of Application 5. You can use your function from that assignment. The template script from that assignment will give you some hints about how to display the grid.
Specifications
You will create a GUI and a function that demonstrate Conway’s Game of Life. Your GUI should have the following features:
· An editable text box for the user to enter the grid size.
· A drop-down menu to select the starting configuration (see the linked webpage to see what these look like). Options include:
· At least one type of still life
· At least one type of oscillato
· At least one type of spaceship
· Optional: A more complex case (like the glider gun)
· An image display (GUI axis) showing the evolution of the system with time.
· A “Run” button that will start the image updates.
· A “Stop” button that will stop the image updates.
· A “Reset” button that will stop the simulation and set the image based on the grid size and selected configuration.
· An “Exit” button that closes the GUI.
To “code” the automation, create a function that creates the next grid based on the cu
ent one and follows the provided rules.
Here are some hints:
· Use a flag that changes based on button presses.
· Use imshow to show a matrix in the axis.
· Use a matrix to represent the state of each cell.
· Avoid doing calculations near the edges of the grid.
· Look at the 3x3 grid centered on the selected point when determining the next state.
· Pause between each “frame” so you can see the changes.
· Press the button next to “String” in the property inspector for the drop-down menu. Type in your options and hit the “Enter” key between each to create multiple options.
· A default grid size of 20 will fit all configurations shown in the link.
Answered Same Day Mar 22, 2021

Solution

Vidhi answered on Mar 27 2021
143 Votes
conway's GUI/figures/Screenshot (584).png
conway's GUI/figures/Screenshot(583).jpg
conway's GUI/src/GameOfLife.m
classdef GameOfLife < handle


properties
grid
neighbors
width
height
generation
periodic=1
end
properties(Constant)
OverPopulationLimit = 4 % at this value and beyond life is impossible
UnderPopulationLimit = 1 % below and at this value life is impossible
ReproductionValue = 3 % optimal conditions for life; new life generated
end

methods
function obj = GameOfLife(booleanGrid)
% booleanGrid is a 2D grid of boolean values
obj.grid = booleanGrid;
obj.width = size(obj.grid,1);
obj.height = size(obj.grid,2);
obj.generation = 0;
end
function update(obj)
updateNeighbors(obj);
for i=1:obj.width
for j=1:obj.height
Nneighbors = obj.neighbors(i,j);
if obj.grid(i,j) == 1
if Nneighbors <= GameOfLife.UnderPopulationLimit
obj.grid(i,j) = 0;
end
if Nneighbors >= GameOfLife.OverPopulationLimit
obj.grid(i,j) = 0;
end
else
if Nneighbors == GameOfLife.ReproductionValue
obj.grid(i,j) = 1;
end
end
end
end
obj.generation = obj.generation + 1;
end
function updateNeighbors(obj)
obj.neighbors = zeros(obj.width, obj.height);
for i=1:obj.width
for j=1:obj.height
if obj.periodic
% find directions left, right, up and down with
% periodic boundary conditions
up = j + 1;
if (up > obj.height); up = 1; end
down = j - 1;
if (down < 1); down = obj.height; end
left = i - 1;
if (left < 1); left = obj.width; end
right = i + 1;
if (right > obj.width); right = 1; end
% count the number of neighbors
obj.neighbors(i,j) = ...
sum(sum(obj.grid([left i right],[up j down]))) - ...
obj.grid(i,j);
end
end
end
end
function plot(obj)
imshow(~obj.grid, 'InitialMagnification', 'fit');
set(gcf, 'Color', [0.6 0.8 0.8]);
end
function run(obj, timesteps, speed)
if nargin < 2
timesteps = 100;
end
if nargin < 3
speed = 0.1;
end
plot(obj);
for i=1:timesteps
pause(speed);
update(obj);
plot(obj);
title(['timestep ' num2str(obj.generation)])
end
end
function insert(obj, nameOfShape, location)
shape.width = 0;
shape.height = 0;
switch nameOfShape
% space ships
case 'glider'
shape.width = 5;
shape.height = 5;
grid = zeros(shape.height,shape.width);
grid(4, 2:4) = 1;
grid(3, 4) = 1;
grid(2, 3) = 1;
case 'LWSS'
shape.width = 7;
shape.height = 6;
grid = zeros(shape.height,shape.width);
grid(2, [3 6]) = 1;
grid(3,2) = 1;
grid(4, [2 6]) = 1;
grid(5, 2:5) = 1;
% still shapes
case 'block'
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here