Hmm, the world still won't build, I think I still might have an infinite loop?
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;
}
}
}