/** * Helper method of moved method. * * @param c receives array of adjacent cells. * @return Returns the cell where the herbivore will be move. */ private Cell adjPlants(ArrayList<Cell> c) { Cell tempCell = null; ArrayList<Cell> emptyCells = new ArrayList<>(); for (Cell fCells : c) { if (fCells.getComponentCount() > 0) { if (fCells.getComponents()[0] instanceof Plant) { // set life back to 5 (since herbivore had eaten) temp.life = 5; tempCell = fCells; return tempCell; } } } /*get a random adjacent cell if plants are not around * two herbivores cannot be in a same spot. */ for (Cell fCells : c) { if (fCells.getComponentCount() == 0) { emptyCells.add(fCells); } } tempCell = emptyCells.get(RandomGenerator.nextNumber(emptyCells.size() - 1)); return tempCell; }
/** The class constructor. */ public Herbivore() { int x = RandomGenerator.nextNumber(10) + 1; if (x >= 6) img = IMAGE1.getImage(); else if (x <= 5) img = IMAGE2.getImage(); newIcon = new ImageIcon(img); setOpaque(false); }