* Porțiunile suplimentare de cod sunt evidențiate cu galben.
;; Sheep and wolves are both breeds of turtle.
breed [sheep a-sheep] ;; sheep is its own plural, so we use "a-sheep" as the singular. sheep reproduce and give a sheep
breed [wolves wolf] ;; wolves reproduce and give a wolf
turtles-own [energy] ;; both wolves and sheep have energy
patches-own [countdown]
to setup
;; (for this model to work with NetLogo's new plotting features,
;; __clear-all-and-reset-ticks should be replaced with clear-all at
;; the beginning of your setup procedure and reset-ticks at the end
;; of the procedure.)
__clear-all-and-reset-ticks
ask patches [ set pcolor green ]
;; check GRASS? switch.
;; if it is true, then grass grows and the sheep eat it
;; if it false, then the sheep don't need to eat
if grass? [
ask patches [
set countdown random grass-regrowth-time ;; initialize grass grow clocks randomly
set pcolor one-of [green brown]
]
]
set-default-shape sheep "sheep"
create-sheep initial-number-sheep ;; create the sheep, then initialize their variables
[
set color white
set size 1.5 ;; easier to see
set label-color blue - 2
set energy random (2 * sheep-gain-from-food) ;; sheep gain energy from grass
setxy random-xcor random-ycor
]
set-default-shape wolves "wolf"
create-wolves initial-number-wolves ;; create the wolves, then initialize their variables
[
set color black
set size 1.5 ;; easier to see
set energy random (2 * wolf-gain-from-food) ;; wolf gain energy from sheep
setxy 0 - xcor 0 - ycor ;; wolf begin in the center of the world, being that the center is 0;0
]
display-labels
update-plot
end
to go
if not any? turtles [ stop ]
ask sheep [
move-sheep ;; sheep move randomly through the entire world
if grass? [
set energy energy - 1 ;; deduct energy for sheep only if grass? switch is on
eat-grass ;; sheep gain energy from grass
]
reproduce-sheep ;; sheep reproduce when energy permits
death ;; sheep die
]
ask wolves [
move-wolf ;; wolf movement is limited
set energy energy - 1 ;; wolves lose energy as they move
catch-sheep ;; wolves gain energy from sheep
reproduce-wolves ;; wolves reproduce when energy permits
death ;; wolves die
]
if grass? [ ask patches [ grow-grass ] ]
tick
update-plot
display-labels
end
to move-sheep ;; sheep move randomly through world
rt random-float 50 - random-float 50
fd 1
end
to move-wolf ;; wolves move randomly through world
rt random-float 50 - random-float 50
if abs (xcor + dx) > max-pxcor - (max-pxcor * refuge-size / 100) [move-wolf] ;; wolf rotates randomly
;; if by moving forward one step wolf reaches refuge it rotates again
fd 1
end
to eat-grass ;; sheep procedure
;; sheep eat grass, turn the patch brown
if pcolor = green [
set pcolor brown
set energy energy + sheep-gain-from-food ;; sheep gain energy by eating
]
end
to reproduce-sheep ;; sheep procedure
if random-float 100 < sheep-reproduce [ ;; throw "dice" to see if you will reproduce
set energy (energy / 2) ;; divide energy between parent and offspring
hatch 1 [ rt random-float 360 fd 1 ] ;; hatch an offspring and move it forward 1 step
]
end
to reproduce-wolves ;; wolf procedure
if random-float 100 < wolf-reproduce [ ;; throw "dice" to see if you will reproduce
set energy (energy / 2) ;; divide energy between parent and offspring
hatch 1 [ rt random-float 360 fd 1 ] ;; hatch an offspring and move it forward 1 step
]
end
to catch-sheep ;; wolf procedure
let prey one-of sheep-here ;; grab a random sheep
if prey != nobody ;; did we get one? if so,
[ ask prey [ die ] ;; kill it
set energy energy + wolf-gain-from-food ] ;; get energy from eating
end
to death ;; turtle procedure
;; when energy dips below zero, die
if energy < 0 [ die ]
end
to grow-grass ;; patch procedure
;; countdown on brown patches: if reach 0, grow some grass
if pcolor = brown [
ifelse countdown <= 0
[ set pcolor green
set countdown grass-regrowth-time ]
[ set countdown countdown - 1 ]
]
end
to update-plot
set-current-plot "populations"
set-current-plot-pen "sheep"
plot count sheep
set-current-plot-pen "wolves"
plot count wolves
if grass? [
set-current-plot-pen "grass / 4"
plot count patches with [pcolor = green] / 4 ;; divide by four to keep it within similar
;; range as wolf and sheep populations
]
end
to display-labels
ask turtles [ set label "" ]
if show-energy? [
ask wolves [ set label round energy ]
if grass? [ ask sheep [ set label round energy ] ]
]
end