Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a combastState class which will be used to help track the outcomes. Using the State Pattern, each state should have a fight ( )

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;
}
// Builder class to create Warrior instances
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

Describe what the for-in statement does.

Answered: 1 week ago

Question

=+b) Is this a prospective or retrospective study? Explain.

Answered: 1 week ago