首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >要求红海龟避开其他海龟,移到它的邻居之一,那里是空的,而且有最高的壳。

要求红海龟避开其他海龟,移到它的邻居之一,那里是空的,而且有最高的壳。
EN

Stack Overflow用户
提问于 2013-11-17 14:46:46
回答 1查看 4.1K关注 0票数 2

我刚接触过网络徽标。我正试着让红海龟向高楼移动。补丁。黄海龟不动。是我干的!但是我也想要求红海龟们不要再看到上面有黄、红海龟的地方,然后搬到高楼的邻居那里去。在我的代码中,我要求他们在被占用的补丁旁边停下来,因为我做不到。我也想避免让2只海龟在同一片土地上的任何时候。有人能帮我吗?

代码语言:javascript
复制
    patches-own [conc] 

to set-up
clear-all
ask patch random-pxcor random-pycor [
set conc 200
set pcolor scale-color red conc 0 1]
crt 5 [setxy random-xcor random-ycor  set shape "circle" set color red]
crt 20 [setxy random-xcor random-ycor set shape "circle" set color yellow]
reset-ticks
end




to go
diffuse conc 0.1
ask patches [set pcolor scale-color red conc 0 1]
ask turtles with [color = red]
[ifelse  not any? turtles-on neighbors
[if [conc] of max-one-of neighbors [conc]  > conc [
face max-one-of neighbors4 [conc]
 move-to max-one-of neighbors4 [conc]]]

[stop]

]

tick

end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-18 13:22:45

如果您使用let来避免重复,我认为您的代码会读得更好一些,如下所示:

代码语言:javascript
复制
let target max-one-of neighbors [conc]
if [conc] of target > conc [
  face target
  move-to target
]

有关强制执行“每个补丁一只海龟”规则的一些不同方法,请参阅NetLogo模型库的代码示例部分中的“每个补丁一海龟”示例模型。

我认为ifelse not any? turtles-on neighbors是你试图让海龟避免被占用的补丁。但是,正如你所写的,它有一个更强的效果-它使它使任何一个相邻的占用补丁的海龟不会移动在任何。

我想你的意思可能更像:

代码语言:javascript
复制
ask turtles with [color = red] [
  let targets neighbors with [not any? turtles-here]
  let target max-one-of targets [conc]
  if target != nobody and [conc] of target > conc [
    face target
    move-to target
  ]
]

希望这能有所帮助。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20032031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档