Netlogo: Creating a square area of patches in the center of the grid - netlogo

I am trying to create a square of brown patches centered at the origin of my grid. Previously, I had a slider on the interface called "sink-patch-radius" that goes from 0 to 20. Then in my code, I created a circular set of patches centered at the origin that were colored brown + 2 and had a radius of "sink-patch-radius", and the surrounding patches were green. Here is the code that worked for that (thank you to JenB for this!):
;;create the 'sink'
let sink-centre patch 0 0
set sink-patches [patches in-radius sink-patch-radius] of sink-centre
ask sink-patches [ set pcolor brown + 2 ]
;; create the 'source'
set source-patches patches with [pcolor != brown + 2]
ask source-patches [ set pcolor green ]
So now, I want that idea to stay the same, but instead of a circular "sink area", I want it to be a square. I know the above code will have to change, maybe even quite a bit, because you cannot use "in-radius" to make a square. I'm thinking of maybe changing the slider to be "sink-patch-length" so that it adjusts the length of the sides of the square. My question would then be: how do I incorporate that into my code, so that I get a square of brown patches centered at the origin? For example, if "sink-patch-length" is set to 20, then I would want a 20x20 square (400 cells) centered at the origin to have a pcolor of brown + 2.
Any help is greatly appreciated! Thank you.

Use the inbuilt coordinate system. So you want (something like) patches with [abs(pxcor) <= sink-patch-length and abs(pycor) <= sink-patch-length]. If you want it centred somewhere other than the middle, you will need to do some fiddling to make the boundaries correct.

Related

How to set patch color in the diagonal Netlogo

Want to draw a river diagonal to the center of the of the world, however ive found that i would have to do a list with all the patches i want to color, is there a faster way to color the diagonal?
Thanks
I need this diagonal, form the middle of the map upwards, Its even tougher to me as its going backwords and doesnt end in the top most middle.
This code:
ask patches with [pxcor = pycor] [
set pcolor green
]
Draws this:
And here's a version that does the diagonal in the upper right corner:
ask patches with [pxcor + pycor = max-pxcor] [
set pcolor green
]

turtles are not moving through the door

