turtles-own [wages]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to go
move-turtles
get-employed
tick
end
to move-turtles
ask turtles [
right random 360
forward 1
]
end
to get-employed
ask turtles [
if pcolor = blue [
set color green
set wages wages + 10
]
ifelse show-wages?
[ set label wages ]
[ set label " " ]
]
end
to setup-patches
ask patches [set pcolor pink ]
patch 0 0 [ set pcolor blue ]
end
to setup-turtles
create-turtles 80
ask turtles [ setxy random-xcor random-ycor ]
ask turtles [ set color red]
end
我想添加代码来选择与有蓝色的补丁接触的80只海龟中的前20%。
答案 0 :(得分:1)
在你的问题中不清楚你将如何使用获得蓝色补丁的前20%的乌龟,所以我假设你只想存储它们以便以后使用这些信息。
我会为每只海龟添加turtles-own
is-first-20-percent?
套false
。
然后,在go
程序结束时,tick
i执行check-20
程序,如下所示:
to check-20
if count turtles with [color = green] = (count turtles * 20 / 100) [
ask turtles with [color = green] [set is-first-20-percent? true]
]
end
在每一刻你都可以通过命令检索到达蓝区的前20%的海龟:
ask turtles with [is-first-20-percent?] [ ... do something ... ]
此代码正常工作,因为80%的20%是整数(16),但如果您打算修改海龟的起始数量,我建议修改check-20
程序,如下所示:
to check-20
if (count turtles with [color = green] >= (count turtles * 20 / 100)
and count turtles with [is-first-20-percent?] = 0) [
ask turtles with [color = green] [set is-first-20-percent? true]
]
end