neighbors4

 

neighbors4 reports an agentset that contains the four patches that surround an agent's current patch in the north, east, west, and south directions. Both turtles and patches are allowed to use this reporter. For example, if we wanted to create a model in which a fire spread from one patch to its neighbors, we would write the following code:

ask patches [
    if pcolor = red [
        ask neighbors4 [
            if pcolor = green [
                set pcolor red
            ]
        ]
    ]
]

Things to keep in mind when using neighbors4:

In the model example below, there is a fire spreading in a forest. A patch on fire will spread to its neighboring patches in cardinal directions that contain trees. If a fire patch has brown neighboring patches that indicate ground, it will turn the ground patches gray to indicate the edge of the forest fire.

 

Try it Yourself