我不太明白括号里的内容;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 ]
Run Code Online (Sandbox Code Playgroud)
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] [ ... ]检查将执行直到它被执行为止。falseextensions [nw palette]、globals [g1 g2 g3]或breed [frogs frog]to move-agent [agent distance] ... endlet t [ [x y z] -> (x + y) * z ]熟悉这些不同的用法有助于了解何时需要使用括号。
对于您的实际代码,我建议将内容写出来以确保它始终有效,并经常编译,这样当您出现不匹配时,就不难看出原因。如果您确实有一大堆需要排除故障的代码,您也可以反转此过程,方法是注释掉代码,直到找到可以工作的代码,然后慢慢添加回去。您可以通过选择代码行并点击来快速完成此操作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
Run Code Online (Sandbox Code Playgroud)
查看该错误,我发现我有[ 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
Run Code Online (Sandbox Code Playgroud)
这里的错误是关于没有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 ]
Run Code Online (Sandbox Code Playgroud)
在这里我们得到一个“预期结束括号”错误,但现在有了我们的知识,我们可以看到一些错误。括号括住条件子句、thewith和命令。另外,这个过程没有结束end,我们应该添加:
to plants
ask neighbors4 with [pcolor = black] [
set pcolor blue
]
ask neighbors4 with [pcolor = blue] [
set pcolor grey
]
end
Run Code Online (Sandbox Code Playgroud)
之后我们收到关于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
;
Run Code Online (Sandbox Code Playgroud)
接下来的问题是取消go评论并解决出现的任何问题。