CS 1010 01 Friday August 24th, 2012 - class #3 review (class #4 preview)

Computational Modeling and Simulation - Fall 2012

The applet requires Java 1.4.1 or higher. It will not run on Windows 95 or Mac OS 8 or 9. Mac users must have OS X 10.2.6 or higher and use a browser that supports Java 1.4. (Safari works, IE does not. Mac OS X comes with Safari. Open Safari and set it as your default web browser under Safari/Preferences/General.) On other operating systems, you may obtain the latest Java plugin from Sun's Java site.


Check out another example: Thursday/01/21/2010 example of IfElse and ODD versus EVEN turtles.
Focus is on expaining the IFELSE and the WHO number of turtles, along with REMAINDER.

 

powered by NetLogo

view/download model file: IfElseAug24th2012.nlogo

## WHAT IS IT?

Friday, August 24th, 2012 CS 1025 Computational Modeling and Simulation example - Followup/to be continued on Monday 08/27 in class #4)

Try out stagger2 first. It is simpler and does NOT use ifelse to make EVEN turtles do something different from the ODD turtles. CLICK THE TOP BUTTON and watch stagger2 at work.


stagger (Even fly further, Odd swim closer button) is more complicated.

## HOW IT WORKS

Try it out with different numbers of turtles. The SLIDER allows you to set the number of turtles that a created.


## HOW TO USE IT

The SLIDER is set to a minimum value of 4, maximum value of 128, increment value of 4. Try it out by dragging on the thumb, or by clicking to the left or right of the thumb.


## THINGS TO NOTICE

TO Stagger  
   ca                                   ;; ca is short for the clear-all command, an Observer Command
                                        ;; ca clears out EVERYTHING in preparation for something new, doing 
                                        ;; doing clear-turtles, clear-patches, clear-drawing.   What are PATCHES?  Don't worry!  
   
   cro howManyTurtles                   ;; howManyTurtles is the name of the SLIDER
   
   ask-concurrent turtles
   [
       ifelse (remainder who 2 = 0) 
           [  
             slowWalk 
             set shape "airplane" 
             slowWalk    
             changeColorEven
           ]           
           [  
             slowerWalk
             wait 1
             changeColorOdd
           ]        
   ]  
END

## SLOW WALK slowWalk is a procedure that uses "wait to slow the turtles down

TO slowWalk
    repeat 60        ;; 60 times 0.1 = 6 so "fd 6" would achieve the same result as "fd 0.1" done 60 times.
    [ 
      fd 0.1         ;; forward 0.1 of a step.
      wait 0.05      ;; wait 1/20th of a second, i.e. pause 5/100th of a second  
    ] 
end


## STAGGER2 is the simpler version (click the top button invokes STAGGER2


TO Stagger2                             ;; all the turtles do the SAME THING
   ca   
   cro howManyTurtles
   
   ask-concurrent turtles
   [
       fd 5
       
       wait 0.1                   ;; wait 0.1 of a second
       pd                         ;; put your pen down using pd command so you DRAW 
                                  ;;      (leave a trail and drag your tail 
                                  ;;          when you walk fd forward or bk backward)
       
       slowWalk        
   ]  
END

## The ODD turtles become WHITE and turn into FISH and change their size gradually from size 1 to size 3. (also rotate 360 degrees)

TO changeColorOdd
    wait 4
    set color white
    wait 1
    repeat 72 [ rt 5 wait 0.01 ]
 
    wait 0.5
    set shape "fish"
  
    changeSize
END
    

TO changeColorEven               ;; the EVEN turtles become YELLOW and do a full 360 degree twirling rotation.
    wait 5
    set color yellow
    wait 1
  
    repeat 144 [ rt 2.5 wait 0.01 ]
    changeSize
END

## Changing SIZE procedure - we repeat adding to the size 20 times with a pause of 0.1 second between each change = 2 seconds total.

TO changeSize
    repeat 20
    [
        set size size + 0.1   ;; size goes from 1 to 1.1 to 1.2 to 1.3 to 1.4 to 1.5 to ... to 2.8 to 2.9 to 3.0
        
        wait 0.1
    ]    
END

## CS 1010 01 8 MWF Computational Modeling and Simulation home page.