Advertisement
Guest User

Clorus.java

a guest
Feb 22nd, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. package creatures;
  2.  
  3.  
  4. import huglife.Creature;
  5. import huglife.Direction;
  6. import huglife.Action;
  7. import huglife.Occupant;
  8. import huglife.HugLifeUtils;
  9.  
  10. import java.awt.Color;
  11. import java.util.ArrayDeque;
  12. import java.util.Deque;
  13. import java.util.Map;
  14.  
  15. /**
  16. * An implementation of Clorus, a fierce blue-colored predator
  17. * that enjoys nothing more than snacking on hapless Plips.
  18. *
  19. * @author Dean Zhang
  20. */
  21.  
  22. public class Clorus extends Creature {
  23.  
  24. /**
  25. * red color.
  26. */
  27. private int r;
  28. /**
  29. * green color.
  30. */
  31. private int g;
  32. /**
  33. * blue color.
  34. */
  35. private int b;
  36.  
  37. /**
  38. * creates clorus with energy equal to E.
  39. */
  40. public Clorus(double e) {
  41. super("clorus");
  42. r = 0;
  43. g = 0;
  44. b = 0;
  45. energy = e;
  46. }
  47.  
  48. /**
  49. * Cloruses should lose 0.03 units of energy when moving.
  50. */
  51. public void move() {
  52. // TODO
  53. energy = energy - 0.03;
  54. if (this.energy < 0) {
  55. energy = 0;
  56. }
  57. }
  58.  
  59. /**
  60. * Cloruses lose 0.01 energy when staying.
  61. */
  62. public void stay() {
  63. // TODO
  64. energy = energy - 0.01;
  65. }
  66.  
  67. /**
  68. * NOTE: Cloruses have no restrictions on their maximum energy.
  69. */
  70.  
  71. /**
  72. * Should return a color with red = 34, blue = 231, and green = 0.
  73. */
  74. public Color color() {
  75. r = 34;
  76. b = 231;
  77. g = 0;
  78. return color(r, g, b);
  79. }
  80.  
  81. /**
  82. * If a Clorus attacks another creature, it should gain that creature's
  83. * energy. You do not need to worry about making sure the attacked
  84. * creature dies -- the simulator does that for you.
  85. */
  86. public void attack(Creature c) {
  87. energy = this.energy + c.energy();
  88. }
  89.  
  90. /**
  91. * Cloruses and their offspring each get 50% of the energy, with none
  92. * lost to the process. Now that's efficiency! Returns a baby
  93. * Clorus.
  94. */
  95. public Clorus replicate() {
  96. energy = energy / 2;
  97. Clorus cloneC = new Clorus(energy);
  98. return cloneC;
  99. }
  100.  
  101. /**
  102. * Cloruses take exactly the following actions based on NEIGHBORS:
  103. * 1. If no empty adjacent spaces, STAY (even if there are Plips
  104. * nearby they could attack since plip squares do not count as
  105. * empty squares.
  106. * 2. Otherwise, if any Plips are seen, the Clorus will ATTACK one
  107. * of them randomly.
  108. * 3. Otherwise, if the Clorus has energy greater than or equal to
  109. * one, it will REPLICATE to a random empty square.
  110. * 4. Otherwise, the Clorus will MOVE to a random empty square.
  111. * <p>
  112. * Returns an object of type Action. See Action.java for the
  113. * scoop on how Actions work. See SampleCreature.chooseAction()
  114. * for an example to follow.
  115. */
  116. public Action chooseAction(Map<Direction, Occupant> neighbors) {
  117. // Rule 1
  118. Deque<Direction> emptyNeighbors = new ArrayDeque<>();
  119. boolean anyPlip = false;
  120. String neighbor;
  121. // TODO
  122. // (Google: Enhanced for-loop over keys of NEIGHBORS?)
  123. // for () {...}
  124. for (Direction d : neighbors.keySet()) {
  125. neighbor = neighbors.get(d).name();
  126. if (neighbor.equals("empty")) {
  127. emptyNeighbors.addLast(d);
  128. }
  129. }
  130.  
  131. if (emptyNeighbors.isEmpty()) { // FIXME
  132. // TODO
  133. return new Action(Action.ActionType.STAY);
  134. }
  135.  
  136. // Rule 2
  137. if (anyPlip) {
  138. Direction randomDir = HugLifeUtils.randomEntry(emptyNeighbors);
  139. return new Action(Action.ActionType.ATTACK, randomDir);
  140. }
  141.  
  142. // Rule 3
  143. if (energy >= 1) {
  144. Direction randomDir = HugLifeUtils.randomEntry(emptyNeighbors);
  145. return new Action(Action.ActionType.REPLICATE, randomDir);
  146. }
  147. // Rule 4
  148. Direction randomDir = HugLifeUtils.randomEntry(emptyNeighbors);
  149. return new Action(Action.ActionType.MOVE, randomDir);
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement