Show patch coordinates in Netlogo - netlogo

I'm using the following code at the command center to show the coordinates of a random patch:
ask one-of patches [show pycor pxcor]
but I get an Expected command error, how can I fix it?

show takes a single argument, and you are passing it two- pxcor and pycor. It depends how you want to display this, but depending on your goal you could do:
observer> ask one-of patches [show pycor show pxcor]
(patch -15 0): 0
(patch -15 0): -15
Or more likely
observer> ask one-of patches [show list pycor pxcor ]
(patch 16 8): [8 16]

show takes only one input, so you need the patch to perform two separate actions. ask one-of patches [show pycor show pxcor] will print both coordinates:
observer> ask one-of patches [show pycor show pxcor]
(patch -9 10): 10
(patch -9 10): -9
If for some reason you don't like the fact that this results in the output being shown on two lines, you can use word and have it reported as a single string: ask one-of patches [show (word pycor " " pxcor)]:
observer> ask one-of patches [show (word pycor " " pxcor)]
(patch -9 4): "4 -9"
Edit Using a list, as in Luke's answer, looks certainly more elegant than using a string with manually-inserted spaces... what a dumb choice lol

Related

Save the times an action has happend then do something Netlogo

I dont have code as I dont really know how to do it. i have some patches of colore brown and at random the turn green for one tick and then they become brown again, it loops. I want to know if there is a way to ask a turtle to know how many times that patch has changed to green.
Thanks in advance.
A variable like you describe is often called a counter. In this case, you probably want it to be a patches-own variable so that each patch can individually track how many times a given event has occurred. In the example below, times-turned-green starts at zero (by default, that is the value for any variable declared in Netlogo) and the patches all update their own 'personal' times-turned-green whenever they change colour.
patches-own [ times-turned-green ]
to setup
ca
ask patches [ set pcolor brown]
reset-ticks
end
to example
repeat 100 [
ask patches [
set pcolor brown
if random-float 1 < 0.05 [
set pcolor green
set times-turned-green times-turned-green + 1
]
]
]
ask n-of 5 patches [
show times-turned-green
]
end
Output in the Command Center should look similar to:
(patch -6 14): 2
(patch -9 4): 6
(patch 2 1): 8
(patch -3 3): 4
(patch -5 12): 5

Obstacle avoidance (in cone)

I am trying to create a command so the turtles would avoid the green patches in my map.
to avoid-obstacle
let front-patches patches in-cone 5 60
if pcolor of one-of front-patches = green [set heading heading - 45]
end
unfortunately gives me
of expected this input to be a reporter block, but got a number or list instead
any idea how to fix it?
The error message is saying that the command of wants a reporter block, which is a code block (in square brackets) that returns something. In this case it wants to get the value of the variable pcolor from the specified turtle. Try this (note the brackets around pcolor):
to avoid-obstacle
let front-patches patches in-cone 5 60
if [pcolor] of one-of front-patches = green [set heading heading - 45]
end
By the way, it's not going to do exactly what you have described. This code will randomly select one of the front-patches (that's what one-of does) and check if it's green. From your description, I think you want to change direction if any of them are green. That would look like:
to avoid-obstacle
let front-patches patches in-cone 5 60
if any? front-patches with [pcolor = green] [set heading heading - 45]
end

How to get "only the x-coordinate" of a randomly selected turtle from the turtles that exist in a straight line on the x-axis?

How do I get only the x-coordinate of a randomly selected turtle from the turtles that exist in a straight line on the x-axis and make it a variable?
For example, here's a sample code, but it doesn't work.
globals [x-only]
;omitted
ask one-of turtles with [xcor < max-pxcor and pycor < 1 and pycor > -1][
set x-only list xcor ycor ;I might be wrong here, but there are no good syntax.
move-to patch x-only 1
]
;omitted

Netlogo: How can I ask one of the turtles in multiple patches?

