I'm trying to recreate the bubble shooter game where there are color bubbles in rows that you pop by angling bubbles of the same color in that direction using the mouse. However, since I am using turtles as the bubbles, when I create more than 2 to shoot, the turtles do not shoot in the order of their who number and I can't tell what color I'm using unless I use inspect.
I tried to use foreach to ask each turtle to list their colors into a list that is displayed and organize them to be shot in the order of their who numbers. I was able to sort them into a list based on their who numbers to display their colors, but I can't order them to be shot out in the correct order.
Here is my code so far:
;creates the bubbles to be shot
set list1 []
cro 10
[set color one-of [red green blue violet]
setxy 0 -12
set size 3
set shape "circle"]
foreach sort-on [who] turtles with [who >= 80] with [who <= 99]
[the-turtles -> ask the-turtles [set list1 lput [color] of self list1]]
user-message "The list in the command center shows the colors of the
bubbles listed here in consecutive order. Refer to the note on the side
of the screen for refernce. Click game1 to angle and fire"
show list1
to game1
;creates the launcher from which turtles are fired from
ask turtle 1 [face (patch mouse-xcor mouse-ycor)]
;moves the bubbles to the target
foreach sort-on [who] turtles with [size = 3]
[the-turtles -> ask the-turtles [
if mouse-down? [ask the-turtles [wait .1 move-to patch mouse-xcor
mouse-ycor
if count turtles in-radius 4 with [color = [color] of self] with [size
= 4] >= 3
[ask turtles in-radius 2 with [color = [color] of self] with [size >=
3] [die]]]]]]
end
;size 3 is the size of the turtles I'm firing with the mouse
;size 4 is the size of the rows of turtles that will be "popped"
Instead of outputting the turtles shot in the order of their who number, the turtles are ordered randomly in comparison to my ordered list1. Any advice is appreciated thank you!
Related
I need to "do-something-special" if around yellow turtle there is at least 3 blue turtles . Is the code bellow correct?
I tried
ask turtles with [color = yellow]
[
if count turtles in-radius 1 with [color = blue] >= 3
[do-something-special]
]
do-something-special should remove (disappear) 3 of blue turtles and current yellow one
Did I do the location of relevant turtles correctly and how do I kill them after I find them?
Hannah's answer is good but the linked example won't fully fix your problem. Since you will be using the set of close agents twice (once to count and once to potentially remove some), you should also create an agentset for efficiency reasons (you don't want to create it twice). Here is a full solution.
ask turtles with [color = yellow]
[ let near-blue turtles in-radius 1 with [color = blue]
if count near-blue >= 3
[ ask n-of 3 near-blue [die]
die
]
]
Also, if you don't care about the exactness of the radius, an alternative to turtles in-radius 1 would be turtles-on (patch-set neighbors patch-here), which is all the turtles on the neighbouring and same patches to wherever your asker turtle is sitting.
At the moment your code counts the amount of turtles that are blue in the radius of one patch around the yellow turtle. If the amount of blue turtles is bigger/equal 3 the yellow turtles die if you use the "die" command instead of "do-something-special". So it looks as follows.
ask turtles with [color = yellow]
[
if count turtles in-radius 1 with [color = blue] >= 3
[die]
]
Maybe you can merge the code with the following example and then kill the neighbors.
I want to extract a node with highest degree centrality from the network. I don't want to extract a node with max links only. I want to extract the node along with the nodes adjacent to it.
Below is the code. In this code, I have loaded a network using nw extensions.
extensions [nw]
turtles-own [ explored? ]
to setup
ca
crt 25
ask turtles [fd random 15]
load-graph
extract_deg
end
to load-graph
let filename user-file
if (filename != false) [
nw:load-graphml filename [
set shape "circle"
set size 1
]
nw:set-context turtles links
]
end
to extract_deg
let n turtles with [my-links = max [count link-neighbors] of turtles]
ask n [show other turtles network:in-link-radius 1 turtles]
end
to layout
ask turtles [ set size sqrt count my-links ]
layout-spring turtles links 0.5 2 1
ask turtles [
facexy 0 0
fd (distancexy 0 0) / 100 ]
end
The code below will choose one of the nodes with largest degree (just remove one-of if you want all of them), turn it red and make its network neighbours green.
You don't need the expression [my-links = max [count link-neighbors] of turtles], standard NetLogo includes the very useful with-max primitive. However, I think your construction would have worked if you had counted my-links (like let n turtles with [count my-links = max [count link-neighbors] of turtles]). Then you have some syntax errors in the next line (the extension is nw and you don't need the turtles.
to extract_deg
let maxk-set one-of turtles with-max [count my-links]
ask maxk-set
[ set color red
ask other nw:turtles-in-radius 1 [set color green]
]
end
I am teaching myself how to create ABMs in Netlogo using the book of Railsback & Grimm 2012. I am having trouble with one book exercise which is on butterflies following "virtual" corridors. Basic idea is that butterflies go uphill for mating using the differences in height as guide. I need to calculate the width of the corridors dividing the number of patches used by the butterflies over the average distance the butterflies fly from the start patch to the end patch. I am
struggling with plotting this corridor width, which I am coding like this:
to-report corridor-width
let patches-visited count patches with [used?]
let mean-distance mean [distance start-patch] of turtles
report patches-visited / mean-distance
I then created a plot in the interface with the command:
plot corridor-width
The error message I get reads:
Division by zero. error while observer running / called by procedure
CORRIDOR-WIDTH called by plot 'Corridor width' pen 'default' update
code called by procedure SETUP called by Button 'setup'
I believe there is something wrong with the way I am coding distance start-patch but I have surfed the web and looked at several codes and I cannot spot my mistake. My whole code looks like this:
globals [ q ] ;; q is the probability that butterfly moves directly to highest patch
turtles-own [ start-patch ]
patches-own [ elevation used? ] ;; patches property of elevation and whether the patch has been used by butterfly or not.
to setup
ca
;; Let's create patches and asign them an elevation and color by using ask patches statement
ask patches
[
;; Elevation decreases linearly with distance from the center of hills. Hills are at (30,30) and
;; (120,120) coordinates. The first hill is 100 units high whereas the second one is 50
let elev1 100 - distancexy 30 30
let elev2 50 - distancexy 120 100
ifelse elev1 > elev2
[ set elevation elev1 ]
[ set elevation elev2 ]
set pcolor scale-color green elevation 0 100
set used? false
]
;; Create 50 butterflies
crt 50
ask turtles [
set size 6
;; set their initial location as their initial patch
setxy random-pxcor random-pycor
set start-patch patch-here
;; have the butterfly draw its path with the pen-down statement
pen-down
]
reset-ticks
;; Initialize the q parameter
set q 0.4
end
;; The master schedule
to go
ask turtles [ move ]
plot corridor-width
tick
if ticks >= 1000
[
let final-corridor-width corridor-width
write "Corridor width: " print final-corridor-width
;export-plot "Corridor width" (word "Corridor-width-output-for-q-" q ".csv")
stop
]
end
;; let's code the butterfly procedure of movement
to move
if elevation >=
[ elevation ] of max-one-of neighbors [ elevation ]
[ stop ]
ifelse random-float 1 < q ;; Decide whether to move to the highest sorrounding
;; patch with p=q
[ uphill elevation ] ;; move deterministically uphill
[ move-to one-of neighbors ] ;; or move randomly
set used? true
end
to-report corridor-width
let patches-visited count patches with [used?]
let mean-distance mean [distance start-patch] of turtles
report patches-visited / mean-distance
end
What happens when the mean-distance is 0?
let mean-distance mean [distance start-patch] of turtles
Essentially, in your setup, you set all the turtle's start-patch to their current patch. So, if you ask all the turtles how far away they are from their start patch, they will all tell you 0 units away.
So, [distance start-patch] of turtles is filled with a list of all 0's.
Thus, a mean of a list of all 0s is 0 causing your divide by 0 error.
Maybe in this situation, you want to report 0 instead...so
ifelse mean-distance = 0
[ report 0]
[report patches-visited / mean-distance]
I want to let my turtle move to certain patch and make a "splotch". The central patch = my turtle location, can be selected randomly, however satisfy two conditions:
has to be in a certain distance from turtle's actual position
has to surrounded (neighbors in certain radius) by patches with specific quality
The reason is to create kind of "buffers" around my turtle's position, with aim to obstruct the close proximity of my clumps.
Please, how can I satisfy these two conditions?
As far, I have:
to go
ask turtles [
; select one of patches in specific distance, and
; surrounded by patches with no magenta color
let aaa one-of patches with [distance myself > 3 and
all? neighbors with [pcolor != magenta]]
; how to write this condition above ??
; and how replace "neighbors" by "in-radius"??
move-to aaa
ask neighbors [ ; create clump of magenta patches
set pcolor magenta ]
ask patch-here [ ; set central patch to magenta
set pcolor magenta ]
]
You're almost there; you just need to reread the documentation for all? and any?.
let _candidates patches with [distance myself > 3]
set _candidates _candidates with [
not any? (patches in-radius 3 with [pcolor = magenta])
]
let aaa one-of _candidates
If it is possible that there will be no candidates, you should guard against that.
I have the code :
if color of patch-here = blue [set heading towards one-of patches in-radius 180 with pcolor = grey ( do that or ) die ]
What I want to be able to do is to allow each turtle to randomly select one of the two options. I appreciate I could assign each command a number and then use a random number generator to select one of the two commands, but I am wondering whether there is a combination of commands that I could use to replace ( do that or ).
Thanks.
Roll a dice, if dice < 5, do something, else do the other thing. Example:
if pcolor = blue [
let dice random 10
ifelse dice < 5 [
set heading towards one-of patches in-radius 180 with pcolor = grey]
[die]
]