【问题标题】:NetLogo get the breed of a turtleNetLogo 获取乌龟的品种
【发布时间】:2014-06-20 19:37:24
【问题描述】:

我正在尝试随机挑选 2 只相同品种的海龟 - 但我正在为如何做到这一点而苦苦挣扎。 我有10个不同的品种。我的代码应该首先随机选择任何品种的乌龟,然后随机选择与第一个品种相同的乌龟。但我真的不知道该怎么做。谁能告诉我该怎么做?从我期望的其他编程语言中,我可以将乌龟对象存储在变量中(有效)

let source one-of turtles

然后以某种方式将品种作为我的 source 乌龟的属性,就像这样(whick 不起作用)

let source-breed source.getBreed

有人可以帮帮我吗?

【问题讨论】:

    标签: netlogo


    【解决方案1】:

    正如您在 NetLogo 的文档中看到的,each turtle has a breed variable 引用了包含该品种所有海龟的代理集。您可以使用 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
    

    注意other 在表达式one-of other breed 中的使用,意思是“我品种的另一只海龟”(不是“另一品种的海龟”。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多