我是 NetLogo 新手,我不太理解括号,NetLogo 要么说我缺少括号,要么需要括号。每当我尝试添加或删除括号时,它只会说另一个括号已损坏。单纯的代码是行不通的。有人帮忙吗!
globals [
initial-trees
burned-trees ]
breed [fires fire]
to setup
clear-all
set-default-shape turtles "square"
ask patches with [(random-float 100) < density]
[ set pcolor green ]
[ plants ]
ask patches with [pxcor = min-pxcor]
[ ignite ]
set initial-trees count patches with [pcolor = green]
set burned-trees 0
reset-ticks
end
to go
if not any? turtles
[ stop ]
ask fires
[ ask neighbors4 with [pcolor = green]
if random 100 < 50 [ignite]
[ ask neighbors4 with [pcolor = blue]
[ ask neighbors4 with [pcolor = grey]
if random 100 < 25 [ignite]
tick
end
to ignite
sprout-fires 1
[ set color yellow ]
set pcolor black
set burned-trees burned-trees + 1
end
to plants
[ ask neighbors4 with pcolor = black
set pcolor blue ]
[ ask neighbors4 with pcolor = blue
set pcolor grey ]
NetLogo 中的括号可以表示一些不同的含义:
let l [10 20 30 40 50]
- l
将有一个包含这五个数字的列表ask turtles [ forward 1 set pcolor red ]
- 每只乌龟将执行两个命令let reds turtles with [color = red]
或 while [count turtles < 100] [ ... ]
extensions [nw palette]
、globals [g1 g2 g3]
或breed [frogs frog]
熟悉这些不同的用法有助于尝试了解何时需要使用括号。
对于您的实际代码,我建议将内容写出来以确保它始终有效,并经常编译,这样当您出现不匹配时,就不难看出原因。如果您确实有一大堆需要排除故障的代码,您也可以反转此过程,方法是注释掉代码,直到找到可以工作的代码,然后慢慢添加回去。您可以通过选择代码行并点击来快速完成此操作
Ctrl-;
注释/取消注释它们,或右键单击并在上下文菜单中选择选项。
我注释掉了
go
、ignite
和plants
程序,但setup
仍然出错:
globals [
initial-trees
burned-trees ]
breed [fires fire]
to setup
clear-all
set-default-shape turtles "square"
ask patches with [(random-float 100) < density]
[ set pcolor green ]
[ plants ] ; error on this line - "Expected command."
ask patches with [pxcor = min-pxcor]
[ ignite ]
set initial-trees count patches with [pcolor = green]
set burned-trees 0
reset-ticks
end
查看该错误,我发现一行上有
[ plants ]
,它看起来像一系列命令,但它没有相应的 ask
、while
或类似的块。它上面的ask patches with [(random-float 100) < density]
已经有一个命令列表,[ set pcolor green ]
。所以我认为我们想要的是结合两个命令块:
to setup
clear-all
set-default-shape turtles "square"
ask patches with [(random-float 100) < density] [
set pcolor green
plants
]
ask patches with [pxcor = min-pxcor]
[ ignite ]
set initial-trees count patches with [pcolor = green]
set burned-trees 0
reset-ticks
end
这里的错误是关于没有定义任何名为
plants
的内容,因为我们将其注释掉了。没关系,我们把它放回去吧:
to setup
clear-all
set-default-shape turtles "square"
ask patches with [(random-float 100) < density] [
set pcolor green
plants
]
ask patches with [pxcor = min-pxcor]
[ ignite ]
set initial-trees count patches with [pcolor = green]
set burned-trees 0
reset-ticks
end
to plants
[ ask neighbors4 with pcolor = black ; expected closing bracket error here
set pcolor blue ]
[ ask neighbors4 with pcolor = blue
set pcolor grey ]
这里我们得到一个“预期结束括号”错误,但现在有了我们的知识,我们可以看到一些错误。括号括住条件子句、
with
和命令。另外,这个过程没有结束end
,我们应该添加:
to plants
ask neighbors4 with [pcolor = black] [
set pcolor blue
]
ask neighbors4 with [pcolor = blue] [
set pcolor grey
]
end
之后,我们收到关于
ignite
中不存在 setup
的投诉,因此我们重新添加它。此时没有编译错误(ignite
原样就很好):
globals [
initial-trees
burned-trees
]
breed [fires fire]
to setup
clear-all
set-default-shape turtles "square"
ask patches with [(random-float 100) < density] [
set pcolor green
plants
]
ask patches with [pxcor = min-pxcor] [
ignite
]
set initial-trees count patches with [pcolor = green]
set burned-trees 0
reset-ticks
end
to ignite
sprout-fires 1
[ set color yellow ]
set pcolor black
set burned-trees burned-trees + 1
end
to plants
ask neighbors4 with [pcolor = black] [
set pcolor blue
]
ask neighbors4 with [pcolor = blue] [
set pcolor grey
]
end
;to go
; if not any? turtles
; [ stop ]
; ask fires
; [ ask neighbors4 with [pcolor = green]
; if random 100 < 50 [ignite]
; [ ask neighbors4 with [pcolor = blue]
; [ ask neighbors4 with [pcolor = grey]
; if random 100 < 25 [ignite]
; tick
;end
;
接下来的问题是取消注释
go
并解决出现的任何问题。