diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..ec72ad2 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e8e1222 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/homework-java-ironbattle.iml b/.idea/homework-java-ironbattle.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/homework-java-ironbattle.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7f6a8e4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..54cb84e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Battle/pom.xml b/Battle/pom.xml new file mode 100644 index 0000000..532edd4 --- /dev/null +++ b/Battle/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + com.ironhack + Battle + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + \ No newline at end of file diff --git a/Battle/src/main/java/org/example/Attacker.java b/Battle/src/main/java/org/example/Attacker.java new file mode 100644 index 0000000..66f7eeb --- /dev/null +++ b/Battle/src/main/java/org/example/Attacker.java @@ -0,0 +1,5 @@ +package org.example; + +public interface Attacker { + void attack(Characters target); +} diff --git a/Battle/src/main/java/org/example/Characters.java b/Battle/src/main/java/org/example/Characters.java new file mode 100644 index 0000000..5132f10 --- /dev/null +++ b/Battle/src/main/java/org/example/Characters.java @@ -0,0 +1,38 @@ +package org.example; +import java.util.UUID; +public abstract class Characters { + private final String id; + private String name; + private int hp; + private boolean isAlive=true; + public Characters(String name,int hp){ + this.id = UUID.randomUUID().toString(); + this.name=name; + this.hp=hp; + this.isAlive=true; + } + public String getName(){ + return name; + } + public void setName(String name){ + this.name=name; + } + public int getHp(){ + return hp; + } + public void setHp(int hp){ + this.hp=hp; + if(this.hp<0) { + this.hp=0; + isAlive=false; + }} + public boolean getAlive(){return isAlive;} + public void setAlive(boolean isAlive){ + this.isAlive=isAlive; + } + +} + + + + diff --git a/Battle/src/main/java/org/example/Main.java b/Battle/src/main/java/org/example/Main.java new file mode 100644 index 0000000..803a27c --- /dev/null +++ b/Battle/src/main/java/org/example/Main.java @@ -0,0 +1,116 @@ +package org.example; + +import java.util.Scanner; +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + boolean exit = false; + System.out.println("================================="); + System.out.println(" WELCOME TO IRON BATTLE!"); + System.out.println("=================================\n"); + while (!exit) { + System.out.println("\n--- MAIN MENU ---"); + System.out.println("1. Create Battle"); + System.out.println("2. Exit"); + System.out.print("Choose an option: "); + int choice = scanner.nextInt(); + scanner.nextLine(); + switch (choice) { + case 1: + customBattle(scanner); + break; + case 2: + System.out.println("Thanks for playing! Goodbye!"); + exit = true; + break; + default: + System.out.println("Invalid option. Please try again."); + } + } + scanner.close(); + } + private static void customBattle(Scanner scanner) { + System.out.println("\n=== CREATE CHARACTER 1 ==="); + Characters character1 = createCharacter(scanner, "Character 1"); + System.out.println("\n=== CREATE CHARACTER 2 ==="); + Characters character2 = createCharacter(scanner, "Character 2"); + startBattle(character1, character2); + } + private static Characters createCharacter(Scanner scanner, String prompt) { + System.out.println("Choose character type:"); + System.out.println("1. Warrior"); + System.out.println("2. Wizard"); + System.out.print("Enter choice: "); + int type = scanner.nextInt(); + scanner.nextLine(); + System.out.print("Enter character name: "); + String name = scanner.nextLine(); + Characters character; + if (type == 1) { + character = new Warriors(name); + System.out.println("Warrior created! HP: " + character.getHp()); + } else { + character = new Wizards(name); + System.out.println("Wizard created! HP: " + character.getHp()); + } + return character; + } + private static void startBattle(Characters char1, Characters char2) { + int battleNumber = 1; + while (true) { + System.out.println("\n" + "=".repeat(50)); + System.out.println("BATTLE #" + battleNumber + " BEGINS!"); + System.out.println("=".repeat(50)); + System.out.println(char1.getName() + " (HP: " + char1.getHp() + ") VS " + + char2.getName() + " (HP: " + char2.getHp() + ")"); + System.out.println("=".repeat(50) + "\n"); +if (battleNumber > 1) { + String name1 = char1.getName(); + String name2 = char2.getName(); + if (char1 instanceof Warriors) { + char1 = new Warriors(name1); + } else { + char1 = new Wizards(name1); + } + if (char2 instanceof Warriors) { + char2 = new Warriors(name2); + } else { + char2 = new Wizards(name2); + } + System.out.println("Characters reset for new battle!"); + System.out.println(char1.getName() + " HP: " + char1.getHp()); + System.out.println(char2.getName() + " HP: " + char2.getHp()); + } + int round = 1; + while (char1.getAlive() && char2.getAlive()) { + System.out.println("\n--- Round " + round + " ---"); + if (char1 instanceof Attacker) { + ((Attacker) char1).attack(char2); + } + if (char2 instanceof Attacker) { + ((Attacker) char2).attack(char1); + } + System.out.println(char1.getName() + " HP: " + char1.getHp() + " | " + + char2.getName() + " HP: " + char2.getHp()); + round++; + } + System.out.println("\n" + "=".repeat(50)); + System.out.println(" BATTLE OVER!"); + System.out.println("=".repeat(50)); + if (!char1.getAlive() && !char2.getAlive()) { + System.out.println("IT'S A TIE! Both warriors fell!"); + System.out.println("Restarting battle to determine a clear winner...\n"); + battleNumber++; + continue; // Restart battle + } else if (char1.getAlive()) { + System.out.println(" WINNER: " + char1.getName()); + System.out.println("Remaining HP: " + char1.getHp()); + } else { + System.out.println("WINNER: " + char2.getName() ); + System.out.println("Remaining HP: " + char2.getHp()); + } + System.out.println("=".repeat(50)); + break; + } + } +} diff --git a/Battle/src/main/java/org/example/Warriors.java b/Battle/src/main/java/org/example/Warriors.java new file mode 100644 index 0000000..6b03d89 --- /dev/null +++ b/Battle/src/main/java/org/example/Warriors.java @@ -0,0 +1,49 @@ +package org.example; + import java.util.concurrent.ThreadLocalRandom; +public class Warriors extends Characters implements Attacker { + private int stamina; + private int strength; + public Warriors(String name){ + super(name,ThreadLocalRandom.current().nextInt(100, 201)); + this.stamina=ThreadLocalRandom.current().nextInt(10,51); + this.strength=ThreadLocalRandom.current().nextInt(1,11); + } + + public int getStamina() { + return stamina; + } + + public void setStamina(int stamina) { + this.stamina = stamina; + } + + public int getStrength() { + return strength; + } + + public void setStrength(int strength) { + this.strength = strength; + } + + @Override + public void attack(Characters target) { + int attackType = ThreadLocalRandom.current().nextInt(0, 2); + + if (attackType == 0 && this.stamina >= 5) { + int damage = this.strength; + this.stamina -= 5; + target.setHp(target.getHp() - damage); + System.out.println(getName() + " uses HEAVY ATTACK! Deals " + damage + " damage."); + } else if (this.stamina >= 1) { + int damage = this.strength / 2; + this.stamina += 1; + target.setHp(target.getHp() - damage); + System.out.println(getName() + " uses WEAK ATTACK! Deals " + damage + " damage."); + } else { + this.stamina += 2; + System.out.println(getName() + " is exhausted! Recovers 2 stamina."); + } + } + + } + diff --git a/Battle/src/main/java/org/example/Wizards.java b/Battle/src/main/java/org/example/Wizards.java new file mode 100644 index 0000000..32ac3d7 --- /dev/null +++ b/Battle/src/main/java/org/example/Wizards.java @@ -0,0 +1,50 @@ +package org.example; +import java.util.concurrent.ThreadLocalRandom; +public class Wizards extends Characters implements Attacker { + private int mana; + private int intelligence; + public Wizards(String name){ + super(name,ThreadLocalRandom.current().nextInt(50,101)); + this.mana=ThreadLocalRandom.current().nextInt(10,51); + this.intelligence=ThreadLocalRandom.current().nextInt(1,51); + } + + public int getMana() { + return mana; + } + + public void setMana(int mana) { + this.mana = mana; + } + + public int getIntelligence() { + return intelligence; + } + + public void setIntelligence(int intelligence) { + this.intelligence = intelligence; + } + + @Override + public void attack(Characters target) { + int attackType = ThreadLocalRandom.current().nextInt(0, 2); + + if (attackType == 0 && this.mana >= 5) { + int damage = this.intelligence; + this.mana -= 5; + target.setHp(target.getHp() - damage); + System.out.println(getName() + " casts FIREBALL! Deals " + damage + " damage."); + } else if (this.mana >= 1) { + int damage = 2; + this.mana += 1; + target.setHp(target.getHp() - damage); + System.out.println(getName() + " hits with STAFF! Deals " + damage + " damage."); + } else { this.mana += 2; + System.out.println(getName() + " out of mana! Recovers 2 mana."); + } + } + + + + } + diff --git a/Battle/target/classes/org/example/Attacker.class b/Battle/target/classes/org/example/Attacker.class new file mode 100644 index 0000000..a35e549 Binary files /dev/null and b/Battle/target/classes/org/example/Attacker.class differ diff --git a/Battle/target/classes/org/example/Characters.class b/Battle/target/classes/org/example/Characters.class new file mode 100644 index 0000000..a29c474 Binary files /dev/null and b/Battle/target/classes/org/example/Characters.class differ diff --git a/Battle/target/classes/org/example/Main.class b/Battle/target/classes/org/example/Main.class new file mode 100644 index 0000000..017b3f8 Binary files /dev/null and b/Battle/target/classes/org/example/Main.class differ diff --git a/Battle/target/classes/org/example/Warriors.class b/Battle/target/classes/org/example/Warriors.class new file mode 100644 index 0000000..0336872 Binary files /dev/null and b/Battle/target/classes/org/example/Warriors.class differ diff --git a/Battle/target/classes/org/example/Wizards.class b/Battle/target/classes/org/example/Wizards.class new file mode 100644 index 0000000..f8f3655 Binary files /dev/null and b/Battle/target/classes/org/example/Wizards.class differ diff --git a/README.md b/README.md index 71c0829..980d9e3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) +\![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) # HW | Java IronBattle (Unit 1 homework)