How can I ask one of the turtles in multiple patches?
I want to select one of the turtles in the specified patches.
(or I want to ask one turtle in a specified range of cells)
For example, I want to use the following syntax: But it doesn’t work.
ask turtles-on patch (1, 0) or (2, 0) or (3, 0)
move-to patch max-pxcor 1
or
ask turtles with [(50 0) < max-pxcor]
move-to patch max-pxcor 1
Your first problem is the way you are identifying patches. For example, the correct form of "patch (0, 1)" is patch 0 1 - no brackets and no comma. You used this syntax correctly in the move-to bit of your code.
To make the first version work, you need to create a patch set of the patches and then select from that:
ask turtles-on (patch-set patch 1 0 patch 2 0 patch 3 0)
[ move-to patch max-pxcor 1
]
I'm not really sure what your logic is intended to be on the second version since you are comparing (sort of) a number to another number. Even if you had the syntax correct, it would be either true for all turtles or false for all turtles. Based on your first example, I suspect you want the turtles to look at their own patch and, if pxcor is < 50 and the pycor is 0, you want them to move. That would be:
ask turtles with [pxcor < 50 and pycor = 0]
[ move-to patch max-pxcor 1
]
If you want the turtles with all the patches in the row (not just the ones up to 50), then you can do:
ask turtles with [pycor = 0]
[ move-to patch max-pxcor 1
]
Note that the second and third examples use the fact that a turtle has automatic access to the variables owned by the patch where it is located.
Note that if you only want one of the turtles (as you say in your text but not in the example code) then you need ask one-of turtles instead of ask turtles.

How to kill of all turtles except for one?

Im making a race model. the function is that the turtles move across the grid horizontally starting at xcor -13 to xcor 13 at speeds that
constantly vary and when a turtle reaches xcor = 13, all of the other turtles (besides the one that crossed first) die
to Race
wait .3
fd random 5
if xcor = 13 ( this is where i want to tell all other turtles to die )
end
how do i ask all other turtles to die?
THe first answer doesnt help me, someone else please respond
You can do that by asking the winner to ask other turtles [die]
to setup
clear-all
reset-ticks
;resize-world min-pxcor max-pxcor min-pycor max-pycor
resize-world -15 20 0 3
set-patch-size 15
;set-patch-size size
create-turtles 10
[setxy -13 1 set heading 90 set shape "car" wait 0.3]
ask patch -13 2 [Set plabel "Start" set pcolor 110] ; just for visualization
ask patch 13 2 [Set plabel "END" set pcolor 110]
end
to go
ifelse count turtles > 1
[
ask turtles
[Race]
]
[stop]
tick
end
to Race
fd random 5
if xcor >= 13 [ set size 2 ask other turtles [die] ]
end
This is a sample screenshot
I really low examples, so there is another way to improve visualization of the race by having multiple lines of cars:
resize-world -15 20 0 5
set-patch-size 15
create-turtles 20
[set xcor -13 set ycor one-of [0 1 2 3 4 ] set heading 90 set shape "car" ]
ask patch -13 5 [Set plabel "Start" set pcolor 110]
ask patch 13 5 [Set plabel "END" set pcolor 110]
What you are trying to do doesn't make much sense. When posting, please make sure that the question provides a better context for answering you question.
My interpretation of your question is that the cars don't matter at all. All you want to figure out is that when one turtle crosses the finish line, you kill all the other turtles.
Doing so I would probably give each turtle a property of a name or label. Store them all in an array.
Then if the turtle crosses the finish line, remove all the turtles from the array except for
if turtle.name == turtle[i].name.
Hope that helps. Please be a little more clear next time.
You need a way to identify the living turtle and kill the other turtles. To do this, you could either write a ton of if statements but it would look horrible.
if(larry.coordinates == 13){
kill(tom);
kill(harry);
}
Your best bet would be to read how to create an array. Store it in an array. Trust me, arrays are pretty simple.