12-06-2013

posted Dec 6, 2013, 11:45 AM by Samuel Konstantinovich   [ updated Dec 6, 2013, 11:45 AM ]
Homework:
1. Tutorials 20 and 21.
2. Compare the solution to your lab. Make sure you can understand why your lab doesn't work the way you want it to act.

Lab Solution:
turtles-own [deathTimer]
globals[accidentDeaths naturalDeaths]
to setup
  ca
  reset-ticks
  crt 5 
  [
    set shape "car" 
    set color red 
    set xcor (- abs random-xcor)
    set heading random 4 * 90
  ]
  crt 10
  [
    set shape "person"
    set color blue
    set xcor abs random-xcor
    set deathTimer -1
  ]
  ask turtles 
  [
    set ycor random-ycor
    set size 2
  ]
end

to go
  ask turtles with [shape = "person"] [peopleMotion]
  ask turtles with [shape = "car"] [carMotion]
  wait .06
  tick
end

to carMotion
  set pcolor gray
  fd 1
  ;kill pedestrians
  ask turtles-here with [shape = "person"] 
  [  
    set accidentDeaths accidentDeaths + 1 
    die
  ] 
end


to-report deathRate
  ifelse count turtles with [shape = "person"] > 150
  [report 10]
  [report 6]
end

to peopleMotion
  ;death logic
  
  ;this  may fix your giant turltes: if color = red and deathtimer < 1
  if deathTimer = 0 
  [ 
    set naturalDeaths naturalDeaths + 1
    die
  ] 
  
  if deathTimer > 0 
  [
    set deathTimer deathTimer - 1 
    set size size - .1
  ]
  if color = blue and random 100 < deathRate 
  [
    set color red 
    set deathTimer 20
  ]

  ;birth logic
  if color = blue and random 100 < 8 
  [
    hatch 1 [rt 90]
  ]
  
  fd .3
  rt random 10
  lt random 10
end
Comments