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

2013/9/24

Life Scenario Help

1
2
3
JasonZhu JasonZhu

2013/9/24

#
I see what you mean by the getRandomNumber chance being wrong. I fixed that. I already have the isPregnant Boolean, but the purpose I wanted for that method was to ensure that one ladybug cannot be pregnant multiple times. What is wrong with my gestationPeriod method? I wanted the separate method gestationPeriod so I could simplify the code in my act method.
danpost danpost

2013/9/24

#
To avoid the same bug getting pregnant more than once, just add '!isPregnant' to the list of conditions.
Zamoht Zamoht

2013/9/24

#
Forgot that thanks danpost. And the problem with your gestationPeriod() is that it only gets called from becomingPregnant(). I think you should somehow add the gestationPeriod() to the act method so it doesn't only update when the ladybug gets pregnant. As your code looks right now the ladybug has to become pregnant a lot of times to actually get the baby bug.
Zamoht Zamoht

2013/9/24

#
Okay try these changes:
private void becomingPregnant()    
{    
    Ladybug bug = (Ladybug)getOneIntersectingObject(Ladybug.class);    
    if(bug != null && !bug.isOld() && bug.isAdult() && !isOld() && isAdult() && isFemale() && bug.isMale() && !gestated && Greenfoot.getRandomNumber(100)==30){    
        gestated = true;  
    }    
} 
    public void act()   
    {  
        moveAround();  
        becomingPregnant();  
        gestationPeriod();
        growOlder();  
    } 
    private void gestationPeriod()  
    {  
        if (isPregnant())
        {
            gestationcycles++;  
            if( gestationcycles == MONTH ) {  
                gestationcycles = 0;  
                gestation++;  
                if( gestation == BIRTH_TIME ) {  
                    getWorld().addObject(new LadyBug(0),getX(),getY());
                    gestation = 0;
                    gestated = false;
                }              
            }   
        }
    } 
These minor changes should make it work. Please tell me if they don't or if you have any questions.
JasonZhu JasonZhu

2013/9/24

#
Let me Try.
JasonZhu JasonZhu

2013/9/24

#
Still doesn't work... Shouldnt becomingPregnant call gestationPeriod?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Ladybug extends Actor
{
    private static final int LIFE_SPAN = 12; // in months
    private static final int MONTH = 100;
    private static final int BIRTH_TIME = 2; // in months
    private int age; // in months
    private int gestation; // in months
    private boolean female;
    private boolean male;
    private boolean gestated;
    private int cycles = 0;
    private int gestationcycles = 0;

    // Overloading constructors
    public Ladybug()
    {
        this(0);
    }
    
    public Ladybug(int age)
    {
        setRotation( Greenfoot.getRandomNumber(360) );
        this.age = age;
        if( Greenfoot.getRandomNumber(2)==0 ) {
            female = true;
        } else {
            male = true;
        }
    }
    
    public void act() 
    {
        moveAround();
        becomingPregnant();
        gestationPeriod();  
        growOlder();
    }  
    
    public boolean isFemale()
    {
        return female;
    }
    
    public boolean isMale()
    {
        return male;
    }
    
    public boolean isOld()
    {
        return age>10;
    }
    
    public boolean isAdult()
    {
        return age>2;
    }
    
    public boolean isPregnant()
    {
        return gestated;
    }
    
    private void growOlder()
    {
        cycles++;
        if( cycles == MONTH ) {
            cycles = 0;
            age++;
            if( age == LIFE_SPAN ) {
                getWorld().removeObject(this);
            }            
        }
    }
    
    private void gestationPeriod()
    {
        gestationcycles++;
        if( gestationcycles == MONTH ) {
            gestationcycles = 0;
            gestation++;
            if( gestation == BIRTH_TIME ) {
                getWorld().addObject(new Ladybug(0),getX(),getY());
            }
            gestation=0;
            gestated=false;
        }        
    }
    
    private void becomingPregnant()      
    {      
        Ladybug bug = (Ladybug)getOneIntersectingObject(Ladybug.class);      
        if(bug != null && !bug.isOld() && bug.isAdult() && !isOld() && isAdult() && isFemale() && bug.isMale() && !gestated && Greenfoot.getRandomNumber(100)<30){      
            gestated = true;    
        }      
    }   

    
    private void moveAround()
    {
        move(5);
        if( Greenfoot.getRandomNumber(100)<60 ) {
            if(Greenfoot.getRandomNumber(2)==0) {
                turn(10);
            } else {
                turn(-10);
            }
        }      
        if( nearEdge() ) {
            turn(20);
        }
    }
    
    private boolean nearEdge()
    {
        return getX()<5 || getY()<5 ||
            getX()>getWorld().getWidth()-5 ||
            getY()>getWorld().getHeight()-5;
    }
}
Heres my updated code
davmac davmac

