📄 ---brokenrabbits.nlogo
字号:
globals [ ticks ]breeds [ sheep wolves rabbits]turtles-own [ energy ]sheep-own [ grabbed? ] ;; used to prevent two wolves from eating the same sheeprabbits-own [ grabbed? ] ;; used to prevent two wolves from eathing the same rabbitpatches-own [ countdown ]to setup ca set ticks 0 ask patches [ set pcolor green ] if grass? [ ;; indicates whether the grass switch is on ;; if it is true, then grass grows and the sheep eat it ;; if it false, then the sheep don't need to eat ask patches [ set countdown random grass-regrowth-time ;; initialize grass grow clocks randomly if (random 2) = 0 ;;half the patches start out with grass [ set pcolor brown ] ] ] set-default-shape sheep "sheep" create-custom-sheep initial-number-sheep ;; create the sheep, then initialize their variables [ set color white set label-color blue - 2 set energy random (2 * sheep-gain-from-food) setxy random screen-size-x random screen-size-y 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 screen-size-x random screen-size-y ] set-default-shape rabbits "bunny" create-custom-rabbits initial-number-rabbits ;; create the wolves, then initialize their variables [ set color yellow set energy random (2 * rabbit-gain-from-food) setxy random screen-size-x random screen-size-y ] display-labels do-plotendto go ask sheep [ move if grass? [ set energy energy - 1 ;; deduct energy for sheep only if grass? switch is on eat-grass ] reproduce-sheep death ] ask wolves [ move set energy energy - 1 ;; wolves lose energy as they move catch-sheep if (initial-number-rabbits > 0 ) [ catch-rabbits ] reproduce-wolves death ] if (initial-number-rabbits > 0 ) [ ask rabbits [ move if grass? [ set energy energy - 1 ;; eat-grass ] reproduce-rabbits death ] ] if grass? [ ask patches [ grow-grass ] ] do-plot ;; plot populations every 0.5 [ display-labels ] set ticks ticks + 1 if not any turtles [ stop ]endto move ;; turtle procedure rt random 50 - random 50 fd 1 endto eat-grass ;; sheep procedure ;; sheep eat grass, turn the patch brown if pcolor = green [ stamp brown set energy energy + sheep-gain-from-food ;; sheep gain energy by eating ] endto reproduce-sheep ;; sheep procedure if random 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 360 fd 1 ] ;; hatch an offspring and move it forward 1 step ]end to reproduce-rabbits ;; sheep procedure if random 100 < rabbit-reproduce [ ;; throw "dice" to see if you will reproduce set energy (energy / 2) ;; divide energy between parent and offspring hatch 1 [ rt random 360 fd 1 ] ;; hatch an offspring and move it forward 1 step ]end to reproduce-wolves ;; wolf procedure if random 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 360 fd 1 ] ;; hatch an offspring and move it forward 1 step ]endto catch-sheep ;; wolf procedure locals [prey] set prey random-one-of sheep-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 eatingendto catch-rabbits ;; wolf procedure locals [prey] set 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-rabbit ] ;; get energy from eatingendto death ;; turtle procedure ;; when energy dips below zero, die if energy < 0 [ die ] endto 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) ] ] endto do-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 ]endto display-labels ifelse show-energy? [ ifelse grass? [ ask turtles [ set label round energy ] ] [ ask wolves [ set label round energy ] ask sheep [ set label no-label ] ] ] [ ask turtles [ set label no-label ] ]end; *** NetLogo Model Copyright Notice ***;; This model was created as part of the project: CONNECTED MATHEMATICS:; MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL; MODELS (OBPML). The project gratefully acknowledges the support of the; National Science Foundation (Applications of Advanced Technologies; Program) -- grant numbers RED #9552950 and REC #9632612.;; Copyright 1998 by Uri Wilensky. All rights reserved.;; Permission to use, modify or redistribute this model is hereby granted,; provided that both of the following requirements are followed:; a) this copyright notice is included.; b) this model will not be redistributed for profit without permission; from Uri Wilensky.; Contact Uri Wilensky for appropriate licenses for redistribution for; profit.;; This model was converted to NetLogo as part of the project:; PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN; CLASSROOMS. The project gratefully acknowledges the support of the; National Science Foundation (REPP program) -- grant number REC #9814682.; Converted from StarLogoT to NetLogo, 2000. Updated 2002.;; To refer to this model in academic publications, please use:; Wilensky, U. (1998). NetLogo Wolf Sheep Predation model.; http://ccl.northwestern.edu/netlogo/models/WolfSheepPredation.; Center for Connected Learning and Computer-Based Modeling,; Northwestern University, Evanston, IL.;; *** End of NetLogo Model Copyright Notice ***@#$#@#$#@GRAPHICS-WINDOW3501067833820208.80487804878048811400CC-WINDOW347341690468Command CenterSLIDER3132177165initial-number-sheepinitial-number-sheep0250011NILSLIDER3169177202sheep-gain-from-foodsheep-gain-from-food0.050.04.01.01NILSLIDER3204177237sheep-reproducesheep-reproduce1.020.04.01.01%SLIDER181132346165initial-number-wolvesinitial-number-wolves02504911NILSLIDER181168346201wolf-gain-from-foodwolf-gain-from-food0.0100.020.01.01NILSLIDER181204346237wolf-reproducewolf-reproduce0.020.05.01.01%SWITCH
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -