简体   繁体   中英

And if statements in NetLogo

I am trying to create a simulation of bees spreading pollen, I was hoping to make it so when a bee visits a flower it changes the colour to blue and the turtle 'bee' gets given a pollen value of +1, then when it has a pollen value of +3 and visits a flower the colour changes to green.

I have tried multiple methods to go about this, if there was an and if statement that would be useful eg

to pollenate 
ask turtles [
if pollen > 3
and if pcolor = yellow or blue [ 
set pcolor white
set pollen pollen - 3
]
]
end 

I'm not sure if and statements exist though.

so I tried with when the turtles have a pollen count of higher than 3 they change color, then tried the code below but keep getting an error message.

to pollenate
ask turtles with [ color = white ] [
if pcolor = blue
[set pcolor = green
set pollen pollen -3
]
]
end

I keep getting the error 'Set expected 2 inputs'

any help on either approach would be greatly appreciated, or if i'm doing the whole thing wrong please let me know haha.

Here's the answers to your two questions.

(1) "and if " can be accomplished by using a logical AND in the test. I HAD to use parentheses around the color tests that were connected by OR and then AND that to the pollen > 3 test. As in mathematics, the inner parentheses are evaluated first. I prefer to use parentheses around all logical tests Like ( pollen > 3) -- it's not required but I find it much easier to read.

to pollenate 
ask turtles [
    if (pollen > 3) and ((pcolor  = yellow) or (pcolor = blue))
            [ 
               set pcolor white
               set pollen pollen - 3
           ]
]
end 

you could use a test for pcolor being found in a list of colors too, like this;

to pollenate 
ask turtles [
    if (pollen > 3) and (member? pcolor [yellow blue] )
            [ 
               set pcolor white
               set pollen pollen - 3
           ]
]
end 

As to your second question, the working version would be

o pollenate
ask turtles with [ color = white ] [
if pcolor = blue
[set pcolor = green
set pollen pollen - 3
]
]
end

Didn't see the difference? I put a space on both sides of the minus sign, otherwise Netlogo sees three parameters: pollen, pollen, and -3 instead of two you want,namely pollen and (pollen - 3).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM