Question: In JAVA: Create a combastState class which will be used to help track the outcomes. Using the State Pattern, each state should have a fight

In JAVA: Create a combastState class which will be used to help track the outcomes. Using the State Pattern, each state should have a fight() method that takes in 2 Warriors. The first is the attacker and the second is the defender. The method should return the winning Warrior.
States:
Power If the attackers calculated power is greater than the defenders calculated power, the attacker wins. Defender wins on ties.
Attack If the attackers calculated attack is greater than the defenders calculated attack, the attacker wins. Defender wins on ties.
Defense If the attackers calculated defense is greater than the defenders calculated defense, the attacker wins. Defender wins on ties.
Traditional If the attackers calculated attack is greater than the defenders calculated defense, the attacker wins. Defender wins on ties.
Inverse If the attackers calculated defense is greater than the defenders calculated attack, the attacker wins. Attacker wins on ties.
Combat is the user of the state pattern. Combat should have a duel() method that takes in 2 Warriors. The first is the attacker and the second is the defender. Go through each of the 5 states. The Warrior that wins the most of the 5 fights is the winner. The method should return the winning Warrior.
Here is the provided code to create the warriors that will be dueling:
public class Warrior {
private final int level;
private final int attack;
private final int defense;
protected Warrior(Builder builder){
this.level = builder.level;
this.attack = builder.attack;
this.defense = builder.defense;
}
public int getLevel(){return level;}
public int getAttack(){return attack;}
public int getDefense(){ defense;}
// Template method: defines the overall algorithm
public double calculatePower(){
return calculateAttack()+ calculateDefense()+ calculateBoost();
}
// Abstract methods to be overridden by subclasses
protected int calculateAttack(){
return attack;
}
protected int calculateDefense(){
return defense;
}
protected double calculateBoost(){
return 0;
}
public static class Builder {
private final int level;
private int attack =0;
private int defense =0;
public Builder(int level){
if (level <0){
throw new IllegalArgumentException("Level cannot be negative");
}
this.level = level;
}
public Builder attack(int attack){
if (attack <0){
throw new IllegalArgumentException("Attack cannot be negative");
}
this.attack = attack;
return this;
}
public Builder defense(int defense){
if (defense <0){
throw new IllegalArgumentException("Defense cannot be negative");
}
this.defense = defense;
return this;
}
public Warrior build(){
return new Warrior(this);
}
}
}
public class AggressiveWarrior extends Warrior {
private AggressiveWarrior(Builder builder){
super(builder);
}
@Override
protected int calculateAttack(){
return getAttack()+2* getLevel();
}
@Override
protected int calculateDefense(){
return getDefense()+ getLevel();
}
@Override
protected double calculateBoost(){
return (double) getAttack()/2;
}
public static class Builder extends Warrior.Builder {
public Builder(int level){
super(level);
this.attack(3).defense(2);
}
@Override
public AggressiveWarrior build(){
return new AggressiveWarrior(this);
}
}
}
public class DefensiveWarrior extends Warrior {
// Constructor is private to enforce Builder pattern
private DefensiveWarrior(Builder builder){
super(builder);
}
@Override
protected int calculateAttack(){
return getAttack()+ getLevel();
}
@Override
protected int calculateDefense(){
return getDefense()+2* getLevel();
}
@Override
protected double calculateBoost(){
return (double) getDefense()/2;
}
public static class Builder extends Warrior.Builder {
public Builder(int level){
super(level);
this.attack(2).defense(3);
}
@Override
public DefensiveWarrior build(){
return new DefensiveWarrior(this);
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!