NetLogo。如何利用 "ifelse "语法中的 "with "语法构建具有特定条件的语句?

问题描述 投票:1回答:1

如何建立一个 "ifelse "语句的语法,让乌龟在指定的补丁中,并让乌龟判断乌龟的颜色是否是指定的颜色?以下是示例代码,但无法使用。顺便说一下,我想用 "max-pxcor - 1 "来构建。

ifelse count turtles-on patch (max-pxcor - 1) with [color = red] = 1[ ;The following code is omitted
netlogo
1个回答
4
投票

(1)补丁X Y符号选择了一个特定的补丁,但你没有提供一个 pycor. 因此,我假设你想让任何补丁的正确值为 pxcor. 其中有多少个补丁被染成红色?(2) 如果你没有选择一个带有标识符的补丁,那么你是在处理一个补丁集而不是一个补丁,即使这个补丁集只有一个补丁。如果你需要从一个补丁集中获取一个补丁,你需要 one-of.

如果你真的想确定那个补丁,这样你就可以用它来做一些事情,那么这种方法把补丁和海龟分开,所以是合理可读的。

let wanted-patch one-of patches with [pxcor = max-pxcor - 1 and color = red]
ifelse count turtles-on wanted-patch = 1
[

但是你也可以这样做:

ifelse any? patches with [pxcor = max-pxcor - 1 and
                          color = red and
                          count turtles-here = 1]
[

当然,如果你真的想让补丁集的右侧红色补丁上的海龟总数是1(所以一个补丁上是1,其他补丁上是0),那么:

ifelse count (turtles-on patches with [pxcor = max-pxcor - 1 and color = red]) = 1
[

最后一个,为了便于阅读,()是可选的。

© www.soinside.com 2019 - 2024. All rights reserved.