Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help reformatting the following code in Prototype Design Pattern. import java.util.ArrayList; import java.util.List; import java.util.Random; public class Main { public static void main(String[]
I need help reformatting the following code in Prototype Design Pattern.
import java.util.ArrayList; import java.util.List; import java.util.Random; public class Main { public static void main(String[] args) { Character char1 = new Orc("Grumlin"); Character char2 = new Elf("Therae"); int damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage"); char1.hasCastSpellSkill = true; damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage"); int damageTaken = char2.takeHit(); System.out.println(char2.name + " has taken a hit and " + "been dealt " + damageTaken + " damage"); char2.hasDodgeAttackSkill = true; damageTaken = char2.takeHit(); System.out.println(char2.name + " has taken a hit and " + "been dealt " + damageTaken + " damage"); } }
import java.util.Random; public abstract class Character { protected String name; protected int strength; protected int resilience; protected boolean hasCastSpellSkill; protected boolean hasDodgeAttackSkill; public int attackEnemy() { Random random = new Random(); int damageDealt; if (hasCastSpellSkill) { int spellDamage = random.nextInt(5); damageDealt = this.strength + spellDamage; } else { damageDealt = strength; } return damageDealt; } public int takeHit() { Random random = new Random(); int damageDealt = random.nextInt(15); int damageTaken; if (hasDodgeAttackSkill) { double chanceToDodge = random.nextDouble(); if (chanceToDodge > 0.50) { damageTaken = 0; } else { damageTaken = damageDealt - resilience; } } else { damageTaken = damageDealt - resilience; } if (damageTaken < 0) { damageTaken = 0; } return damageTaken; } }
public class Elf extends Character { public Elf(String name) { this.name = name; this.strength = 4; this.resilience = 2; } }
public class Orc extends Character { public Orc(String name) { this.name = name; this.strength = 10; this.resilience = 9; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Answer To refactor the provided code using the Prototype design pattern well create a prototype regi...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