2017-10-26 HW

posted Oct 26, 2017, 6:31 AM by Konstantinovich Samuel
Homework:
Watch NetLogo video 16,17.

Goal: Changing restricting values in variables:

Given a typical  go procedure:

to go:
  every 1 / 30[
    ask turtles[
       turtleStuff  
    ]
  ]
end

We can change values inside turtles easily. Let us increment a value (increase it by an amount) :
to turtleStuff
            set pxcor  pxcor + .1
            ;This means set the pxcor to the value ( pxcor + .1) 
            ;Since pxcor + .1 is evaluated before the set command is triggered
            ;this works as intended.
end

we can add an if statement to limit those values!

to turtleStuff
    set pxcor  pxcor + .1
    if pxcor > 5 
    [ set pxcor 5 ]
end


Since you know about reporter functions you should break up your code!

;turtle context reporter function:
to-report quadrant
  if xcor > 0 and ycor > 0
  [ report 1]
  if xcor <= 0 and ycor > 0
  [ report 2]
  if xcor <= 0 and ycor <= 0
  [ report 3]
  report 4
end

Now when a turtle calls the quadrant function, it evaluates to the number that matches the quadrant!

;turtle context command
to act
  if quadrant = 1
  [ quad1 ]
  if quadrant = 2
  [ quad2 ]
  if quadrant = 3
  [ quad3 ]
  if quadrant = 4
  [ quad4 ]
end

Comments