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

2012/12/13

Leaves and Wombats - Random Leaves

Gruff757 Gruff757

2012/12/13

#
Hello, I need help with placing random Leaves and Golden Leaves in a way that there is a maximum of 40 on screen but to have a random amount of normal leaves show up with the golden leaves picking up the remainder needed to hit a total of 40. Could anyone help me out?
vonmeth vonmeth

2012/12/13

#
// get a random number between 0 and 40.
int numNormal = Greenfoot.getRandomNumber(41); 
//40 minus the random number generated above to get the number of Golden
int numGolden = 40-numNormal; 
The result will always be a total of 40 (with a chance of all Golden and all Normal). You could change the number of random (41), to a lower number, so you always at least have a chance of some Golden leaves being generated. Edit: If you want to at least have a chance of Normal, you will need to add this:
int numNormal = Greenfoot.getRandomNumber(30)+1; 
Since it could still end up being 0, and would end up with all golden.
Gruff757 Gruff757

2012/12/13

#
Ive put in that code but I am not getting the desired effect. Here is the source page. if that'll help any.
/**
 * A world where wombats live.
 * 
 * @author Michael Kolling
 * @version 1.0.1
 */
public class WombatWorld extends World
{
    /**
     * Create a new world with 8x8 cells and
     * with a cell size of 60x60 pixels
     */
    public WombatWorld() 
    {
        super(8, 8, 60);        
        setBackground("cell.jpg");
        setPaintOrder(Wombat.class, Leaf.class, GoldenLeaf.class); //HALL: Wombats walk over Leasves and Golden Leaves
        populateWombatWorld();   
    }

    /**
     * Populate the world with a fixed scenario of wombats and leaves.
     */    
    public void populateWombatWorld()
    {
        placeRandomLeaves(28); //HALL: Place 28 leaves on the world
        placeRandomWombats(5); //HALL: Place 5 wombats on the world (R1)    
        placeRandomGoldenLeaf(12); //HALL: Place 12 Golden Leaves on the World
    }

    /**
     * HALL: this act method keeps track of the number of objects in the world
     */
    public void act()
    {
        if ( countWombatsInTheWorld() <= 0 )  //HALL: if there are no wombats in the world 
        {                                     // stop the simulation (R11)
            Greenfoot.stop();           
        }
    }

    /**
     * HALL: this method counts and return the number of wombats in the world
     */ 

    public int countWombatsInTheWorld()
    {      
        return getObjects(Wombat.class).size();      
    }

    /**
     * HALL: Place a number of leaves into the world at random places.
     * HALL: The number of leaves can be specified.
     */
    public void placeRandomLeaves(int howManyLeaves)
    {
        for(int i = 0; i < howManyLeaves; i++) {                //HALL: (R3)
            Leaf randomLeaf = new Leaf();
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(randomLeaf, x, y);
        }
    }

    /**
     * HALL: Place a number of wombats into the world at random places.
     * HALL: The number of wonbats can be specified.
     */
    public void placeRandomWombats(int howManyWombats)
    {
        for(int i = 0; i < howManyWombats; i++) {
            Wombat randomWombat = new Wombat();
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(randomWombat, x, y);
        }
    }
    
    /**
     * HALL: Place a number of Golden Leaves into the world at random places.
     * HALL: The number of Golden Leaves can be specified.
     */
    public void placeRandomGoldenLeaf(int howManyGoldenLeaf)
    {
        for(int i = 0; i < howManyGoldenLeaf; i++) {
            GoldenLeaf randomGoldenLeaf = new GoldenLeaf();
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(randomGoldenLeaf, x, y);
        }
    }
}

vonmeth vonmeth

2012/12/13

#
public void populateWombatWorld()  
    {  
        int numNormal = Greenfoot.getRandomNumber(41);
        int numGolden = 40-numNormal;

        placeRandomLeaves(numNormal); //HALL: Place 28 leaves on the world  
        placeRandomWombats(5); //HALL: Place 5 wombats on the world (R1)      
        placeRandomGoldenLeaf(numGolden); //HALL: Place 12 Golden Leaves on the World  
    }  
Edit: You are not likely going to see 40 actual total leaves in the world. Since the world is 8x8, the chances of one Leaf, landing on top of another leaf is high. So visually, you are probably going to see less than 40.
Gruff757 Gruff757

2012/12/13

#
It works now. Thanks a lot vonmeth! =)
You need to login to post a reply.