Don't Understand Brackets

113 views Asked by At

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 ]
1

There are 1 answers

2
Jasper On

Brackets in NetLogo can mean a few different things:

  • A list of literal values: let l [10 20 30 40 50] - l will have a list with those five numbers
  • A sequence of commands, sometimes called a command block: ask turtles [ forward 1 set pcolor red ] - each turtle will execute the two commands
  • A conditional expression that could be evaluated multiple times: let reds turtles with [color = red] each turtle will execute the check or while [count turtles < 100] [ ... ] the check will execute until it is false
  • As part of the globals, extensions, breeds, and breeds-own declarations: extensions [nw palette], globals [g1 g2 g3], or breed [frogs frog]
  • A list of procedure arguments: to move-agent [agent distance] ... end
  • An anonymous task: let 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, ignite and plants procedures and still go an error in 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

Looking at that error, I see that I have [ plants ] on a line, which looks like a sequence of commands, but it has no corresponding ask, while or 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:

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

And here the error is about nothing named plants being defined because we commented it out. That's okay, let's go put it back in:

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 ]

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 with and the commands. Also, this procedure has no closing end, which we should add:

to plants
  ask neighbors4 with [pcolor = black] [
    set pcolor blue 
  ]
  ask neighbors4 with [pcolor = blue] [
    set pcolor grey 
  ]
end

And after that we get a complaint about ignite not existing in setup, so we re-add that. And no compile errors at this point (ignite was fine as-is):

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
;

Next would be a matter of getting go uncommented and working through any issues there as they occur.