Asking patches with coordinates contained in a list

Viewed 44

I have turtles with a memory that stores the coordinates of patches they travel:

turtles-own [
  memory
]

to setup
  clear-all
  create-turtles 1 [
    set memory (list)
  ]
end

to go
  ask turtles [
    right random 360
    move-to patch-ahead 1
    set memory (lput patch-here memory)
  ]
end

From this list, I want to ask something to any patches that have these coordinates and are white. I was trying:

ask patches at-points memory with [pcolor = white] [do-something] 

But getting the error

Invalid list of points [(patch 0 1)]

How can this be fixed?

2 Answers

Fix it with:

set memory (lput ([list pxcor pycor] of patch-here) memory)

Your answer works so feel free to ignore this. I just thought I'd offer you a few more options.

The problem with your original code is that you are not storing coordinates in a list but rather are storing patches in a list. To work with agents that you have stored in a list, you will need to use foreach. This allows you to cycle through all agents in the list and ask them indidivually to change their color.

to change-pcolor-list
  
  ask turtles [
    foreach memory [
       the-patch -> ask the-patch [set pcolor white]
    ]
  ]
  
end

Another option is to store all the patches a turtle passes through in a turtles-own agent-set. This allows the turtles to call upon all the patches simultaneously instead of patch by patch and allows you to bypass the need for using anonymous procedures (which is what foreach uses).

turtles-own [
  memory-2
]

to setup
  clear-all
  create-turtles 1 [
    set memory-2 (patch-set)
  ]
end

to go
  ask turtles [
    right random 360
    move-to patch-ahead 1
    set memory-2 (patch-set memory-2 patch-here)
  ]
end

to change-pcolor-agentset
  
  ask turtles [
    ask memory-2 [
      set pcolor white
    ]
  ]

end
Related