globals [grass]
;; 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.
breed [wolves wolf]
turtles-own [energy] ;; both wolves and sheep have energy
patches-own [countdown]
to setup
clear-all
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 water? [
if grass? [
ask patches [
set pcolor one-of [green brown blue]
if-else pcolor =[green blue]
[ set countdown grass-regrowth-time ]
[ set countdown random grass-regrowth-time ] ;; initialize grass grow clocks randomly for brown patches
]
]
if grass? [
ask patches [
set pcolor one-of [green brown]
if-else pcolor = green
[ set countdown grass-regrowth-time ]
[ set countdown random grass-regrowth-time ] ;; initialize grass grow clocks randomly for brown patches
]
]
]
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)
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 2 ;; easier to see
set energy random (2 * wolf-gain-from-food)
setxy random-xcor random-ycor
]
display-labels
set grass count patches with [pcolor = green]
;;set water count patches with [pcolor = blue]
reset-ticks
end
to go
if not any? turtles [ stop ]
ask sheep [
move
if water? [
if pcolor = blue [
set energy energy = 0
death ;;sheep drowns
]
]
if grass? [
set energy energy - 1 ;; deduct energy for sheep only if grass? switch is on
eat-grass
]
death
reproduce-sheep
]
ask wolves [
move
if water? [
if pcolor = blue [
set energy energy = 0
death
]
]
set energy energy - 1 ;; wolves lose energy as they move
catch-sheep
death
reproduce-wolves
]
if grass? [ ask patches [ grow-grass ] ]
set grass count patches with [pcolor = green]
tick
display-labels
end
to move ;; turtle procedure
rt random 50
lt random 50
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 display-labels
ask turtles [ set label "" ]
if show-energy? [
ask wolves [ set label round energy ]
if grass? [ ask sheep [ set label round energy ] ]
]
end
但是当我运行它时(你必须在狼的绵羊捕食中放置一个开关水?)它就像我根本没有做任何改变一样。为什么是这样?我怎样才能解决这个问题?
任何与水有关的东西都被我加入了代码。
答案 0 :(得分:1)
在第一个if water?
(setup
)中有两个if语句,并且都响应grass?
所以你确实将一些补丁变成了蓝色,但是你执行了发送{{} 1}}并将所有颜色变为棕色或绿色。
每当您感到困惑时,请制作较小的程序。如,
if
另一个问题是,你不想要to color-patches
if grass? [
let patch-colors [green brown]
if water? [set patch-colors [green brown blue]]
ask patches [set pcolor one-of patch-colors]
]
end
,其中(i)总是假的,(ii)试图设置水补丁的再生长倒计时。