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

2014/10/25

Prevent Objects From Spawning in the Same Cell

blairtch blairtch

2014/10/25

#
In my program it is possible (although rare) for the two wombats to occupy the same cell. How can I use the findEmptyCell() method to place the second wombat? Here is my code:
import greenfoot.*;  // imports Actor, World, 
public class WombatWorld extends World
{
    //Private class member variables can be declared here
    int x;
    int y;
    
    
    /**
     * Create a new world with 8x8 cells & with a cell size of 60x60 px
     * Set the background of the world to "cell.jpg" which is located in
     * the scenario's image folder. Finally, populate the world.
     */
    public WombatWorld() 
    {
        super(8, 8, 60);        
        setBackground("cell.jpg");
        populate();
    }

    /**
     * Populates world with a random arrangement of wombats and leaves
     */    
    public void populate()
    {
        addObject(new Wombat(), 1, 7);
        addObject(new Wombat(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()) );
        
        randomLeaves(getNumLeaves());

    }

    /**
     * Place a pre-determined number of leaves into the world in random
     * cells. Each leaf can only be placed in an unoccupied cell.
     * @param howMany Number of leaves to be placed randomly in cells.
     */
    public void randomLeaves(int howMany)
    {
        
        for(int i=0; i<howMany; i++)
        {
            
            findEmptyCell();
            addObject(new Leaf(), x, y); 

        }
    }


    /**
     * Get a random number of leaves that will be placed in the world
     * @return a random int; represents number of leaves to be placed
     */
    public int getNumLeaves()
    {
        return Greenfoot.getRandomNumber(getWidth() * getHeight() - 11) + 10;
    }

    // -------- NEW METHODS TO BE CREATED ---------- //
    
    /**
     * findEmptyCell() - Randomly locate a currently unoccupied cell
     * in the world. Use the class member variables to hold the x and y
     * coordinates once an empty cell has been found. No parameters
     * are needed, and the return type is void.
     */
    public void findEmptyCell()
    {

        while (isOccupied(x, y)== true)
        {
            x = Greenfoot.getRandomNumber(getWidth());
            y = Greenfoot.getRandomNumber(getHeight());
        }
        
    }


    /**
     * isOccupied - Used to determine if a space is occupied by another
     * Actor object.
     * @param x int for the x-coordinate of the target cell to inspect
     * @param y int for the y-coordinate of the target cell to inspect
     * @return boolean true if target cell has one or more Actor objects
     */
    public boolean isOccupied(int x, int y)
    {
        if (getObjectsAt(x, y, null).isEmpty()) 
        {
            return false;
        } else {
            return true;
        }
    }
    
}
blairtch blairtch

2014/10/25

#
Would placing a call to the method above where I'm adding the second wombat in the populate() method fix this issue for sure?
 public void populate()
    {
        addObject(new Wombat(), 1, 7);
        findEmptyCell();
        addObject(new Wombat(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()) );
        
        randomLeaves(getNumLeaves());

    }
blairtch blairtch

2014/10/25

#
I created a new method addWombat()
 public void addWombat()
    {
        
            findEmptyCell();
            addObject(new Wombat(), x, y); 

    }
And called this method in the populate() method and it seems to have fixed the issue!
You need to login to post a reply.