Netlogo,Neighbors4并通过agent-color

时间:2016-04-08 03:31:04

标签: colors netlogo agentset

我是NetLogo的新手,所以如果我的问题看起来像个新手......那就是原因。

我使用neigbhors4命令来识别攻击者代理的四个邻居。然后,我想根据它们的颜色和优先级排序(黑色,棕色和白色)从四个邻居中进行选择。如果邻居的颜色为黑色(优先级#1),则下一组指令将应用于该代理。如果没有邻居是黑色,则优先级排序中的下一个颜色(棕色)将接收指令。

使用某种类型的列表最好能实现这一目标吗?

1 个答案:

答案 0 :(得分:1)

以下答案强调新手的简单性。所以它只涉及提出的非常具体的问题。

to-report choose-neighbor
  let _candidates neighbors4 with [pcolor = black]
  if any? _candidates [report one-of _candidates]
  set _candidates neighbors4 with [pcolor = brown]
  if any? _candidates [report one-of _candidates]
  set _candidates neighbors4 with [pcolor = white]
  if any? _candidates [report one-of _candidates]
  report nobody
end

你会注意到这段代码有很多重复。如果将这样的重复捆绑到子例程中可能是个好主意,例如

to-report choose-nbr [#color]
  let _candidates neighbors4 with [pcolor = #color]
  report ifelse-value (any? _candidates) [one-of _candidates] [nobody]
end