Courses/CS 461/Winter 2006/Jesse Zwerling/Homework 1
From CSWiki
< Courses | CS 461 | Winter 2006 | Jesse Zwerling
- grass_rabbits_wolves
breeds [rabbits wolves]
turtles-own [ energy ]
rabbits-own [ grabbed?]
to setup
ca
ct
cp
clear-plot
ask patches [ set pcolor green ]
grow-grass
set-default-shape rabbits "rabbit"
create-custom-rabbits number-rabbits [
set color white
randomize-position
set energy random 10 ;start with a random amt. of energy
set grabbed? false
]
set-default-shape wolves "wolf"
create-custom-wolves initial-number-wolves ;; create the wolves, then initialize their variables
[
set color black
set energy random (2 * wolf-gain-from-food)
setxy random-float screen-size-x
random-float screen-size-y
set size 1.5
]
setup-plot
do-plot
end
to go
grow-grass
move
do-plot
if not any? turtles
[ stop ]
end
to move
ask rabbits
[ step
eat-grass
reproduce
death ]
ask wolves [
step
catch-rabbits
reproduce-wolves
death
]
end
to catch-rabbits ;; wolf procedure
let prey random-one-of (rabbits-here ;; grab a random sheep
with [not grabbed?]) ;; that no one else is grabbing
if prey != nobody ;; did we get one? if so,
[ set grabbed?-of prey true ;; prevent other wolves from grabbing it
ask prey [ die ] ;; kill it
set energy energy + wolf-gain-from-food ] ;; get energy from eating
end
to reproduce-wolves ;; wolf procedure
if random-float 100 < wolf-reproduction-rate [ ;; 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 grow-grass
ask patches [
if pcolor = brown [
if ( random-float 1000 ) < grass-grow-rate
[ set pcolor green ]
] ]
end
to step ;moving takes some energy
rt random-float 50
lt random-float 50
fd 1
set energy ( energy - 0.5 )
end
to eat-grass ;;gain "grass-energy" by eating grass
if ( pcolor = green )
[ set pcolor brown
set energy ( energy + grass-energy ) ]
end
to reproduce ;;give birth to a new rabbit, but it takes lots of energy
if ( energy > rabbit-reproduction )
[ set energy ( energy / 2 )
hatch 1 [ fd 1 ] ]
end
to death ;;die if you run out of energy
if ( energy < 0 )
[ die ]
end
to do-plot
set-current-plot "Populations"
set-current-plot-pen "grass"
plot ( ( count patches with [pcolor = green] ) / 4 )
set-current-plot-pen "rabbits"
plot count turtles
set-current-plot-pen "weeds"
plot ( ( count patches with [pcolor = violet] ) / 4 )
set-current-plot-pen "wolves"
plot ( ( count turtles with [pcolor = black] ) / 4 )
end
to randomize-position
setxy random-float screen-size-x
random-float screen-size-y
end
to setup-plot ;; set up plotting
set-current-plot "Populations"
set-plot-y-range 0 number-rabbits
end

