This site requires JavaScript, please enable it in your browser!
Greenfoot back
Phoenix36
Phoenix36 wrote ...

2018/11/22

Bee production

Phoenix36 Phoenix36

2018/11/22

#
I'm stuck trying to figure out how to do a loop like the one it describes: The hive should do nothing for the first 400 turns. After that, the hive should release one bee at a time, randomly placing the bee in one of the five spots from (9, 1) - (9, 5). The hive should then wait a random delay between 80-400 turns before launching the next bee. Here is what I have, but I'm still trying to configure it. public void act() { int spawn = Random.generator().nextInt(321) + 80; while (turns == 401) { this.produce(); bumbleBees--; turns = 0; break; } if (turns == 0) { this.produce(); } turns++; Can anyone give me a tip or show how I can format it the way this description is?
Super_Hippo Super_Hippo

2018/11/22

#
1. Don't create a new random number (spawn) each act cycle. Despite the fact that you don't use that variable at all. Use a timer and set it to a random number once when needed. 2. What do you think does while (turn == 401) do? SPOILER:
private int beeTimer=400;

public void act()
{
    if (--beeTimer==0)
    {
        getWorld().addObject(new Bee(), 9, 1+Greenfoot.getRandomNumber(5));
        beeTimer = 80+Greenfoot.getRandomNumber(321);
    }
}
Phoenix36 Phoenix36

2018/11/23

#
Like I said still trying to configure it and thought I could use another counter or something. The while was going to activate once it reached 401 which would go into its loop then I was thinking of having the if statement in place after. The way you have it set up, looks like it the beetimer becomes the random turn number then follows with the if in decreasing to then add a bee after that certain number of turns goes by correct? Also thanks for your help on this.
Super_Hippo Super_Hippo

2018/11/23

#
Yes, the beeTimer can be read as "how many turns before the next bee should be placed into the world". At the beginning, it is set to 400 for the "do nothing" part and then it is used as the random time between.
You need to login to post a reply.