ThomasC
ThomasC

Reputation: 881

How can I move a turtle as close as possible to a certain patch?

I have a single blue patch and would like to move a turtle to the closest, empty patch to it. The only way I can think of doing this is using in-radius in a loop, increasing the radius size by one each time, but is there a better way?

Upvotes: 1

Views: 116

Answers (1)

Alan
Alan

Reputation: 9610

globals [bluey]

to setup
  ca
  ask one-of patches [set pcolor blue set bluey self]
  ask n-of 250 patches [sprout 1]
end

to-report nearest-empty [#patch]
  report min-one-of 
         [other (patches with [not any? turtles-here])] of #patch
         [distance #patch]
end

to test
  setup
  ;the following fails if all patches occupied
  ;(can add a test for nobody)
  ask nearest-empty bluey [set pcolor red]
end

Upvotes: 1

Related Questions