2017-12-13

posted Dec 13, 2017, 6:21 AM by Konstantinovich Samuel   [ updated Dec 13, 2017, 7:50 AM ]
Do Now:
Take out your homework
Share with your neighbor :
    Which 2 models
    What things did you learn

Mini Lab: Lists and States

You can make turtles have different behaviors at different times. 
Sometimes those actions are based on something that is not currently happening but was initiated several ticks ago!

e.g.
Jumping
Sleeping
Looking for a turtle they saw recently but do not see now
Being angry / happy / sad / etc. based on an event 

This means that a turtle has to remember what it is doing so it keeps doing it next tick. 
We can store the "State" of the turtle in a variable so that it knows what to do next tick.

We can change states in several ways:
 - Timers can change a state when enough time has passed
 - Changes in the surroundings (patches / turtles / or whatever) can change a state
 - You can randomly change states



Make setup / go buttons. Make go forever.

Now use this base code:

turtles-own [ meetings mode spin-timer]
;;mode is the state of the turtle  
;;1 : forward 
;;2 : wiggle 
;;3 : spinning 
;;meetings is a list of all the turtles they have interacted with or "met"

to setup
  ca 
  reset-ticks
  ask n-of 10 patches 
  [
    sprout 1 
    [
      set size 2
      set mode one-of [ 1 2 3]
      ;; IF YOUR STATE IS SPINNING: set spin-timer to 30

      set meetings []
    ] 
  ]
end

to go
  ask turtles[
    ;;Show the who and the list of meetings as a label
    set label word who word " " meetings

    ;;THIS SHOULD BE A PROCEDURE
    ;;Have a 1 / 200 chance of changing to spinning, and setting a spin-timer to 30 

    ;;THIS SHOULD BE A PROCEDURE:
    ;;when a turtle is in "forward" mode they:
    ;; move forward .1, change to red
    ;;when a turtle is in "wiggle" mode they:
    ;; wiggle, change to green
    ;;when a turtle is in "spinning" mode they:
;; turn 2 degrees,
    ;; change to white, 
    ;; lower the spin timer by 1. 
    ;; When spin-timer reaches 0, change mode to forward or wiggle (randomly)
    
    ;;THIS SHOULD BE A PROCEDURE
    ;;When turtles are in a 2 patch radius of eachother AND they they have NOT yet met:
    ;;  Pick one of the turtles nearby randomly to interact with:
    ;;    you and the other turtle add each other's who number to the meetings list
    ;;    you and the other turtle exchange states. (do this once! not twice)

  ]
  tick
end

to wiggle
  fd .1
  rt random 10
  lt random 10
end

Comments