CS 105 - Understanding Our Algorithmic World

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).

example simulation

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 total
  • infection rate - this is the probability (out of 100) that a sprite will get sick when it touches a sick sprite
  • fatality rate - the probability (out of 100) that a sprite will die rather than recover
  • deaths - 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:

get sick script

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.

initialization framework

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 initialize the population.

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 my block to get the collection of clones, and the send to block 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:

wandering script

Your part is to implement the wander block. 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 bounce block).

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 chance block. 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 get sick block.

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 recover block is very similar to the get sick block.

Set the health to 'recovered' and switch the costume.

Dying

The die block 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 ( delete clone block ).

Dead or Not?

One of the scripts is handling the wait interval between getting sick and recovery.

recovery script

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 recovery or die block. 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:

spreading the infection script

You need to create infect block.

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 my block.

For each neighbor, send them a 'get sick' message if they are touching you ( touching block), and if they are unlucky (check the infection rate). I recommend the use of for each item block.

Expectations

  • Upon clicking the green flag button, 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.