// Miya Natsuhara // 08-17-2019 // CSE142 // TA: Grace Hopper // Chameleon behavior: // constructor: Chameleon() // getColor: alternate between Color.GREEN and Color.MAGENTA depending on move // (starting with Color.GREEN) // getMove: if getFront is same as me, turn left, // otherwise if getFront is different from me, infect // otherwise hop // toString: @ import java.awt.*; public class Chameleon extends Critter { // tracking whether the Chameleon should appear green or not when asked next private boolean shouldBeGreen; // Constructs a new chameleon, set up to be green initially public Chameleon() { shouldBeGreen = true; } // Returns the Chameleon's display, always a @ character. public String toString() { return "@"; } // Returns the Chameleon's next move, given information in the provided CritterInfo parameter. // The Chameleon turns left if there is another Chameleon in front of it, infects if there is // a Critter of another kind in front of it, and otherwise hops forward. // CritterInfo info: A CritterInfo object containing information about the surroundings of // this Chameleon. public Action getMove(CritterInfo info) { shouldBeGreen = !shouldBeGreen; Neighbor frontNeighbro = info.getFront(); if (frontNeighbro == Neighbor.SAME) { return Action.LEFT; } else if (frontNeighbro == Neighbor.OTHER) { return Action.INFECT; } else { return Action.HOP; } } // Returns the color of the Chameleon, alternating between green and magenta on each move. public Color getColor() { if (shouldBeGreen) { return Color.GREEN; } else { return Color.MAGENTA; } } }