I don't really understand the brackets; NetLogo either says I'm missing brackets or need brackets. Whenever I try to add or remove brackets it just says a different one is broken. Code just flat out does not work. Someone help!
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 ]
Brackets in NetLogo can mean a few different things:
let l [10 20 30 40 50]-lwill have a list with those five numbersask turtles [ forward 1 set pcolor red ]- each turtle will execute the two commandslet reds turtles with [color = red]each turtle will execute the check orwhile [count turtles < 100] [ ... ]the check will execute until it isfalseextensions [nw palette],globals [g1 g2 g3], orbreed [frogs frog]to move-agent [agent distance] ... endlet t [ [x y z] -> (x + y) * z ]It's good to be familiar with these different usages to try to know when you need to be using brackets.
For your actual code, I recommend writing things out to make sure it always works, compiling frequently so when you have a mismatch, it's not hard to see why. You can also reverse this process if you do have a big bunch of code you're troubleshooting by commenting out the code until you get something that works, then slowly adding pieces back in. You can do this quickly by selecting lines of code and hitting
Ctrl-;to comment/uncomment them, or right-clicking and picking the option in the context menu.I commented out
go,igniteandplantsprocedures and still go an error insetup:Looking at that error, I see that I have
[ plants ]on a line, which looks like a sequence of commands, but it has no correspondingask,whileor similar block. The one above it,ask patches with [(random-float 100) < density]already has a command list,[ set pcolor green ]. So I think what we want is to combine the two command blocks:And here the error is about nothing named
plantsbeing defined because we commented it out. That's okay, let's go put it back in:And here we get an "Expected closing bracket" error, but now armed with our knowledge we can see some mistakes. The bracket goes around the conditional clause, the
withand the commands. Also, this procedure has no closingend, which we should add:And after that we get a complaint about
ignitenot existing insetup, so we re-add that. And no compile errors at this point (ignitewas fine as-is):Next would be a matter of getting
gouncommented and working through any issues there as they occur.