我正在尝试板条一个复杂的环境,其中三种类型的树共存,您可以使用滑块修改每种类型的数量。我在实施方面遇到的问题是"白天/夜"周期。当夜晚时,颜色应该变暗,但是有了代码,我的颜色会变得深色,再也不会恢复更明亮的颜色。我用作藻类模型的基础。
这是我的代码:
to setup
clear-all
setup-world
reset-ticks
end
to setup-world
ask n-of synchronic-tree-density patches [
set pcolor blue
]
ask n-of asynchronious-tree-density patches [
set pcolor yellow
]
ask n-of tree-patches patches [
set pcolor green
]
recolor-world true
end
to recolor-world
ask patches [
if pcolor = blue [
ifelse setting-up? or daytime? [
set pcolor blue
] [
set pcolor blue - 3
]
]
if pcolor = yellow [
ifelse setting-up? or daytime? [
set pcolor yellow
] [
set pcolor yellow - 3
]
]
if pcolor = green [
ifelse setting-up? or daytime? [
set pcolor green
] [
set pcolor green - 3
]
]
]
end
to go
recolor-world false
tick-advance 1
end
to-report daytime?
report ticks mod 24 < day-length
end
luke的答案解决了您的问题。但是,您可能还想在某种程度上简化代码,以便每次勾选一次daytime?
的状态。例如:
to recolor-world
if-else daytime? [
ask patches with [ tree-type = 1 ] [ set pcolor blue ]
ask patches with [ tree-type = 2 ] [ set pcolor yellow ]
ask patches with [ tree-type = 3 ] [ set pcolor green]
] [
ask patches with [ tree-type = 1 ] [ set pcolor blue - 3 ]
ask patches with [ tree-type = 2 ] [ set pcolor yellow - 3 ]
ask patches with [ tree-type = 3 ] [ set pcolor green - 3 ]
]
end
欢迎来到堆栈溢出。请查看MCVE指南,以获取一些提示。理想情况下,您的问题代码被削减到其他用户运行您的程序所需的内容 - 目标是他们可以按原样复制您的代码。现在,如果没有经过相当多的修改,我无法运行您的程序 - 我不确定我的解决方案是否适用于您的设置。如果简化代码,您更有可能获得有用的答案。
也就是说,我很确定您的问题来自recolor-world
过程中所有IF语句的事实。考虑这个:
if pcolor = green [
ifelse daytime? [
set pcolor green
] [
set pcolor green - 3
]
]
因此,在第一次浏览中,您确实会有一些绿色补丁,因为它们是在setup
过程中设置的。但是,一旦daytime?
变为false
,这些补丁程序运行命令set pcolor green - 3
,因此它们不再将if pcolor = green
评估为True-他们将永远不会再运行该代码块。我认为最简单的修复方法是使用除颜色以外的分组变量进行过滤:
patches-own [ tree-type ]
to setup
clear-all
reset-ticks
setup-world
end
to setup-world
ask n-of 50 patches [
set pcolor blue
set tree-type 1
]
ask n-of 50 patches [
set pcolor yellow
set tree-type 2
]
ask n-of 50 patches [
set pcolor green
set tree-type 3
]
end
to recolor-world
ask patches with [ tree-type = 1 ] [
ifelse daytime? [
set pcolor blue
] [
set pcolor blue - 3
]
]
ask patches with [ tree-type = 2 ] [
ifelse daytime? [
set pcolor yellow
] [
set pcolor yellow - 3
]
]
ask patches with [ tree-type = 3 ] [
ifelse daytime? [
set pcolor green
] [
set pcolor green - 3
]
]
end
to go
recolor-world
tick
end
to-report daytime?
report ticks mod 24 < 12
end
编辑
请参阅 @jenb的进一步改进,以进行更有效的实施。