CS 105 - Assignment Six
Goals
- Demonstrate that you can manage a simulation with a lot of moving parts
- Gain experience working with clones
Prerequisite
For this assignment, you will need my starter project. Open the link and then save to make your own copy.
Background: Building a infection simulation
This week, you are going to build a (very simplified) infection simulation.
The simulation works by cloning a large number of sprites and letting them wander around. Some of the sprites are sick. When they come in contact with healthy sprites, there is a chance that the healthy sprites also get sick. Eventually sick sprites will either recover or die.
Because an infection simulation is not topical enough during a pandemic, I've also added a Halloween theme, so healthy sprites are brown bats, sick sprites are black bats, and recovered sprites are ghosts (like the coins, you will see that I have provided appropriately labeled costumes for the sprite).
Rather than having you write this assignment from scratch, I've decided to provide most of the framework. Throughout the simulation, I use custom blocks, and these I have left for you to fill in.
Variables
The simulation uses four variables.
initial population
- this is the number of clones to make totalinfection rate
- this is the probability (out of 100) that a sprite will get sick when it touches a sick spritefatality rate
- the probability (out of 100) that a sprite will die rather than recoverdeaths
- a tally of the number of sprites that died during the simulation
Communication
You will notice that one of the scripts I provided received messages:
In particular, it receives the message 'get sick'. When you want a sprite to get sick, send it this message.
Implementation
Initialization
I have provided you with the start of the initialization.
This initializes the variables for the simulation and sets up our sprite to be cloned. Your task is to actually create the clones by adding the implementation for .
There are a number of ways that you could implement this. Possibly the most straightforward would be to first create all of the clones, and then tell the appropriate number to get sick. I recommend the use of the to get the collection of clones, and the
to send a targeted message selected clones.
Wandering
For the simulation, the sprites will be wandering at random around the space. This is governed by this script:
Your part is to implement the . Create a "forever" loop in which the sprite turns by some random amount (left or right), moves forwards a few steps and then if it is on the edge, bounces back into the stage (remember
).
Roll the dice
A number of the actions in our simulation are dependant on "rates". These are the chance that some event will happen. For example, the infection rate
governs the likelihood of becoming infected if contacted by someone who is infected.
You need to implement the . This just involves generating a random number between 1 and 100 and then checking to see if the value is less than the provided rate.
Getting sick
The next priority is to get the sprite sick when it is told that it is time by implementing the .
You will notice that there is another variable that is sprite specific, which is called health
. When a sprite gets sick, you should set this variable to 'sick' and switch the costume appropriately. You should only do this if the sprite is currently healthy.
Recovery
The is very similar to the
.
Set the health to 'recovered' and switch the costume.
Dying
The is a little bit different. The sprite should slowly fade away and vanish. Use a loop and the ghost effect blocks from the Looks palette. Once the sprite has fully faded, add 1 to the
death
count and delete the clone ( ).
Dead or Not?
One of the scripts is handling the wait interval between getting sick and recovery.
As you can see, it waits a small random period of time and then, if the sprite is sick, it transitions to the next phase. You need to implement . This uses the
fatality rate
to determine if the sprite should recover or expire and then runs the appropriate block.
Infecting others
The final task is to spread the infection. Again, I provided you with the framework:
You need to create .
For this block, you want to iterate over all of the sprite's neighbors. These are defined as other sprites in the immediate area. You can get this list using the .
For each neighbor, send them a 'get sick' message if they are touching you ( ), and if they are unlucky (check the
infection rate
). I recommend the use of .
Expectations
- Upon clicking the
,
initial population
sprites should be created - Once created the sprites should wander randomly around the stage
- Some number of sprites should become sick at the start of the simulation
- When a sick sprite touches a healthy one, the healthy one should potentially become sick as determined by the
infection rate
- After a random interval, sick sprites should either get better or die based on the
fatality rate
- A running tally of the number of deaths should be maintained
- Sprites should wear costumes appropriate to their health status
Exceeding expectations
To exceed expectations, add two more parameters to the simulation: masked rate
and masked infection rate
. The masked rate
will determine which percentage of the population wears masks, while the masked infection rate
will determine the probability of infection if either of the touching sprites are masked. Raid the costume collection (or add new images) so we can differentiate "masked and healthy" from "healthy" and "masked and sick" from "sick".
Submitting
Share the project using the instructions from exercise 1.
Visit the assignment page on Canvas to submit the URL.