Lecture: 12-03-2012 + Answers

posted Dec 3, 2012, 12:29 AM by Samuel Konstantinovich   [ updated Dec 4, 2012, 12:50 PM ]
Homework Watch videos: 
16  (link is broken on the site)

Do the Do now, computer work, AND read the notes. 

1. Reporter Computer work:
a. Write a reporter function that generates a random named color based on its number. All of the named colors end in the number 5:
5, 15, 25, ... 165.
b. Write a reporter function that generates a random integer from a to b not including b.    to-report randomIntRange [a b]
c. Write a reporter function that generates a random floating point from a to b not including b.    to-report randomFloatRange [a b]

Those three functions  are really useful when writing programs!

2. Patches computer work:
See if you can make a group of patches change color to complete the 4 problems below. (read the hint after you read the questions)
C1. Make an observer function that turns each quadrant of the screen a different color.
C2. Make an observer function that makes a large rectangle (non-square) in the middle of the screen turn blue. 
note: Flags are not square, Change your world size for 3 and 4. 
C3. Make an observer function that creates any flag with 3 vertical bars of different colors. (horizontal is fine too)
C4. Using only patches make a complicated stripy flag like Greece or the, United States.

hint: You probably want to use if statements inside of an ask patches block : 
ask patches [ if ??? [ set pcolor ...]  ]    

Sample flag command:
to flag
  ask patches 
    if xcor > ycor [set pcolor white] 
    if xcor = ycor [set pcolor red] 

3. You should read the notes, because I am not going to mention any of this unless someone asks a question in class. (or email)

a. Some weekend issues that popped up repeatedly:

The point of a turtle command, is that it asks "A turtle" not all turtles. If we want one turtle to run it, we ask one turtle to run the command. If we want all of them to run it, we ask all of them.  Take this move function:

to move  ;turtle context because of the commands inside
  fd .5

How many turtles are there? We don't know. 
Do we know that any turtle could run this? Yes, because it is in turtle context.

I could type it into the command center to make ALL turtles move like this:
(turtle context) >> move
;This makes EVERY turtle run the move command one at a time.

I could type this in the command center or in an observer context button  to make ALL turtles move:
(observer context) >> ask turtles [ move ]

I could type this in the command center or in an observer context button to make ONLY turtle 0 move:
(observer context) >> ask turtle 0 [ move ]

Or only the red turtles to do it:
(observer context) >> ask turtles [ if color = red [ move ]
>> ask turtles with [color = red] [move ]  ;you didn't learn this yet but it works

b. End of trees: Here is basically what you should have had for your 2nd tree... or something close to it. There was sample code posted that would have gotten you at least this far. 

to tree [ n branch angle ]
   if n > 0
      fd branch ;use the parameters here, and in the lt/rt command to follow
      hatch 1 [ rt angle tree (n - 1) (branch * .75) (angle + random 5 - random 5)]   ; .75 could have been a slider (branchDecay)
      hatch 1 [ lt angle tree (n - 1) (branch * .75) (angle + random 5 - random 5)]  ; so could the number 5 (maxRandAngle)
     ;notice the branches get 25% smaller, and the angle gets larger or smaller by up to 4 degrees. 

You can instead randomize the decay rate : multiply by a random floating point number like (branch *  randomFloatRange .5 .9  )  so it gets a 10% up to 50% reduction in size randomly. 
That is the same as saying   (branch * (.5 + random-float  .4) )  since you just wrote the randomFloatRange command. (This is not a built in command!)

to-report randomIntRange [ a b]
  report a + random ( b - a)
to-report randomFloatRange [a b]
  report a + random-float (b - a)

to c1
  ask patches
    if pxcor > 0 and pycor > 0 [set pcolor red]
    if pxcor > 0 and pycor <= 0 [set pcolor blue]
    if pxcor <= 0 and pycor > 0 [set pcolor pink]     
    if pxcor <= 0 and pycor <= 0 [set pcolor orange]

to c2
  ask patches [ 
    if pxcor > -3 and pxcor < 6 and 
       pycor > -2 and pycor < 4 
    [set pcolor white]
to c3
  ask patches[
    ifelse pxcor < -6 
    [ set pcolor red]
    [ ifelse pxcor > 6 
        [set pcolor blue]
        [set pcolor orange]