Reputation: 13
I'm trying to crate a complex environment, where three types of trees co-exits and you can modify the numbers of each type using a slider. What I'm having problems implementing is a "day/night" cycle. When it is night the colors should turn darker, but with the code I have the colors get darker and never return to a brighter color. I used as a base the Algae model.
Here is my code:
to setup
clear-all
setup-world
reset-ticks
end
to setup-world
ask n-of synchronic-tree-density patches [
set pcolor blue
]
ask n-of asynchronious-tree-density patches [
set pcolor yellow
]
ask n-of tree-patches patches [
set pcolor green
]
recolor-world true
end
to recolor-world
ask patches [
if pcolor = blue [
ifelse setting-up? or daytime? [
set pcolor blue
] [
set pcolor blue - 3
]
]
if pcolor = yellow [
ifelse setting-up? or daytime? [
set pcolor yellow
] [
set pcolor yellow - 3
]
]
if pcolor = green [
ifelse setting-up? or daytime? [
set pcolor green
] [
set pcolor green - 3
]
]
]
end
to go
recolor-world false
tick-advance 1
end
to-report daytime?
report ticks mod 24 < day-length
end
Upvotes: 1
Views: 164
Reputation: 17678
Luke's answer addresses your issue. But you might additionally want to simplify the code somewhat to only assess the state of daytime?
once each tick. For example:
to recolor-world
if-else daytime? [
ask patches with [ tree-type = 1 ] [ set pcolor blue ]
ask patches with [ tree-type = 2 ] [ set pcolor yellow ]
ask patches with [ tree-type = 3 ] [ set pcolor green]
] [
ask patches with [ tree-type = 1 ] [ set pcolor blue - 3 ]
ask patches with [ tree-type = 2 ] [ set pcolor yellow - 3 ]
ask patches with [ tree-type = 3 ] [ set pcolor green - 3 ]
]
end
Upvotes: 2
Reputation: 10291
welcome to Stack Overflow. Please have a look at the MCVE guidelines for some asking tips. Ideally your question code is pared down to just what is necessary for other users to run your program- the goal being that they can just copy your code as is. Right now, I can't run your program without a fair bit of modification- I'm not sure if my solution will apply to your setup. You are more likely to get useful answers if you simplify your code.
That said, I'm pretty sure your issue comes from that fact that all your if statements in your recolor-world
procedure. Consider this one:
if pcolor = green [
ifelse daytime? [
set pcolor green
] [
set pcolor green - 3
]
]
So on the first go-around, you do have some green patches since they were set that way in the setup
procedure. However, once daytime?
becomes false
, those patches run the command set pcolor green - 3
and so they no longer evaluate if pcolor = green
as true- they will never run that code block again. I think the easiest fix is to use a grouping variable other than color for filtering:
patches-own [ tree-type ]
to setup
clear-all
reset-ticks
setup-world
end
to setup-world
ask n-of 50 patches [
set pcolor blue
set tree-type 1
]
ask n-of 50 patches [
set pcolor yellow
set tree-type 2
]
ask n-of 50 patches [
set pcolor green
set tree-type 3
]
end
to recolor-world
ask patches with [ tree-type = 1 ] [
ifelse daytime? [
set pcolor blue
] [
set pcolor blue - 3
]
]
ask patches with [ tree-type = 2 ] [
ifelse daytime? [
set pcolor yellow
] [
set pcolor yellow - 3
]
]
ask patches with [ tree-type = 3 ] [
ifelse daytime? [
set pcolor green
] [
set pcolor green - 3
]
]
end
to go
recolor-world
tick
end
to-report daytime?
report ticks mod 24 < 12
end
Edit
See @JenB's further improvement in her answer for a more efficient implementation.
Upvotes: 1