Search code examples
colorsnetlogoagentset

Netlogo, Neighbors4 and selecting by agent-color


I am new to NetLogo, so if my question reads like a novice...that's why.

I am using the neigbhors4 command to identify the four neighbors of an aggressor agent. I then want to select from the four neighbors based on their color and a priority ranking (Black, Brown and White). If the neighbor's color is black (priority #1), the next set of instructions would be applied to that agent. If none of the neighbors are black, the next color in the priority ranking (brown) would receive the instruction.

Would this be best achieved using some type of list?


Solution

  • The following answer emphasizes simplicity for a novice. So it deals only with the very specific question posed.

    to-report choose-neighbor
      let _candidates neighbors4 with [pcolor = black]
      if any? _candidates [report one-of _candidates]
      set _candidates neighbors4 with [pcolor = brown]
      if any? _candidates [report one-of _candidates]
      set _candidates neighbors4 with [pcolor = white]
      if any? _candidates [report one-of _candidates]
      report nobody
    end
    

    You will notice that this code has a lot of repetition. If would probably be a good idea to bundle such repetition into a subroutine, such as

    to-report choose-nbr [#color]
      let _candidates neighbors4 with [pcolor = #color]
      report ifelse-value (any? _candidates) [one-of _candidates] [nobody]
    end