AGENT BASED PROGRAMMING, LAB 2 (Part 2)

The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Sun's Java site.

powered by NetLogo

view/download model file: completed_tutorial4.nlogo

WHAT IS IT?

Thhis model is an extension of the model described in Tutorial 3 in the NetLogo User Manual. Instructions for making it can be found at:
http://bridges.brooklyn.cuny.edu/collegenow/modules/P9_NetLogo/Labs/Lab_2.pdf


PROCEDURES

;;;;;;;;;;;;;;;;;;; Made Change Here to add sound ;;;;;;;;;;;;;;;;;;;;
extensions [sound]       ;; adds the ability to use sound to our program

;;;;;;;;;;;;;;;;;;; Made Change Here to add AGE ;;;;;;;;;;;;;;;;;;;;
turtles-own [energy age] ;; for keeping track of when the turtle is ready
                         ;; to reproduce and when it will die

to setup
  clear-all
  setup-patches
  setup-turtles
  do-plots
  sound:play-drum "Splash Cymbal" 64 ;; play a cymbal crash
end

to setup-patches
  ask patches [ set pcolor green ]
end

to setup-turtles
  create-turtles number    ;; uses the value of the number slider to create turtles
  ask turtles [ setxy random-xcor random-ycor ]
  ;;;;;;;;;;;;;;;;;;; Made Change Here to add AGE ;;;;;;;;;;;;;;;;;;;;
  ask turtles [ set age random max-lifespan ]
  set-default-shape turtles "cow"   ;;;;;;;;;;;;;;;;;;; Made Change Here to add Shape ;;;;;;;;;;;;;;;;;;;;
  ask turtles [ set color white ]
end

to go
  if ticks >= 500 [ 
    sound:play-note "TRUMPET" 60 64 2
    stop 
  ] ;; stop after 500 ticks
  move-turtles
  eat-grass
  reproduce
  check-death
  regrow-grass
  tick                     ;; increase the tick counter by 1 each time through
  do-plots
end

to move-turtles
  ask turtles [
    ;; right random 360    ;;;;;;;;;;;;;;;;;;; Made Change Here to Intelligent Feeding ;;;;;;;;;;;;;;;;;;;;
    ;; forward 1
    let candidates patches in-radius vision-range with [ pcolor = green ]
    ifelse any? candidates
    [ face one-of candidates ]
    [ rt random 360 ]
    forward 1
    set energy energy - 1  ;; when the turtle moves it looses one unit of energy
    set age age + 1   ;;;;;;;;;;;;;;;;;;; Made Change Here to add AGE ;;;;;;;;;;;;;;;;;;;;
  ]
 ;; repeat 5 [ ask turtles [ fd 0.2 ] display ]  ;;;;;;;;;;;;;;;;;;; Made Change Here to Intelligent Feeding ;;;;;;;;;;;;;;;;;;;;
end

to eat-grass
  ask turtles [
    if pcolor = green [
      set pcolor black
           ;; the value of energy-from-grass slider is added to energy
      set energy (energy + energy-from-grass)
    ]
  ifelse show-energy?
    [ set label energy ] ;; the label is set to be the value of the energy
    [ set label "" ]     ;; the label is set to an empty text value
  ]
end

to reproduce
  ask turtles [
    if energy > birth-energy [
    set energy energy - birth-energy  ;; take away birth-energy to give birth
    hatch 1 [ set energy birth-energy set age 0 ]  ;;;;;;;;;;;;;;;;;;; Made Change Here to add AGE ;;;;;;;;;;;;;;;;;;;;
    ]
  ]
end

to check-death
  ask turtles [
    if energy <= 0 [ die ] ;; removes the turtle if it has no energy left
    if age >= max-lifespan [die]   ;;;;;;;;;;;;;;;;;;; Made Change Here to add AGE ;;;;;;;;;;;;;;;;;;;;
  ]
end

to regrow-grass
  ask patches [ ;; 3 out of 100 times, the patch color is set to green
    if ticks mod rain-frequency = 0 [ 
      set pcolor green 
    ] 
  ]
end

to do-plots
  set-current-plot "Totals" ;; which plot we want to use next
  set-current-plot-pen "turtles" ;; which pen we want to use next
  plot count turtles ;; what will be plotted by the current pen
  set-current-plot-pen "grass" ;; which pen we want to use next
  plot count patches with [pcolor = green] ;; what will be plotted by the current pen
end