Search code examples
netlogo

How can I prevent NetLogo patches to overlap?


I have a question regarding NetLogo. I want to create N blue patches, surrounded by grey patches (and some turtles, but not important for this question). These patches should NOT overlap, and have at least 1 black patch between them. However, they DO overlap.

What did I do wrong? And how should I fix it?

Thanks in advance!

patches-own []
turtles-own [need awareness mode]

to setup

  clear-all

  ask n-of N patches with [pcolor = black and all? neighbors [pcolor = black] and all? neighbors [all? neighbors [pcolor = black]]] [
    set pcolor blue

    ask neighbors [
      set pcolor grey
    ]
  ]

  ask n-of M patches with [pcolor != blue and pcolor != grey and not (any? neighbors with [pcolor = grey])] [
    set pcolor red

    sprout 1 [set color white]
  ]

  reset-ticks

end

to go

end

Solution

  • the problem with your code is that Netlogo first chooses N random patches for which the condition applies, puts them into an agentset and then lets them execute the command in turn. It doesn't reevaluate in between if all conditions still apply to all patches that were initially chosen.

    Instead, I suggest repeat and one-of so that each new patch is chosen after the previous one has finished.

      repeat N [ 
        ask one-of patches with [pcolor = black and all? neighbors [pcolor = black] and all? neighbors [all? neighbors [pcolor = black]]] [
          set pcolor blue
        
          ask neighbors [
            set pcolor grey
          ]
        ]
      ]