2015-12-09 LabN04

posted Dec 8, 2015, 9:01 PM by Samuel Konstantinovich   [ updated Dec 9, 2015, 7:58 AM ]
Goal: Complete LabN04, will go over any parts from yesterday in class.
Homework:
Complete any remaining lab!

Classwork:
Complete your lab from yesterday then add more:
I will go over 

Quick Changes:
Make pedestrians go forward 0.1 instead of 0.01
Make cars go forward speed instead of 0.1

New Variables:
1. There should be global variables accidentDeath and naturalDeath. These variables will act as a counters for the number of deaths that happened by getting hit by a car, or dying naturally. Clear-all will reset them to 0. Turtles dying will increase them.

2. Each turtle should have a variable age that determines how many times they have been alive when turtleMotion was called (increment it in peopleMotion)

New Widgets:

Make two sliders: startingPedestrians , startingCars
They control how many cars/pedestrians are created in the setup.

Make three 'monitor' widgets on your netlogo interface. 
Display the 
    -accidentDeaths (Just put the name of the variable into the monitor.)
    -naturalDeaths 
    -number of people shaped turtles (count them!)

Additional carMotion features:
When a car is on the same patch as a person, it makes the person die right away AND adds 1 to the accidentDeath count.

Additional peopleMotion features: 
1. Add 1 to the turtle's age.

2. (birth) People have an 1% chance of hatching a new person. The child should turn 90 degrees to the left  so they split apart from the parent quickly. (see the hatch command)

3. (death) People that are over 100  ticks old have a chance to die every time you call the peopleMotion command. 
(aka: every time you call go / every frame / every tick , these all mean the same thing)

3a. This chance is equal to:   1% per year over 100.  This requires a little math, but you can do it! 
e.g.
age 100 or less never die naturally.
age 113 has a 13% chance of death.
age 200 has 100% chance of dying naturally.
This can be an expression, or you can tuck it away in another function to make your code readable!

3b. This type of death is a 'natural death' and should add 1 to the naturalDeath counter.
(Organize your code! Make two functions birthChance and deathChance to handle these complex parts of the peopleMotion )


Code demo'ed in class:

turtles-own [speed age]
globals [accidentDeath naturalDeath]
to setup
  ca
  crt startingCars [
    set shape "car"
    set color red
    setxy (-1 * abs random-xcor) random-ycor
    set heading 90 * random 4
    set speed .1 * 2 ^ random 3
  ]
  crt startingPedestrians [
    set shape "person"
    set color blue
    setxy abs random-xcor random-ycor
  ]
end

to go
  every 1.0 / 30.0[
    ask turtles with [shape = "car"]
    [ carMotion]
    ask turtles with [shape = "person"]
    [ peopleMotion]
  ]
end


to carMotion
  fd speed
  if random 100 < 2
  [ set heading abs ( heading - 180)]
  hitPedestrians
end

to peopleMotion
  fd .1
  rt random 11
  lt random 11  
    
  ;change turtle variable
  set age age + 1 
  ;death and birth code
  death
  birth

  ;code for toggle widget
  ifelse showAge
  [set label age]
  [set label ""]
end

Comments