My assignment is to find a way to alter the program so that when the world is populated the leaves and wombats won't spawn on top of each other. I am supposed to create two methods: findEmptyCell() and isOccupied(), the second of which returns a boolean value. Been working on it for a while now and I can't seem to get it to work right. Can anyone point me in the right direction? Thank you!
Here is my code:
import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage import java.util.Random; import java.util.List; /** * A world where wombats live. * * @author * @version */ 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(), newX(), newY()); 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++) { addObject(new Leaf(), newX(), newY()); if (isOccupied(x,y)) { findEmptyCell(); } } } /** * Get a random coordinate value for x * @return a random integer value to be used as a new x-coordinate */ public int newX() { return Greenfoot.getRandomNumber(getWidth()); // getWidth() is a method defined in the World class. // Since WombatWorld "is a" World it has this method as well. } /** * Get a random coordinate value for x * @return a random integer value to be used as a new y-coordinate */ public int newY() { return Greenfoot.getRandomNumber(getHeight()); // getHeight() is a method defined in the World class. // Since WombatWorld "is a" World it has this method as well. } /** * 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(getHeight() * getWidth()); } // -------- 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() { x = newX(); y = newY(); if (isOccupied(x, y)==true) { x = newX(); y = newY(); } } /** * 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) != null) { return false; } else { return true; } } }