/** * Randomly selects a neighbor and changes this critter's color to be the same as that neighbor's. * If there are no neighbors, no action is taken. */ public void processActors(ArrayList<Actor> actors) { int n = actors.size(); if (n == 0) return; int r = (int) (Math.random() * n); Actor other = actors.get(r); setColor(other.getColor()); }
/** * Randomly selects a neighbor and changes this critter's color to be the same as that neighbor's. * If there are no neighbors, it will darken. */ @Override public void processActors(ArrayList<Actor> actors) { int actorsSize = actors.size(); // if there are no neighbors, the critter darkens. if (actorsSize == 0) { Color color = getColor(); int red = (int) (color.getRed() * (1 - DARKENING_FACTOR)); int green = (int) (color.getGreen() * (1 - DARKENING_FACTOR)); int blue = (int) (color.getBlue() * (1 - DARKENING_FACTOR)); setColor(new Color(red, green, blue)); return; } // randomly select a neighbor and put on its color int rand = (int) (Math.random() * actorsSize); Actor neighbor = actors.get(rand); setColor(neighbor.getColor()); }