Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Feline extends Critter implements Movable Instance variables: Each Feline will keep track of the number of times it has moved, the amount of times it
Feline extends Critter implements Movable
- Instance variables: Each Feline will keep track of the number of times it has moved, the amount of times it has not eaten, and the current direction it is going in. You can make these variables private.
- No-arg constructor: A default no-arg constructor. The Critter should be full. And moveCount should be set such that it will change to a new direction at the next (first) call to getMove(). This means that Feline should not start eating once it is created (look at eat()). The string representation of Feline is Fe. (Hint: think about which inherited instance variable you need to set.)
- Override getMove(): Felines are jumpy and tend to go in random directions, so it will go in a new random direction (excluding CENTER) every 5th move (including the first move) that it makes. NOTE: "new random direction" here means a newly chosen direction, not necessarily a unique/different direction. If Feline went NORTH for 5 moves, it should still be able to choose NORTH as its next 5 moves.
- Override eat(): Felines dont need to eat that much, so every 5th time it encounters food, it will eat.
- Override getAttack(): Felines should always POUNCE.
Lion extends Feline
- Instance variables: Each Lion will keep track of the number of fights it wins until it goes to sleep, which will determine its eating behavior. Losing a fight does not decrease the instance variable. You may need a few private variables to keep track of the Lions movement.
- No-arg constructor: A default no-arg constructor. The string representation of Lion is Lion.
- Override getColor(): a Lion is YELLOW. (Hint: what method gives you the color of a Critter?)
- Override getMove(): The king of beasts does not fear other critters, and they wait for their chance to engage. A Lion will first go SOUTH 5 times, then WEST 5 times, then NORTH 5 times, then EAST 5 times (i.e. a clockwise square pattern). As mentioned, think about what instance variable to initialize to keep track of its movement.
- Override eat(): Return true if the Lion has won at least one fight since it last ate or slept. Think of the Lion as having a hunger that is triggered by fighting. Initially the Lion is not hungry, but winning a fight makes the Lion hungry. When a Lion is hungry, the next call to the eating method should return true. However, once the Lion has eaten OR slept (when sleep() is called), the future calls of eat() should return false until the next win. (Hint: Think about why sleep() would make a lion full again.)
- Override sleep() and wakeup(): When a Lion goes to sleep, it will reset the number of fights it won to zero, and reverse its display name to noiL. When it wakes up, it reverts back to Lion.
- Note: Handle the behavior of sleep() and wakeup() separately in their own respective methods.
- Override win(): When a Lion wins a fight, it becomes hungry. Make sure to keep track of the number of fights it won as it affects its eating behavior.
- Do not override getAttack(). It should have the same behavior as Feline.
Leopard extends Feline
- Instance variables: Each Leopard, in addition to the instance variables inherited from its superclasses, will all telepathically keep track of their confidence together. The confidence starts at 0 when the simulation starts. When the confidence of one Leopard is affected, ALL Leopards' confidence will be affected in the exact same way.
- Hint: What type of modifier can you apply to a variable to make that variable shared across all instances?
- No-arg constructor: The string representation of Leopard is "Lpd".
- Override getColor(): Leopards are RED (for camouflage, of course).
- Override getMove(): The Leopard always checks its neighbors before moving. If one of the four neighborsNORTH, SOUTH, EAST, WEST contains either food or Starfish, then the Leopard will move towards that direction. If more than one direction has Starfish or food, then the Leopard will move towards the first found direction. If none of the directions contain Starfish or food, then the Leopard will randomly choose a direction to move (excluding CENTER). (When checking the neighbors, please follow the N S E W order).
- Override eat(): The Leopards will always have (confidence * 10)% chance of eating. For example, if confidence is at 2, then there is a 20% chance of eating.
- Override win() and lose(): If a Leopard wins a fight, all Leopards confidence will increment by one if their confidence is less than 10. If a Leopard loses, all Leopards will reduce their confidence by 1 if their confidence is greater than zero. The minimum confidence they can have is 0, and the maximum is 10.
- Override getAttack(): The Leopard will ROAR if the opponent is Turtle or if all Leopards' confidence is greater than 5. Otherwise, the Leopard will randomly choose an attack method. A Leopard should also never FORFEIT.
- You should create a separate helper method generateAttack() to randomly choose an attack method.
Ocelot extends Leopard
- No-arg constructor: The string representation of Ocelot is "Oce".
- Override getColor(): Ocelots are LIGHT GRAY.
- Override getAttack(): The Ocelot will attack in the following ways:
- Otherwise, randomly choose an attack method. (Hint: Do not re-write code you have already inherited from Leopard! Which helper method you defined inside Leopard.java can help you accomplish the same result?)
- If confidence is greater than 5, SCRATCH if the opponent is a Lion (both awake or asleep), a Feline, or a Leopard. POUNCE otherwise.
Elephant extends Critter implements Movable
- Instance variables: Elephants share an int goalX and int goalY coordinate (do not make them private). An Elephant also has a Random object used to generating random ints when picking new goal coordinates.
- Hint: Look at the modifier for goalX and goalY. What does that mean about all Elephant objects?
- No-arg constructor: The string representation of Elephant is El. The random object must be initialized. The very first goalX and goalY should be (0,0).
- Override getColor(): Elephants are GRAY.
- Override getMove(): Elephants are sensible creatures and know that they are safer if they move in herds. Because of this, Elephants have a precise movement pattern.
- All elephants move towards the shared goalX and goalY coordinate in the simulation. Each Elephant moves towards their goal in the axis in which they are further from their goal. So if an Elephant is further from its goal in the x-axis, it would move EAST or WEST depending on the location of their goal and their current location. When an Elephant is further from its goal in the y-axis, an Elephant would move NORTH or SOUTH. If the distances are equal, choose to move on either the x- or y-axis (but be consistent with your choice - always choose x or always choose y).
- If an Elephant reaches the goalX and goalY, it must change the goalX and goalY variables to a new random location on the board.
- You do not consider the "wrap-around" case for either axis. For example, consider the case where the world is 60x50, ElephantAlpha is at (0, 0), and the goal is at (59, 5). Then ElephantAlpha will move EAST because goalX is further than goalY, even though it can technically move WEST to get to the goalX-coordinate faster on the x-axis. The same holds true for the y-axis.
- Override eat(): Out in the wild, Elephants need a lot of food, so they will always eat.
- Override mate(): When an Elephant mates, increment its level by 2.
- Do not override getAttack() from the Critter class.
Turtle extends Critter implements Movable
- No-arg constructor: The string representation of Turtle is Tu.
- Override getColor(): Turtles are GREEN.
- Override getMove(): Turtles always move WEST.
- Override eat(): Turtles like to play it safe when eating. They only eat when there are no hostile animals next to the Turtle (hostile animals are anything that is not an empty space, food, or Turtle).
- Override getAttack(): Turtles dont always fight, but sometimes they do. Turtles attack with ROAR 50% of the time and try to FORFEIT the other 50%. Slow and steady wins the race, after all.
In Movable class:
In Critter class:
import java.util.Random; k* interface Movable Critter can optionally implement t and eat public interface Movable ( Random random new Random(); t * returns the move direction by t. @return a direction selected Direction getMove(); ks returns whether the Movable Cr @return true if the critter de a: boolean eat (); mport java.awt.* enum Direction NORTH, SOUTH EAST, WEST, CENTER 3: enum Attack POUNCE, ROAR, SCRATCH, FORFEIT 3: public abstract class Critter ( protected int level 0 String displayName null; public Critter(String name) this.displayName name; public int getLevel) return this.level; // This function updates the level of the object protected void incrementLevel (int num) [ level level num @Override public final string tostringo return this.displayName; // These methods are provided to inform you about the result of fig //sleeping, etc. /You can override these methods in your Critter to be informed of /called when you win a fight against another animal public void win) ( / called when you Lose a fight against another animal, and die public void lose) / called when your animal is put to sleep for eating too much food public void sleep()f / calLed when your animal wakes up from sleeping public void wakeup () // called when the game world is reset public void reset) / called when your critter mates with another critter public void mate) /called when your critter is done mating with another critter // called when your critter is done mating with another critter public void mateEnd() public Color getcolor) ( return Color.BLACK; public void buffBehavior(Critterstate s) return; public void debuff (Critterstate s)( return; public Attack getAttack (String opponent) return Attack. FORFEIT; // I use these fields to implement the methods below such as getx and / getNeighbor. private int x; private int y; private int width; private int height; private boolean alive true; private boolean awake true; private final string[] neighbors , " ", " ","", ""J; private final stringe neighbors . {--, --, --,--,--); / The following methods are provided to get information about the critter / Technically the critter could call setxxxx) on itself // but the game model ignores this anyway, so it's useless to do so. / These methods are declared final so you can't override them /Returns the height of the game simulation world. protected final int getHeight () return height; // Returns the animal that is 1 square in the given direction away /from this animal. A blank space, ",signifies an empty square. /A dot "." signifies food protected final string getNeighbor(Direction direction) ( return neighbors [direction.ordinal()]; // Returns the width of the game simulation world. protected final int getwidth() return width; //Returns this animal's current x-coordinate. protected final int getx) return x; //Returns this animal 's current y-coordinate protected final int gety) return y /Returns true if this animal is currently alive. /This wilL return false if this animal has Lost a fiaht and died // Returns true if this animal is currently alive. // This will return false if this animal has Lost a fight and died. protected final boolean isAlive() f return alive; //Returns true if this animal is currently awake. // This will temporarily return false if this animal has eaten too muc //and fallen asleep. protected final boolean isAwake) return awake; // Sets whether or not this animal is currently alive. // This method is called by the simulator and not by your animal itsel protected final void setAlive(boolean alive) f this.alive alive; / Sets whether or not this animal is currently awake / This method is called by the simulator or in the buff method. protected final void setAwake (boolean awake) ( this.awake awake; // Sets the height of the game simulation world to be the given value / so that future calls to getHeight will return this value // This method is called by the simulator and not by your animal itsel protected final void setHeight(int height) ( this.height = height; // Sets the neighbor of this animal in the given direction to be the give //value /so that future calls to getNeighbor in that direction will return this // This method is called by the simulator and not by your animal itself. protected final void setNeighbor(Direction direction, String value) neighbors(direction.ordinal()] = value; // Sets the width of the game simulation world to be the given value. so that future calls to getwidth will return this value. / This method is called by the simulator and not by your animal itself. protected final void setwidth(int width) f this.width -width; // Sets this animal's memory of its x-coordinate to be the given value. /so that future calls to getx will return this value. / This method is called by the simulator and not by your animal itself. protected final void setx(int x) f this.x - x // Sets this animal's memory of its y-coordinate to be the given value / so that future calls to gety wilL return this value. / This method is called by the simulator and not by your animal itself. protected final void setY(int y) f this.y -y import java.util.Random; k* interface Movable Critter can optionally implement t and eat public interface Movable ( Random random new Random(); t * returns the move direction by t. @return a direction selected Direction getMove(); ks returns whether the Movable Cr @return true if the critter de a: boolean eat (); mport java.awt.* enum Direction NORTH, SOUTH EAST, WEST, CENTER 3: enum Attack POUNCE, ROAR, SCRATCH, FORFEIT 3: public abstract class Critter ( protected int level 0 String displayName null; public Critter(String name) this.displayName name; public int getLevel) return this.level; // This function updates the level of the object protected void incrementLevel (int num) [ level level num @Override public final string tostringo return this.displayName; // These methods are provided to inform you about the result of fig //sleeping, etc. /You can override these methods in your Critter to be informed of /called when you win a fight against another animal public void win) ( / called when you Lose a fight against another animal, and die public void lose) / called when your animal is put to sleep for eating too much food public void sleep()f / calLed when your animal wakes up from sleeping public void wakeup () // called when the game world is reset public void reset) / called when your critter mates with another critter public void mate) /called when your critter is done mating with another critter // called when your critter is done mating with another critter public void mateEnd() public Color getcolor) ( return Color.BLACK; public void buffBehavior(Critterstate s) return; public void debuff (Critterstate s)( return; public Attack getAttack (String opponent) return Attack. FORFEIT; // I use these fields to implement the methods below such as getx and / getNeighbor. private int x; private int y; private int width; private int height; private boolean alive true; private boolean awake true; private final string[] neighbors , " ", " ","", ""J; private final stringe neighbors . {--, --, --,--,--); / The following methods are provided to get information about the critter / Technically the critter could call setxxxx) on itself // but the game model ignores this anyway, so it's useless to do so. / These methods are declared final so you can't override them /Returns the height of the game simulation world. protected final int getHeight () return height; // Returns the animal that is 1 square in the given direction away /from this animal. A blank space, ",signifies an empty square. /A dot "." signifies food protected final string getNeighbor(Direction direction) ( return neighbors [direction.ordinal()]; // Returns the width of the game simulation world. protected final int getwidth() return width; //Returns this animal's current x-coordinate. protected final int getx) return x; //Returns this animal 's current y-coordinate protected final int gety) return y /Returns true if this animal is currently alive. /This wilL return false if this animal has Lost a fiaht and died // Returns true if this animal is currently alive. // This will return false if this animal has Lost a fight and died. protected final boolean isAlive() f return alive; //Returns true if this animal is currently awake. // This will temporarily return false if this animal has eaten too muc //and fallen asleep. protected final boolean isAwake) return awake; // Sets whether or not this animal is currently alive. // This method is called by the simulator and not by your animal itsel protected final void setAlive(boolean alive) f this.alive alive; / Sets whether or not this animal is currently awake / This method is called by the simulator or in the buff method. protected final void setAwake (boolean awake) ( this.awake awake; // Sets the height of the game simulation world to be the given value / so that future calls to getHeight will return this value // This method is called by the simulator and not by your animal itsel protected final void setHeight(int height) ( this.height = height; // Sets the neighbor of this animal in the given direction to be the give //value /so that future calls to getNeighbor in that direction will return this // This method is called by the simulator and not by your animal itself. protected final void setNeighbor(Direction direction, String value) neighbors(direction.ordinal()] = value; // Sets the width of the game simulation world to be the given value. so that future calls to getwidth will return this value. / This method is called by the simulator and not by your animal itself. protected final void setwidth(int width) f this.width -width; // Sets this animal's memory of its x-coordinate to be the given value. /so that future calls to getx will return this value. / This method is called by the simulator and not by your animal itself. protected final void setx(int x) f this.x - x // Sets this animal's memory of its y-coordinate to be the given value / so that future calls to gety wilL return this value. / This method is called by the simulator and not by your animal itself. protected final void setY(int y) f this.y -yStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started