2013/9/24

#
I don't see why you'd need to call gestationPeriod from becomingPregnant, since it is called anyway every act cycle (line 37 of your code above). One thing that does seem wrong is that it is called unconditionally - I would have thought you'd only want to call it if the Ladybug is in fact pregnant, i.e:
if (isPregnant()) {
    gestationPeriod();
}
JasonZhu JasonZhu

2013/9/24

#
Yes exactly...
davmac davmac

2013/9/24

#
Also, on line 87, you have: gestation=0; gestated=false; This means the Ladybug becomes no longer pregnant. This happens whenever gestationcycles == MONTH, before gestation == BIRTH_TIME. So the Ladybug never gives a chance to give birth. You need to move these statements inside the 'if' statement above them.
davmac davmac

2013/9/24

#
Yes exactly...
I don't understand what you mean by this. You asked:
Shouldnt becomingPregnant call gestationPeriod?
The answer to that is no. You should call it from act, as you do in the code above, but you should only call it if the Ladybug is pregnant. I'm not suggesting that becomingPregnant should call gestationPeriod at all.
JasonZhu JasonZhu

2013/9/24

#
Thanks for all your help! It works fine now! It was just misplacement of code and small errors that made it not work. Nevermind about what I said earlier also.
JasonZhu JasonZhu

2013/9/24

#
I said "Yes exactly" to "I would have thought you'd only want to call it if the Ladybug is in fact pregnant" and also, could someone explain to me why: there are bug.isOld and isOld respectively in the conditions simultaneously?
    private void becomingPregnant()      
    {      
        Ladybug bug = (Ladybug)getOneIntersectingObject(Ladybug.class);      
        if(bug != null && !bug.isOld() && bug.isAdult() && !isOld() && isAdult() && isFemale() && bug.isMale() && !gestated && Greenfoot.getRandomNumber(100)<30){      
            gestated = true;    
        }      
    }   
danpost danpost

2013/9/24

#
You could avoid using so many fields for the same thing by using just the 'gestationcycles' field, Just call the following method from the act method (instead of calling two methods):
private void checkPregnancy()
{
    if (gestationcycles > 0)
    {
        gestationcycles--;
        if (gestationcycles == 0) getWorld().addObject(new LadyBug(0), getX(), getY());
    }
    else
    {
        Ladybug bug = (Ladybug)getOneIntersectingObject(Ladybug.class);    
        if(bug != null && !bug.isOld() && bug.isAdult() && bug.isMale() && Greenfoot.getRandomNumber(100)==30)
        gestationPeriod =  500; // or  however many cycles before giving birth
   }
}
You can use the following in the act method to call this method:
if (isFemale() && !isOld() && isAdult()) checkPregnancy();
danpost danpost

2013/9/24

#
JasonZhu wrote...
could someone explain to me why: there are bug.isOld and isOld respectively in the conditions simultaneously?
You need to check not only the female being of good age, but also the male. Hence, !isOld() and !bug.isOld().
JasonZhu JasonZhu

2013/9/24

#
I now have a problem that the male is giving birth and that is just wrong haha. And why does it need to use "bug".isOld? It's so confusing. Is there a different way to differentiate?
There are more replies on the next page.
1
2
3