public class Monster protected String clanAffiliation pro.pdf
1. public class Monster {
protected String clanAffiliation;
protected int ferocity;
protected int defense;
protected int magic;
protected int treasure;
protected int health;
public Monster(String clanAffiliation, int ferocity, int defense, int magic) {
this.clanAffiliation = clanAffiliation;
this.ferocity = ferocity;
this.defense = defense;
this.magic = magic;
this.treasure = 0;
this.health = 100;
}
public String getClanAffiliation() {
return clanAffiliation;
}
public int getFerocity() {
return ferocity;
}
public int getDefense() {
return defense;
}
public int getMagic() {
return magic;
}
public int getTreasure() {
return treasure;
}
public int getHealth() {
return health;
}
public void addTreasure(int treasure) {
if (health > 0) {
this.treasure += treasure;
}
}
public void subtractTreasure(int treasure) {
if (health > 0) {
this.treasure -= treasure;
if (this.treasure < 0) {
2. this.treasure = 0;
}
}
}
public void addHealth(int amount) {
if (health > 0) {
health += amount;
}
}
public void subtractHealth(int amount) {
if (health > 0) {
health -= amount;
if (health < 0) {
health = 0;
}
}
}
public boolean isAlive() {
return health > 0;
}
public double getBattleScore() {
if (health <= 0) {
return 0;
}
return (ferocity + defense + magic) / 3.0;
}
public abstract void attack(Monster opponent);
public abstract void defend(double opponentBattleScore);
@Override
public String toString() {
return "Monster{" +
"clanAffiliation='" + clanAffiliation + ''' +
", ferocity=" + ferocity +
", defense=" + defense +
", magic=" + magic +
", treasure=" + treasure +
", health=" + health +
'}';
}
}
public class Orc extends Monster {
private OrcCommander commander;
3. public Orc(String clan, int ferocity, int defense, int magic, int treasure, int health) {
super(clan, ferocity, defense, magic, treasure, health);
}
public void setCommander(OrcCommander commander) {
this.commander = commander;
}
public OrcCommander getCommander() {
return commander;
}
public int getLeadership() {
if (this instanceof Warlord) {
Warlord warlord = (Warlord) this;
return warlord.getLeadership();
}
return 0;
}
public int getInfantryCount() {
if (this instanceof Warlord) {
Warlord warlord = (Warlord) this;
return warlord.getInfantryCount();
}
return 0;
}
public void setInfantry(Warlord warlord) {
if (this instanceof Infantry) {
Infantry infantry = (Infantry) this;
infantry.setCommander(warlord);
}
}
@Override
public void attack(Monster opponent) {
int attackScore = calculateAttackScore();
int defenseScore = opponent.calculateDefenseScore();
int damage = Math.max(attackScore - defenseScore, 0);
opponent.takeDamage(damage);
}
}
public class Warlord extends Orc {
private int leadershipRating;
private Infantry[] infantry;
public Warlord(String name, int ferocity, int defense, int magic, int leadershipRating) {
super(name, ferocity, defense, magic);
4. this.leadershipRating = leadershipRating;
this.infantry = new Infantry[5];
}
public int getLeadershipRating() {
return leadershipRating;
}
public void setLeadershipRating(int leadershipRating) {
if (leadershipRating >= 1 && leadershipRating <= 5) {
this.leadershipRating = leadershipRating;
} else {
System.out.println("Leadership rating must be between 1 and 5");
}
}
public Infantry[] getInfantry() {
return infantry;
}
public void addInfantry(Infantry newInfantry) {
boolean added = false;
for (int i = 0; i < infantry.length; i++) {
if (infantry[i] == null) {
infantry[i] = newInfantry;
newInfantry.setWarlord(this);
added = true;
break;
}
}
if (!added) {
System.out.println("Could not add Infantry - this Warlord already has 5 Infantry in their
command.");
}
}
public void removeInfantry(Infantry infantryToRemove) {
for (int i = 0; i < infantry.length; i++) {
if (infantry[i] == infantryToRemove) {
infantry[i] = null;
infantryToRemove.setWarlord(null);
break;
}
}
}
public void soundBattleCry() {
if (getHealth() > 0) {
5. int boost = leadershipRating * 5;
System.out.println(getName() + " sounds their battle cry, boosting the health of their Infantry by " +
boost);
for (Infantry soldier : infantry) {
if (soldier != null) {
soldier.receiveHealthBoost(boost);
}
}
} else {
System.out.println(getName() + " cannot sound their battle cry - they are dead.");
}
}
}
public class Infantry extends Monster {
private Warlord commander;
public Infantry(String clan, int ferocity, int defense, int magic, int treasure, int health, Warlord
commander) {
super(clan, ferocity, defense, magic, treasure, health);
this.commander = commander;
}
public Warlord getCommander() {
return commander;
}
public void setCommander(Warlord commander) {
this.commander = commander;
}
public void attack(Monster monster) {
if (this.getHealth() <= 0 || monster.getHealth() <= 0) {
return;
}
double battleScore = (this.getFerocity() + this.getDefense() + this.getMagic()) / 3.0;
if (this.commander != null && this.commander.getLeadership() > 0) {
battleScore *= 1.5;
}
if (monster instanceof Warlord) {
battleScore *= 0.5;
}
if (battleScore > monster.getBattleScore()) {
int damage = (int) Math.round(battleScore - monster.getBattleScore());
monster.setHealth(monster.getHealth() - damage);
if (monster.getHealth() <= 0 && monster instanceof Orc) {
((Orc) monster).removeInfantry(this);
6. }
}
}
}
public class Goblin extends Monster {
private Goblin swornEnemy;
public Goblin(String clanAffiliation, int ferocity, int defense, int magic, int treasure, int health,
Goblin swornEnemy) {
super(clanAffiliation, ferocity, defense, magic, treasure, health);
this.swornEnemy = swornEnemy;
}
public Goblin getSwornEnemy() {
return swornEnemy;
}
public void setSwornEnemy(Goblin swornEnemy) {
this.swornEnemy = swornEnemy;
}
}
public class Manticore extends Monster {
private String clanAffiliation;
public Manticore(String clanAffiliation, int ferocity, int defense, int magic, int treasure, int health) {
super(clanAffiliation, ferocity, defense, magic, treasure, health);
this.clanAffiliation = clanAffiliation;
}
public String getClanAffiliation() {
return clanAffiliation;
}
public void setClanAffiliation(String clanAffiliation) {
this.clanAffiliation = clanAffiliation;
}
@Override
public double getBattleScore() {
return (getFerocity() + getDefense() + getMagic()) / 3.0 * 1.5;
}
}
public abstract void attack (Monster opponent): public abstroct vaid befend double ononpublic
class Warlord extends Orc { private int leadershipRating; private Infantry[] infantry; public
Warlord(String name, int ferocity, int defense, int magic, int leadershipRating) { super(name,
ferocity, defense, magic); this. leadershipRating = leadershipRating; this. infantry = new Infantry [5
]; 3 public int getleadershipRating() ( return leadershipRating; (3) public void
setLeadershipRating(int leadershipRating) { if (leadershipRating >=1 s& leadershipRating <=5 ) f
this, leadershipRating = leadershipRating; 3 else System.out. println("Leadership rating must be
7. between 1 and 5 ); 3 3 public Infantry[] getinfantry () { return infantry; 3 public void addInfantry
(Infantry newinfantry) & boolean added = false; for (Int 1=0;1< infantry, 1ength; 1++)< if (infantry [i]
=n= nu11) { infantry [1]= newinfantry; newinfantry, setwarlord(this); added = true; break; ) 3 if
(ladded) { System,out, println("Could not add Infantry. this Wariord already has 5 Infantry in th (
comsand. "); 3 public void removeInfantry (Infantry infantry ToRemove) if. for { int 1 * 3;1< infantry.
Length; 1+} i If (infantry[ 1 in infantryToremove) {public class Infantry extends Monster { private
Warlord commander; public Infantry(String clan, int ferocity, int defense, int magic, int treasure, int
health, Warlord commander) { super(clan, ferocity, defense, magic, treasure, health); this .
commander = commander; 3 public Warlord getCommander() { return commander; 3 public void
setCommander(Warlord commander) ( this. commander = commander; 3 public void
attack(Monster monster) ( return; 7 double battlescore - (this, getferocity ()+ this.getDefense() +
this.getMagic()) 3.0 ; if (this. commander I= nu11 && this. commander.getLeadership ( ) > ) {
battlescore =1.5 3 if (monster instanceof Warlord) & battlescore 0.5; 3 if (battlescore >
monster.getBattlescore()) { int damage = (int) Math. round (battleScore - monster.
getBattlescore()); monster.setHealth(monster, getHealth() - damage); If (monster, gethealth ( ) <=
0&& monster instanceof Orc) { ((orc) monster). removeInfantry(this); 3public class Goblin extends
Monster { private Goblin swornEnemy; public Goblin(String clanAffiliation, int ferocity, int defense,
int magic, int treasure, int health, Goblin sworntnemy) & super(clandffiliation, ferocity, defense,
magic, treasure, health); this. swornenemy = swornEnemy; 3 public Goblin getswornemony() &
return swornEnemy; 3 public void setSwornEnemy (Goblin swornEnemy) & this. swornEnemy =
swornEnemy; l