I'm coding a fun assignment for a class I'm taking and its all about polymorphism and coding objects, etc... Basically, we are asked to code a "critter" object that fights other students critters.
How can I make it that so when a certain method is called on my critter object, (lose(), when my critter loses the fight) that it then calls its own constructor again so it is "reborn"?
Here's my code.
import java.awt.*;
import java.util.*;
public class Husky extends Critter {
//fields
private int x = getWidth();
private int y = getHeight();
private Random r = new Random();
private Direction[] directions = {Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST};
public Husky() {}
public boolean eat() {
return true;
}
public Attack fight(String opponent) {
if (opponent.equals("%")) {
return Attack.ROAR;
} else if (opponent.equals("^") || opponent.equals(">") || opponent.equals("V")
|| opponent.equals("<") || opponent.equals("0")) {
return Attack.SCRATCH;
} else {
return Attack.ROAR;
}
}
public Direction getMove() {
int z = r.nextInt(4);
return directions[z];
}
public Color getColor() {
return Color.CYAN;
}
public String toString() {
return "∏";
}
public void lose() {
this();
}
}
I want my critter to be undying, basically. Is it possible?