问题描述
如何将<breed>-here
的品种名称作为参数传递给另一个函数?我目前收到语法错误:
to fwd-reaction [ #asking-species species2 #x-k_on #x-alpha #species-to-die #new-breed ]
ask #asking-species [
if (any? other species2-here ) and random-float 1000 < (#x-k_on * 2 * #x-alpha)
[
ask one-of other #species-to-die -here
[ die ]
set breed #new-breed
set-attributes2 0 2 self true false red
]
]
end
编辑:
谢谢,grow3表现出色。
问题2:
具有特殊属性的海龟会有轻微的并发症,如何在args中传递它?例如:
ask monomers1 with [ aggregated = false ]
而不只是
ask monomers1
作为第一个参数?
问题3:
此外,我该如何传递一个必须孵化的品种的论点?
像这样
hatch-<breed> 1
但该品种作为#hatching-species
作为参数传递hatch-#hatching-species 1
解决方法
这里有两个选择。我认为这两者都不是很好的选择,但是如果我正在编写它,我会选择grow2
,因为您通常不想在字符串上使用runresult
,除非您确实需要
grow1
使用从品种名称创建的runresult
on a string,从而使frogs-here
或mice-here
。 grow2
将乌龟的品种转换为字符串,以便可以与a with
clause中的品种名称进行比较。两者都使用word
to make the string。
编辑以添加:前两个选项假定您要传递的品种为字符串值。如果您从另一只乌龟那里获得了实际的繁殖值,则可以通过进行类似[breed] of my-turtle
的操作来修改grow2
以接受该繁殖并丢弃word
的东西。我将其命名为grow3
breed [ mice mouse ]
breed [ frogs frog ]
to setup
clear-all
create-mice 100 [ fd 100 set color grey ]
create-frogs 100 [ fd 100 set color green ]
ask n-of 100 patches [ grow1 "mice" ]
ask n-of 100 patches [ grow2 "frogs" ]
let mice-breed [breed] of one-of mice
ask n-of 100 patches [ grow3 mice-breed ]
end
to grow1 [breed-name]
let breed-here (runresult (word breed-name "-here"))
if any? breed-here [
ask one-of breed-here [
set size (size + 1)
]
]
end
to grow2 [breed-name]
let breed-here turtles-here with [(word breed) = breed-name]
if any? breed-here [
ask one-of breed-here [
set size (size + 1)
]
]
end
to grow3 [breed-val]
let breed-here turtles-here with [breed = breed-val]
if any? breed-here [
ask one-of breed-here [
set size (size + 1)
]
]
end