Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package creatures;
- import huglife.Creature;
- import huglife.Direction;
- import huglife.Action;
- import huglife.Occupant;
- import huglife.HugLifeUtils;
- import java.awt.Color;
- import java.util.ArrayDeque;
- import java.util.Deque;
- import java.util.Map;
- /**
- * An implementation of Clorus, a fierce blue-colored predator
- * that enjoys nothing more than snacking on hapless Plips.
- *
- * @author Dean Zhang
- */
- public class Clorus extends Creature {
- /**
- * red color.
- */
- private int r;
- /**
- * green color.
- */
- private int g;
- /**
- * blue color.
- */
- private int b;
- /**
- * creates clorus with energy equal to E.
- */
- public Clorus(double e) {
- super("clorus");
- r = 0;
- g = 0;
- b = 0;
- energy = e;
- }
- /**
- * Cloruses should lose 0.03 units of energy when moving.
- */
- public void move() {
- // TODO
- energy = energy - 0.03;
- if (this.energy < 0) {
- energy = 0;
- }
- }
- /**
- * Cloruses lose 0.01 energy when staying.
- */
- public void stay() {
- // TODO
- energy = energy - 0.01;
- }
- /**
- * NOTE: Cloruses have no restrictions on their maximum energy.
- */
- /**
- * Should return a color with red = 34, blue = 231, and green = 0.
- */
- public Color color() {
- r = 34;
- b = 231;
- g = 0;
- return color(r, g, b);
- }
- /**
- * If a Clorus attacks another creature, it should gain that creature's
- * energy. You do not need to worry about making sure the attacked
- * creature dies -- the simulator does that for you.
- */
- public void attack(Creature c) {
- energy = this.energy + c.energy();
- }
- /**
- * Cloruses and their offspring each get 50% of the energy, with none
- * lost to the process. Now that's efficiency! Returns a baby
- * Clorus.
- */
- public Clorus replicate() {
- energy = energy / 2;
- Clorus cloneC = new Clorus(energy);
- return cloneC;
- }
- /**
- * Cloruses take exactly the following actions based on NEIGHBORS:
- * 1. If no empty adjacent spaces, STAY (even if there are Plips
- * nearby they could attack since plip squares do not count as
- * empty squares.
- * 2. Otherwise, if any Plips are seen, the Clorus will ATTACK one
- * of them randomly.
- * 3. Otherwise, if the Clorus has energy greater than or equal to
- * one, it will REPLICATE to a random empty square.
- * 4. Otherwise, the Clorus will MOVE to a random empty square.
- * <p>
- * Returns an object of type Action. See Action.java for the
- * scoop on how Actions work. See SampleCreature.chooseAction()
- * for an example to follow.
- */
- public Action chooseAction(Map<Direction, Occupant> neighbors) {
- // Rule 1
- Deque<Direction> emptyNeighbors = new ArrayDeque<>();
- boolean anyPlip = false;
- String neighbor;
- // TODO
- // (Google: Enhanced for-loop over keys of NEIGHBORS?)
- // for () {...}
- for (Direction d : neighbors.keySet()) {
- neighbor = neighbors.get(d).name();
- if (neighbor.equals("empty")) {
- emptyNeighbors.addLast(d);
- }
- }
- if (emptyNeighbors.isEmpty()) { // FIXME
- // TODO
- return new Action(Action.ActionType.STAY);
- }
- // Rule 2
- if (anyPlip) {
- Direction randomDir = HugLifeUtils.randomEntry(emptyNeighbors);
- return new Action(Action.ActionType.ATTACK, randomDir);
- }
- // Rule 3
- if (energy >= 1) {
- Direction randomDir = HugLifeUtils.randomEntry(emptyNeighbors);
- return new Action(Action.ActionType.REPLICATE, randomDir);
- }
- // Rule 4
- Direction randomDir = HugLifeUtils.randomEntry(emptyNeighbors);
- return new Action(Action.ActionType.MOVE, randomDir);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement