我正在尝试随机挑选 2 只相同品种的海龟 - 但我正在努力解决如何做到这一点。我有10个不同的品种。我的代码应该首先随机选择任何品种的海龟,然后随机选择一个与第一个品种相同的品种。但我真的不知道该怎么做。谁能告诉我如何做到这一点?从我期望的其他编程语言中,我可以将海龟对象存储在变量中(有效)
let source one-of turtles
Run Code Online (Sandbox Code Playgroud)
然后以某种方式将品种作为我的source乌龟的属性,就像这样(这不起作用)
let source-breed source.getBreed
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?
正如您在 NetLogo 的文档中所见,每只海龟都有一个breed变量,该变量引用包含该品种所有海龟的代理集。您可以使用of访问海龟变量,或在ask块的上下文中引用它。
下面是一个例子:
breed [ mice mouse ]
breed [ cats cat ]
breed [ dogs dog ]
to go
clear-all
create-mice 10
create-cats 10
create-dogs 10
let source one-of turtles
show word "We picked: " source
show word "The source breed is: " [ breed ] of source
ask source [
let other-turtle one-of other breed
show word "Here is another turtle of the same breed: " other-turtle
]
end
Run Code Online (Sandbox Code Playgroud)
请注意other表达式中的 使用one-of other breed,意思是“我品种的另一只海龟”(不是“另一品种的海龟”。)