i a new to Netlogo. I have to design a chess board in Netlogo with pawns on it. but i really don't know how to do it. please help needed.I have drawn a little bit but it's not acceptable i want fully like as it does in real. thanks
NetLogo makes that kind of thing relatively easy. The most straightforward approach is probably to use patches as board squares. Here is some code to get you started:
to setup
clear-all
; create-board:
resize-world 0 7 0 7
set-patch-size 80
let square-colors (list (brown - 2) (brown + 2))
(foreach sort patches range count patches [ [p i] ->
ask p [ set pcolor item ((i + (pycor mod 2)) mod 2) square-colors ]
])
; create pieces (just pawns in this example):
ask patches with [ pycor = 1 or pycor = 6] [
sprout 1 [ set shape "pawn" ]
]
; set pieces color to white or black depending on their location:
ask turtles [
set color ifelse-value (pycor < 4) [ white ] [ black ]
]
end
This assumes that you have defined a "pawn" turtle shape. You'll have to make it using the Turtle Shapes Editor, and do the same for "king", "queen", "bishop", "knight" and "rook".
You'll have to figure out the rest by yourself. Ask questions here if you're stuck, but try to keep them precise and show us code for what you tried...
Related
I'm working on a smaller project and got stuck on an issue, I'm not really sure if it's possible to solve it in NetLogo but I want to give StackOverflow a go!
I got a model that divides the world into different parts and randomly add physical features (such as rivers). If a feature goes through the whole region, I want it to separate the region and make into two regions. As an example, in the picture below, I want to separate the purple region into two unique regions accordingly to the physical feature (black).
The code I used to generate the picture above, can be found below.
to setup
ca
;Setting world.
resize-world 0 19 0 19
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1][
while [x <= max-pxcor + 1 ][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 10
]
set x 5
set y y + 5
]
;Generating physical features.
ask n-of 5 patches[ sprout 1[
set pcolor black]
]
let i 0
while [ i < (max-pycor * 2 )][
ask turtles [
fd 1
set pcolor black
ifelse (random 20 <= 1)
[
rt one-of [-90 0 90]
forward 1
]
[
fd 1
set pcolor black
fd 1
set pcolor black
]
set pcolor black
set i i + 1]
]
ask turtles [die]
end
My strategy for handling this is to realize that all we really need to do is "flood" a patch out by color and tag all the found adjacent patches, then repeat for any un-tagged, non-black patches until they are all done.
NetLogo does not have a "flood" command to get all patches adjacent to a patch meeting a criteria, so we make a special reporter of our own to handle it, patches-adjacent. Then it's just easy to ask those patches-adjacent to set their region to the currently chosen region.
I don't love this code, it's a little finicky and would be prone to infinite loops if tweaked incorrectly, but it should work. I bet there is a cleaner way to do this that I'm not thinking of at the moment.
; add a variable to track the different regions
; the default value will be `0` for each patch when `clear-all` is called
patches-own [ region ]
to set-regions
let current-region 1
; only act on non-black patches that haven't yet been assigned a region
let untagged patches with [ region = 0 and pcolor != black ]
while [any? untagged] [
ask one-of untagged [
ask patches-adjacent [
set region current-region
]
]
; update the region and the untagged patches we have left to process
set current-region current-region + 1
set untagged patches with [ region = 0 and pcolor != black ]
]
; this is just to get a view of the regions to quickly see if our code worked, it can be removed
ask patches [ set plabel region ]
end
to-report patches-adjacent
report patches-adjacent-ex (patch-set self) pcolor
end
to-report patches-adjacent-ex [found pc]
let newly-found neighbors4 with [ (not member? self found) and pcolor = pc and region = 0 and pcolor != black ]
set found (patch-set found newly-found)
ask newly-found [
; use recursion to find the patches adjacent to each newly-found one
; relying on updating the `found` agentset as we go to avoid duplicates
; or looping forwarder
set found (patches-adjacent-ex found pc)
]
report found
end
I solved this by using the Patch Clusters model that can be found in the NetLogo model library.
I am new to NetLogo, so my apologies in advance if that question is very stupid. I would like to create an Agent-Based Model where animals move around in a complex terrain looking for water sources. Movement should be downhill, constrained by steep slopes (>25°) and targets should be lakes. I am using a real-world example from GIS data for this, and I have already managed to setup a world containing an ASCII elevation grid, a shapefile containing lines that represent slopes steeper 25degrees and a shapefile containing areas representing lakes. I have created animals (cows) and found a code line telling them to move downhill. Now, I would like to tell them
a) to avoid slopes >25° by using the slope shapefiles as obstacles and
b) to go to the lakes by using the lake shapfiles as targets
Can someone help me how to code this?
Many thanks in advance!
Here is the code I have put together so far
breed [ cows cow ]
extensions [ gis ]
patches-own [ elevation ]
globals [
slope-dataset
lake-dataset
elevation-dataset
]
to setup-terrain
clear-all
reset-ticks
set slope-dataset gis:load-dataset "FILENAME.shp" ;;extent of GIS datasets is N42.3-43.4 and W120.0-121.1
set lake-dataset gis:load-dataset "FILENAME.shp"
set elevation-dataset gis:load-dataset "FILENAME.asc"
gis:set-world-envelope gis:envelope-of slope-dataset
gis:set-world-envelope gis:envelope-of lake-dataset
gis:set-world-envelope gis:envelope-of elevation-dataset
end
to display-slopes
gis:set-drawing-color red
gis:draw slope-dataset 0.5
end
to display-lakes
gis:set-drawing-color blue
gis:draw lake-dataset 2
end
to display-elevation-in-patches
gis:apply-raster elevation-dataset elevation
let min-elevation gis:minimum-of elevation-dataset
let max-elevation gis:maximum-of elevation-dataset
ask patches
[ ; note the use of the "<= 0 or >= 0" technique to filter out
; "not a number" values, as discussed in the documentation.
if (elevation <= 0) or (elevation >= 0)
[ set pcolor scale-color black elevation min-elevation max-elevation ] ]
end
to setup-cows
set-default-shape cows "cow"
create-cows 100 [
setxy random-pxcor random-pycor
set size 1
set color white
]
end
to move
move-to patch-here ;; go to patch center
let p min-one-of neighbors [elevation]
if [elevation] of p < elevation [
face p
move-to p ;; makes cows move to the next lower elevation patch, if no lower
elevetion is present, cow doesn't move
]
end
to go
ask cows [
move
]
end
Thanks Seth, I have found the solution using the gis:intersects? primitive you suggested:
to display-slopes
ask patches gis:intersecting slope-dataset
[ set pcolor red ]
end
to go
ask cows
[fd 1
avoid-ostacles]
tick
end
to avoid-obstacles
ask cows [
if [pcolor] of patch-ahead 1 = red
[rt 90 fd 1]]
end
This way I can color the patches in red that intersect with the shapefile containing the slope vector dataset and then ask the cows to avoid and move around red colored patches.
Thanks again!
Netlogo only provides four topology options that dictates what turtles and patches do when they reach the any given edge of the netlogo world or interface, namely, box, torus, horizontal wrapping and vertical wrapping ... Is there a way to adjust these options so that just one edge is wrapped? Or three edges are wrapped for instance?
Nigus- I do remember, I never forget a Corgi! Anyway, as Jen mentioned there is no built-in for this, but you can build it into your turtle movement rules. For example, with this setup:
to setup
ca
crt 10 [ pd ]
reset-ticks
end
If you want your turtles to treat a boundary as closed, you could have them check their patch-ahead by whatever their movement speed is (example speed of 1, here) and do a simple math operation to see if they are 'allowed' to wrap at that boundary. To have the left boundary closed, try:
to left-closed ; turtle procedure
ask turtles [
let target patch-ahead 1
if ( ( [pxcor] of target ) - pxcor ) <= 1 [
fd 1
]
]
tick
end
To have the right boundary closed, you can just do the opposite:
to right-closed ; turtle procedure
ask turtles [
let target patch-ahead 1
if ( pxcor - ( [pxcor] of target ) ) <= 1 [
fd 1
]
]
tick
end
Obviously, this is a very simple example and would need some work to massage it to fit your current movement rules.
Edit:
To show this in action, check out this example code. First, a modified setup that specifies where turtles should spawn and also creates a 'wall' of red patches:
to setup
ca
ask patches with [ pxcor = 5 ] [
set pcolor red
]
ask patch -5 0 [
sprout 10 [
pd
]
]
reset-ticks
end
Now, a modified version of the right-closed procedure above that turns it into a to-report:
to-report closed-border-right? [ target-patch ]
report ( pxcor - ( [pxcor] of patch-ahead 1 ) ) <= 1
end
Now, we should expect that turtles should not be able to cross the red wall. They should also not be able to travel off the right border, but they should be able to cross the left border. So, if the turtles are free to wander, using this movement procedure:
to move-example
ask turtles [
rt random 61 - 30
let target patch-ahead 1
if closed-border-right? target and [pcolor] of target != red [
fd 1
]
]
tick
end
We should expect them to eventually get 'trapped' between the unwrapped border and the red wall- and this happens as we expect, once the turtles make their way through the left border, they cannot get back:
layout intersections with traffic lights. I want the car to stop when the red lights and the lights go xanh.thi what I should use for car algorithm can recognize the color of the lights
I have used light layout algorithm as follows
to draw-stop-light
ifelse stop-light = "north"
[
ask patch -7 12 [set pcolor green ]
ask patch 10 10 [ set pcolor red ]
ask patch -10 -10 [ set pcolor red ]
]
[
ask patch -7 12 [ set pcolor red ]
ask patch 10 10 [ set pcolor green ]
ask patch -10 -10 [ set pcolor green ]
]
end
This is really a question better suited to the NetLogo users group here than Stack Overflow since you are asking how to program a poorly defined specification. For example, the users group may be able to point you to a similar model. Stack Overflow is really about fixing specific problems with your code. However, here's something to get you started until the question gets deleted.
Break down what you want to happen into very small steps and then code each step. The steps will be something like:
When the car gets near the intersection, check to see what colour the traffic light is
If the light is red, remember to stop at the intersection
Stop at the intersection if required
If at the intersection, wait until the light turns green then go
Do NOT try to write the whole thing. Instead, write one bit at a time and test is to see if it works. Here is some approximate code for the first bit:
ask cars
[ ifelse heading = 180 and ycor > 20 and ycor < 25 and not stop-flag
[ if [ pcolor of patch -7 12 ] = red [ set stop-flag true ] ]
[ ... (cars from other directions)
]
ask cars
[ forward speed
if stop-flag and ... (on intersection patches)
[ ... (move backwards to be outside the intersection)
set stop-flag false
]
]
patch-right-and-ahead (http://ccl.northwestern.edu/netlogo/docs/dictionary.html#patch-lr-and-ahead) could be useful here. Perhaps something like:
if [pcolor] of patch-right-and-ahead 90 1 = red
[ set speed 0 ]
Following on from How to control square size in a grid from square area?, I would like to randomly remove block side in a grid. Here my idea:
%% In a first time, I randomly select a white patch in the grid (figure below):
let random-patch one-of patches with [pcolor = white]
%% Then, I draw red patches to identify intersections between white roads (figure below):
ask patches with [ (pxcor mod (side + 1) = 0 ) and (pycor mod (side + 1) = 0 ) ] [set pcolor red]
%% Finally, I color in brown all white patches that are situated between two red patches and on the same side than the random patch. To identify these white patches, have I to calculate distance between the random patch and the nearest red patches and to color all white patches situated along these distances ?
Thanks in advance for your help.
An alternative way to think about your problem is in terms of finding clusters of white patches: you're picking a white patch at random, and you want to turn all the contiguous white patches to brown.
You can look at the "Patch Clusters Example" in the Code Examples section of NetLogo's model library to see one way to do this.
Here is how I would do it. Let's start by defining a grow-cluster reporter:
to-report grow-cluster [ current-cluster new-patch ]
let patches-to-add [
neighbors4 with [
pcolor = [ pcolor ] of myself and
not member? self current-cluster
]
] of new-patch
let new-cluster (patch-set current-cluster new-patch)
report ifelse-value any? patches-to-add
[ reduce grow-cluster fput new-cluster sort patches-to-add ]
[ new-cluster ]
end
The code may be hard to understand if you're not used to functional programming because it uses recursion within a call to reduce. Still, in a nutshell, it:
takes an existing patch cluster and a new patch to add to this cluster
looks for neighbors of that new patch that should also be added to the cluster, i.e., those that are the same color but not already part of the cluster
calls itself for these new patches to add so that their neighbors can be added to the cluster (and those neighbors' neighbors, etc.) until no new patches are found.
Once you have grow-cluster, you can use it to accomplish exactly what you want by seeding it with an empty cluster and the random patch that you selected:
to remove-random-side
let random-patch one-of patches with [pcolor = white]
let side grow-cluster no-patches random-patch
ask side [ set pcolor brown ]
end
Caveat: for this to work, world wrapping has to be disabled in your model settings.
Since you're making a uniform grid, you might consider just doing math on pxcor and pycor instead of taking the Patch Clusters Example approach. That approach is best suited to dealing with irregular shapes.
To set up your grid, you can just do:
ask patches [
set pcolor brown
let horizontal-street? pycor mod (side + 1) = 0
let vertical-street? pxcor mod (side + 1) = 0
if horizontal-street? or vertical-street?
[ set pcolor white ]
if horizontal-street? and vertical-street?
[ set pcolor red ]
]