diff --git a/main/java/com/example/Attacker.java b/main/java/com/example/Attacker.java new file mode 100644 index 0000000..df2ab5d --- /dev/null +++ b/main/java/com/example/Attacker.java @@ -0,0 +1,5 @@ +package com.example; + +public interface Attacker { + void Attack(Character target); +} diff --git a/main/java/com/example/BattleSimulator.java b/main/java/com/example/BattleSimulator.java new file mode 100644 index 0000000..6ed4aa7 --- /dev/null +++ b/main/java/com/example/BattleSimulator.java @@ -0,0 +1,47 @@ +package com.example; + +import java.util.Random; + +public class BattleSimulator { + public static void duel(Character c1,Character c2){ + System.out.println("Duel Started - Player one: " + + c1.getName() + + " | Player two: " + + c2.getName()); + int round = 1; + boolean WinnerFound=false; + + while (c1.isAlive() && c2.isAlive()) { + System.out.println("Round " + round + ":"); + + + if (c1 instanceof Attacker) ((Attacker)c1).Attack(c2); + if (c2 instanceof Attacker) ((Attacker)c2).Attack(c1); + + System.out.println(c1.getName() + " HP: " + c1.getHp()); + System.out.println(c2.getName() + " HP: " + c2.getHp()); + System.out.println("-------------------\n"); + + round++; + + + } + if (c1.isAlive() && !c2.isAlive()) { + System.out.println(c1.getName() + " WINS!"); + WinnerFound=true; + } else if (!c1.isAlive() && c2.isAlive()) { + System.out.println(c2.getName() + " WINS!"); + WinnerFound=true; + } else { + System.out.println("It's a TIE! Restarting battle..."); + c1.setHp(100 + new Random().nextInt(101)); + c1.setAlive(true); + c2.setHp(100 + new Random().nextInt(101)); + c2.setAlive(true); + + } + + + } + +} diff --git a/main/java/com/example/Character.java b/main/java/com/example/Character.java new file mode 100644 index 0000000..9fe0774 --- /dev/null +++ b/main/java/com/example/Character.java @@ -0,0 +1,57 @@ +package com.example; + +public abstract class Character { + private String id; + private String name; + private int hp; + private boolean isAlive=true; + + public String getId() { + return id; + } + + public void setId(String id) { + if (id==null){ + throw new NullPointerException("The id cannot be null"); + } + else{ + this.id=id; + } + } + + public String getName() { + return name; + } + + public void setName(String name) { + if(name==null){ + throw new NullPointerException("The name cannot be null"); + } + else{ + this.name = name; + } + } + + public int getHp() { + return hp; + } + + public void setHp(int hp) { + this.hp = Math.max(0,hp); + if(this.hp==0){this.isAlive=false;} + } + + public boolean isAlive() { + return isAlive; + } + + public void setAlive(boolean alive) { + this.isAlive = alive; + } + + public Character(String name, int hp) { + this.id=super.toString(); + this.name = name; + this.hp = hp; + } +} diff --git a/main/java/com/example/Main.java b/main/java/com/example/Main.java new file mode 100644 index 0000000..1564d00 --- /dev/null +++ b/main/java/com/example/Main.java @@ -0,0 +1,34 @@ +package com.example; + +import java.util.Random; + +import static com.example.BattleSimulator.duel; + +public class Main { + public static void main(String[] args){ + try{ + Random random=new Random(); + Warrior warrior = new Warrior( + "Knight", + 150 + random.nextInt(51), + 20 + random.nextInt(31), + 5 + random.nextInt(6) + ); + + Wizard wizard = new Wizard( + "Saruman", + 80 + random.nextInt(41), + 15 + random.nextInt(21), + 8 + random.nextInt(16) + ); + + duel(warrior,wizard); + } + catch (IllegalArgumentException e) { + System.out.println("Invalid number: " + e.getMessage()); + } + finally { + System.out.println("Code was executed"); + } + } +} diff --git a/main/java/com/example/Warrior.java b/main/java/com/example/Warrior.java new file mode 100644 index 0000000..c4aa04d --- /dev/null +++ b/main/java/com/example/Warrior.java @@ -0,0 +1,46 @@ +package com.example; + +import java.util.Random; + +public class Warrior extends Character implements Attacker { + + private int stamina; + private int strength; + private Random random = new Random(); + + public Warrior(String name, int hp, int strength, int stamina) { + super(name, hp); + this.stamina = stamina; + this.strength = strength; + } + + public int getStamina() { return stamina; } + public void setStamina(int stamina) { this.stamina = Math.max(0, stamina); } + + public int getStrength() { return strength; } + public void setStrength(int strength) { this.strength = Math.max(0, strength); } + + @Override + public void Attack(Character target) { + + boolean heavyAttack = random.nextBoolean(); + int damage = 0; + + if (heavyAttack && stamina >= 5) { + damage = strength; + target.setHp(target.getHp() - damage); + stamina -= 5; + System.out.println(getName() + " attacks " + target.getName() + " with HEAVY attack for " + damage + " damage"); + } + else if (stamina >= 1) { + damage = strength / 2; + target.setHp(target.getHp() - damage); + stamina += 1; + System.out.println(getName() + " attacks " + target.getName() + " with WEAK attack for " + damage + " damage"); + } + else { + stamina += 2; + System.out.println(getName() + " is exhausted and recovers stamina"); + } + } +} \ No newline at end of file diff --git a/main/java/com/example/Wizard.java b/main/java/com/example/Wizard.java new file mode 100644 index 0000000..1507556 --- /dev/null +++ b/main/java/com/example/Wizard.java @@ -0,0 +1,44 @@ +package com.example; + +import java.util.Random; + +public class Wizard extends Character implements Attacker { + + private int mana; + private int intelligence; + private Random random = new Random(); + + public Wizard(String name, int hp, int mana, int intelligence) { + super(name, hp); + this.mana = mana; + this.intelligence = intelligence; + } + + public int getMana() { return mana; } + public void setMana(int mana) { this.mana = Math.max(0, mana); } + + public int getIntelligence() { return intelligence; } + public void setIntelligence(int intelligence) { this.intelligence = Math.max(0, intelligence); } + + @Override + public void Attack(Character target) { + + boolean fireball = random.nextBoolean(); + int damage = 0; + + if (fireball && mana >= 5) { + damage = intelligence; + target.setHp(target.getHp() - damage); + mana -= 5; + System.out.println(getName() + " attacks " + target.getName() + " with FIREBALL for " + damage + " damage"); + } + else if (mana > 0) { + damage = 2; + target.setHp(target.getHp() - damage); + mana += 1; + } else { + mana += 2; + System.out.println(getName() + " has no mana and recovers"); + } + } +} \ No newline at end of file diff --git a/test/java/WizardTest.java b/test/java/WizardTest.java new file mode 100644 index 0000000..b8cf715 --- /dev/null +++ b/test/java/WizardTest.java @@ -0,0 +1,37 @@ +import com.example.Wizard; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class WizardTest { + private Wizard wizard; + @BeforeEach + void setUp(){ + wizard=new Wizard("Sauron",100,20,35); + } + @Test + void NewWizardTest(){ + assertNotNull(wizard); + assertEquals("Sauron",wizard.getName()); + assertEquals(100,wizard.getHp()); + assertEquals(20,wizard.getMana()); + assertEquals(35,wizard.getIntelligence()); + } + @Test + void WizardGettersTest(){ + assertEquals("Sauron",wizard.getName()); + assertEquals(100,wizard.getHp()); + assertEquals(20,wizard.getMana()); + assertEquals(35,wizard.getIntelligence()); + } + @Test + void WizardSettersTest(){ + wizard=new Wizard("Gandalf",101,20,34); + assertEquals("Gandalf",wizard.getName()); + assertEquals(101,wizard.getHp()); + assertEquals(20,wizard.getMana()); + assertEquals(34,wizard.getIntelligence()); + } + +}