Here is my code. I have walls in the colors grey and brown, which turtles should avoid when moving. I have to move my turtles when Eat = 1 toward the door with the color green and enter the room if Eat =0 turtle has to move in black patches only.
I have an issue as turtles are not moving through the door and also when facing the wall they stop there instead of
to go
ask students [
pd
set heading 90
forward 1
if (eat = 1)
[
if (([pcolor] of patch-left-and-ahead 90 1 = green) or ([pcolor] of patch-left-and-ahead 90 2 = green) or
([pcolor] of patch-left-and-ahead 90 3 = green) or ([pcolor] of patch-left-and-ahead 90 4 = green) or
([pcolor] of patch-left-and-ahead 90 5 = green) and pycor <= -10 )
[
set heading towards one-of patches with [pcolor = green]
forward 1
]
if ([pcolor] of patch-ahead 1 = green)
[
set heading 0
forward 3
]
if (pycor >= -9 and [pcolor] of patch-ahead 1 = black)
[
set heading 270
forward 1
]
]
while [ any? patches in-radius 1 with [ pcolor = grey ] or any? patches in-radius 1 with [ pcolor = brown - 2]] [
bk 1
lt (random 180) - 90
fd 1
]
]
tick
end
Your code looks quite chaotic to me so it is hard to give immediate pointers. Instead, let me describe what happens at each part:
let new-patch patch-ahead 2
You define new-patch as the patch in the direction your student is currently facing
if ([pcolor] of patch-here = black ) [
ifelse (eat = 0)
[set heading random 360
forward 1]
[
set heading random 360
forward 1
set heading towards one-of patches with [pcolor = green]
forward 5
]
When a turtle is standing on a black patch, it has two options, depending on whether or not it has any eat. If it has no eat, it faces a random direction and takes a step forward. If it has eat, it also faces a random direction and takes a step forward. After that it chooses a random door and moves 5 steps towards it.
The immediate problems I see with this part are:
What is the purpose of turning in a completely random direction and stepping forward?
Since both the turtles with eat and those without eat have the random walk it, it should not be inside the ifelse blocks.
The random step could make your turtle step onto a grey or brown patch, which I assume you don't want
The students with eat != 1 move towards a random green patch but this green patch they choose can be a different one each tick, causing them to move back and forward between two doors. I suggest either having them pick one door as destination and storing it as a turtle variable, always moving towards that one until they reach it (set my-door one-of patches with [pcolor = green]) or having them move towards the closest door (set heading towards min-one-of (patches with [pcolor = green]) [distance myself])
Here as well, your turtle could walk onto a wall if it is in the direct line towards your door
Onto the next part
if ([pcolor] of new-patch = grey or [pcolor] of new-patch = brown - 2 ) [
right 180
forward 1
]
Here you have the turtle turn around and take a step in the other direction, if the patch that was 2 in front of them at the start was grey or brown.
This part of the code is executed regardless of whether or not the first part was executed, meaning that if the turtle stood on a black patch before, it first chose new-patch as the patch that was 2 in front of it, then it did some random turning and a step forward, and now it checks what color the patch they initially defined as new-patch had, even if they are now looking in a completely opposite direction. This could actually make them turn back towards the patch you are trying to avoid
I'm just wondering, why have new-patch be the patch that is 2 steps ahead and not 1 step ahead?
And finally
if ( pxcor >= -9 and [pcolor] of patch-here = grey or [pcolor] of patch-here = brown - 2)[
left 180
forward 1
]
Here you have a turtle turn around if the patch they are standing on is grey or brown. Not necessarily a bad way of solving them walking into walls but it does not solve all of those instances and furthermove provides problems once you are stuck in those walls
Let's assume you are standing on a grey patch while facing the door. This means that there is generally grey in front and grey behind. Since the student is not standing on black, there is no random walk. The patch 2 ahead is grey so you turn around and take a step. Now you are standing on another grey patch so you turn back and take a step, ending right where you started this tick.
Of course for this to happen you first need to actually end up in the wall. For that we have the forward 5 from earlier to blame. A turtle that is close to the wall and moves 5 steps toward a door can do so through part of a wall. It gets deep enough for both of your 180° turns not to be able to get them out.
Instead of all this turning around, I suggest you add in a local variable called destination let destination patch-ahead 1, let it check whether or not that patch is a wall and only start randomly turning if it is a wall. Then set destination patch-ahead 1 and check again. Only let it move towards destination after all the checking is done.
In general, I suggest you let the turtles put their pen down to more accurately trace their movement ask turtles [pd], reduce the number of turtles and see what happens from step to step
As to your final question of why they don't move through the door: well, in the code there is no command that tells them what to do if the patch they are standing on is green so once they step onto the green they will simply stop. (or if they manage to move through the door, they will turn back towards it (or another door) and with the random walks added in, they will eventually end up either in a wall or on top of the door, both of which make them stop)
Last but certainly not least: start a bit smaller. Use just one or a few turtles, a short wall and a single door. Make sure your movement works on that before scaling up to all those different rooms
I hope this points you in the right direction and will be glad to further elaborate if you have questions

Netlogo: Says its in turtle context when it is not

I have to make a button that colors in the patches in the bottom half of the grid randomly with darker shades of a given color based off of a slider, and the top of half of the grid randomly with lighter shades of a given color based off of the slider. When I wrote my code it says that I can't use my command because it is in patch-context because by command is in turtle-only context. I am confused because I am not using turtles, I am only using patches. Inside of my button I wrote "colorRandomShade2" which is the name of my command. The button is also in patch context and its display name is "colorRandomShade2".
Code:
to colorRandomShade2
if ycor > 0 [ set pcolor ( main_color + random 5 ) ]
if ycor < 0 [ set pcolor ( main_color + random 9 ) ]
end
Patch coordinates are pxcor and pycor, but you have used xcor and ycor, which are the variable names for coordinates of a turtle. So you are pressing the button to call the code and the first thing that NetLogo sees is a request to look at the ycor or y-coordinate of a turtle.

Net Logo Turtle disappears inside of Patch

In Net Logo is there a value for a turtles Z coordinate, or its height in the 3D View? I have white patches in my simulation, when my turtles go into those patches they are not visible until they emerge from the other side of the patch.
In the screen shot, you can see edges of turtles inside of my white patches, I want to display them on top of the patch.
How can I do this?
I found the issue, simply use
set zcor 2
will place the turtles on top of the patches as I had hoped.

How can I simulate a moving patch?

I'm trying to make a Frogger-like game where the patches move around and the turtle can only move onto a safe patch. If I have a few patches that are red, how can I 'move' them around as if they were turtles? I currently have this, but it seems to move more than 1 patch at a time occasionally and as a result, some red patches will be destroyed if there are more than one that is red:
if pcolor = red
[ ask patch-at 0 1
[ set pcolor red]
set pcolor black ]
You could ask the neighbor patch (left or right) to be painted red and the actual patch to be painted back to black or whatever the default color is.
For this you'll want to get the actual patch coordinates. Using patch-at asks the patch at 0,1 relative to the whole world.