Trummler
Trummler

Reputation: 15

In an «ask turtles» method: How can I read a variable of a patch in a relative angle and distance from a turtle?

I want [turtles with [shape = "sheep"]] to move either to the lefthand side or the righthand side, depending on how many [attractive (green) patches] are around a certain relative patch.

How the turtles with [shape = "sheep"] should count the patches (yellow)

The (incorrect) code looks like this:

to move-sheep
  ask turtles with [shape = "sheep"] [
    right random (16 + (count neighbors4 with [pcolor = green] patch-right-and-ahead 60 2)^ 2)
    left random (16 + (count neighbors4 with [pcolor = green] patch-right-and-ahead -60 2)^ 2)
    forward 1
    #(some other commands…)
  ]
end

Thank you ^^

Upvotes: 0

Views: 100

Answers (1)

JenB
JenB

Reputation: 17678

It would be much easier if you actually told us what the problem is. You say your code is wrong, but not how you know it's wrong. Is it reporting an error (and, if so, what is the error and which line is reporting it)? Is the moving turtle going the wrong way?

Regardless, the easiest way to approach this is to do something smaller before trying to move. If you simply count the number of green patches and print that out, you can test the code. Once you introduce movement, how will you tell if it counted correctly?

I am still not entirely sure what you are asking. But I think this code might help you diagnose your problem. It tells you what the count is around the target patches, so you can see if it is doing the correct count. Once you know the counting works, you can then modify for movement.

to testme
  clear-all
  ask patches [set pcolor one-of [yellow green]]
  create-turtles 1 [set heading 30 set color black]
  ask one-of turtles
  [ ask patch-right-and-ahead 60 2 [set pcolor red]
    type "Right: "
    print count ([neighbors4] of patch-right-and-ahead 60 2) with [pcolor = green] 
    ask patch-right-and-ahead -60 2 [set pcolor red]
    type "Left: "
    print count ([neighbors4] of patch-right-and-ahead -60 2) with [pcolor = green]
  ]
end

Upvotes: 2

Related Questions