diff --git a/Java-Knight/src/main/java/org/project/Game.java b/Java-Knight/src/main/java/org/project/Game.java new file mode 100644 index 0000000..dcbc704 --- /dev/null +++ b/Java-Knight/src/main/java/org/project/Game.java @@ -0,0 +1,242 @@ +package org.project; + +import org.project.entity.enemies.*; +import org.project.entity.players.Assassin; +import org.project.entity.players.Knight; +import org.project.entity.players.Player; +import org.project.entity.players.Wizard; +import org.project.location.Location; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +public class Game { + private final ArrayList players; + private final ArrayList regularEnemies; + private final ArrayList locations; + private final Scanner sc; + private Player player; + private final ArrayList> collectedKeyTypes = new ArrayList<>(); + + public Game() { + sc = new Scanner(System.in); + + players = new ArrayList<>(); + players.add(new Assassin()); + players.add(new Knight()); + players.add(new Wizard()); + + regularEnemies = new ArrayList<>(); + regularEnemies.add(new Goblin()); + regularEnemies.add(new Skeleton()); + regularEnemies.add(new Vampire()); + + locations = new ArrayList<>(); + locations.add(new Location("Jungle", regularEnemies)); + locations.add(new Location("China", new ArrayList<>(regularEnemies.subList(0, 2)))); + locations.add(new Location("Iran", new ArrayList<>(regularEnemies.subList(1, 3)))); + } + + public void start() { + player = choosePlayer(); + + while (true) { + displayKeys(); + Location location = chooseLocation(); + Enemy enemy = chooseEnemy(location); + + System.out.println("Wanna fight?\n1. Yes\n2. No"); + int choice = sc.nextInt(); + if (choice != 1) continue; + + combat(player, enemy); + System.out.println("Player HP: " + player.getHP()); + + if (!player.isAlive()) { + System.out.println("Game over."); + break; + } + + if (canChallengeDragon()) { + System.out.println("\nYou have collected all keys! The Dragon awaits..."); + System.out.println("Challenge the Dragon?\n1. Yes\n2. No"); + if (sc.nextInt() == 1) { + challengeDragon(); + break; + } + } + } + } + + private boolean canChallengeDragon() { + for (Enemy enemy : regularEnemies) { + if (!collectedKeyTypes.contains(enemy.getClass())) return false; + } + return true; + } + + private void challengeDragon() { + Dragon dragon = new Dragon(); + combat(player, dragon); + if (player.isAlive()) { + System.out.println("Congratulations! You defeated the Dragon and completed the game!"); + } else { + System.out.println("The Dragon was too powerful. Better luck next time."); + } + } + + private Player choosePlayer() { + System.out.print("1. Assassin\n2. Knight\n3. Wizard\nChoose your character: "); + int choice = sc.nextInt(); + Player chosen = switch (choice) { + case 1 -> players.get(0); + case 2 -> players.get(1); + default -> players.get(2); + }; + System.out.println("You chose " + chosen.getName() + + " (" + chosen.getClass().getSimpleName() + ")"); + return chosen; + } + + private void displayKeys() { + System.out.println("=".repeat(30)); + System.out.print("Keys collected: "); + List collected = new ArrayList<>(); + List missing = new ArrayList<>(); + for (Enemy enemy : regularEnemies) { + if (collectedKeyTypes.contains(enemy.getClass())) collected.add(enemy.getClass().getSimpleName()); + else missing.add(enemy.getClass().getSimpleName()); + } + if (collected.isEmpty()) { + System.out.println("none"); + } else { + System.out.println(String.join(", ", collected)); + } + if (!missing.isEmpty()) { + System.out.println("Still needed: " + String.join(", ", missing)); + } + System.out.println("=".repeat(30)); + } + + private void displayLocations() { + for (Location location : locations) { + System.out.println(location); + } + } + + private Location chooseLocation() { + displayLocations(); + System.out.print("1. Jungle\n2. China\n3. Iran\nChoose location: "); + int choice = sc.nextInt(); + Location location = switch (choice) { + case 1 -> locations.get(0); + case 2 -> locations.get(1); + default -> locations.get(2); + }; + System.out.println("You chose " + location.getName()); + return location; + } + + private Enemy chooseEnemy(Location location) { + int index = new Random().nextInt(location.getEnemies().size()); + Enemy enemy = location.getEnemies().get(index); + enemy.setHP(enemy.getMaxHP()); + enemy.setMP(enemy.getMaxMP()); + System.out.println("The enemy is: " + enemy.getClass().getSimpleName()); + return enemy; + } + + private void combat(Player player, Enemy enemy) { + int turn = 0; + + while (player.isAlive() && enemy.isAlive()) { + + if (turn == 0) { + System.out.println("Choose your action:"); + System.out.println("1. Light attack 2. Heavy attack 3. Defend" + + " 4. Heal 5. Special ability 6. Flask"); + int choice = sc.nextInt(); + boolean actionTaken = handlePlayerAction(choice, enemy); + if (!actionTaken) continue; + turn = 1; + printCombatStatus(enemy); + if (!enemy.isAlive()) break; + } + + else { + System.out.println("-".repeat(30)); + + if (player instanceof Assassin && player.getUsingSpecialAbility()) { + player.setUsingSpecialAbility(false); + System.out.println(enemy.getClass().getSimpleName() + " skipped its turn!"); + turn = 0; + continue; + } + + System.out.println(enemy.getClass().getSimpleName() + "'s turn:"); + boolean enemyActed = handleEnemyAction(enemy); + if (!enemyActed) continue; + turn = 0; + printCombatStatus(enemy); + if (!player.isAlive()) break; + } + } + + if (!player.isAlive()) { + System.out.println("You lost!"); + } else { + System.out.println("You won!"); + player.gainXP(Player.xpRewardFor(enemy)); + player.setHP(player.getMaxHP()); + player.setMP(player.getMaxMP()); + + if (!(enemy instanceof Dragon)) { + Class type = enemy.getClass(); + if (!collectedKeyTypes.contains(type)) { + collectedKeyTypes.add(type); + System.out.println(enemy.getClass().getSimpleName() + "'s key dropped!"); + player.achieveKey(enemy); + } else { + System.out.println("You already have " + enemy.getClass().getSimpleName() + "'s key."); + } + } + } + } + + private boolean handlePlayerAction(int choice, Enemy enemy) { + switch (choice) { + case 1 -> { player.lightAttack(enemy); return true; } + case 2 -> { player.heavyAttack(enemy); if (player.isSuccessfulAction()) return true; } + case 3 -> { player.defend(); if (player.isSuccessfulAction()) return true; } + case 4 -> { player.heal(10); if (player.isSuccessfulAction()) return true; } + case 5 -> { player.specialAbility(enemy); if (player.isSuccessfulAction()) return true; } + case 6 -> { player.getFlask().use(player); return true; } + default -> { System.out.println("Invalid choice."); return false; } + } + System.out.println("Not enough mana! Choose another action (1. Light attack is always free)."); + return false; + } + + private boolean handleEnemyAction(Enemy enemy) { + int choice = new Random().nextInt(3); + switch (choice) { + case 1 -> { enemy.defend(); if (enemy.isSuccessfulAction()) return true; } + case 2 -> { enemy.heal(10); if (enemy.isSuccessfulAction()) return true; } + } + + enemy.attack(player); + return true; + } + + + private void printCombatStatus(Enemy enemy) { + System.out.println("[" + player.getClass().getSimpleName() + " - " + player.getHP() + "/" + player.getMaxHP() + + " HP | " + player.getMP() + "/" + player.getMaxMP() + "]"); + System.out.println("[" + enemy.getClass().getSimpleName() + " - " + enemy.getHP() + "/" + enemy.getMaxHP() + + " HP | " + enemy.getMP() + "/" + enemy.getMaxMP() + "]"); + } + + +} \ No newline at end of file diff --git a/Java-Knight/src/main/java/org/project/Main.java b/Java-Knight/src/main/java/org/project/Main.java index 78c5c7d..7d1ecb5 100644 --- a/Java-Knight/src/main/java/org/project/Main.java +++ b/Java-Knight/src/main/java/org/project/Main.java @@ -1,198 +1,7 @@ package org.project; -import org.project.entity.enemies.*; -import org.project.entity.players.Assassin; -import org.project.entity.players.Knight; -import org.project.entity.players.Player; -import org.project.entity.players.Wizard; -import org.project.item.weapons.Sword; -import org.project.item.weapons.Weapon; -import org.project.location.Location; - -import java.util.ArrayList; -import java.util.List; -import java.util.Random; -import java.util.Scanner; - public class Main { public static void main(String[] args) { - Weapon wp = new Sword(); - ArrayList players = new ArrayList<>(); - players.add(new Assassin()); - players.add(new Knight()); - players.add(new Wizard()); - ArrayList enemies = new ArrayList<>(); - enemies.add(new Goblin()); - enemies.add(new Skeleton()); - enemies.add(new Vampire()); - ArrayList locations = new ArrayList<>(); - locations.add(new Location("Jungle", enemies)); - locations.add(new Location("China", new ArrayList<>(enemies.subList(0, 2)))); - locations.add(new Location("Iran", new ArrayList<>(enemies.subList(1, 3)))); - Player player; - Enemy enemy; - Location location; - Scanner sc = new Scanner(System.in); - - while (true) { - player = choosePlayer(players,sc); - while (true) { - location = chooseLocation(locations,sc); - enemy = chooseEnemy(location); - System.out.println("wanna fight?\n1.Yes\n2.No"); - int choice = sc.nextInt(); - if (choice == 1) { - combat(player,enemy,sc); - System.out.println("player AHP: " + player.getHP()); - break; - } - else { - continue; - } - } - - } - - - + new Game().start(); } -///////////////////////////////////////////////////////////////////////////////////////// - public static void displayLocations(List locations) { - for (Location location : locations) { - System.out.println(location); - } - } - - public static Player choosePlayer(ArrayList players, Scanner sc) { - System.out.print("1.Assassin\n2.Knight\n3.Wizard\nChoose your Character: "); - int choice = sc.nextInt(); - Player player; - switch (choice) { - case 1 -> player = players.get(0); - case 2 -> player = players.get(1); - default -> player = players.get(2); - } - System.out.println("You chose " + player.getName() + "(" + player.getClass().getSimpleName() + ")"); - return player; - } - - public static Location chooseLocation(ArrayList locations, Scanner sc) { - - displayLocations(locations); - System.out.print("1.Jungle\n2.China\n3.Iran\nChoose Location: "); - int choice = sc.nextInt(); - Location location = null; - - switch (choice) { - case 1 -> location = locations.get(0); - case 2 -> location = locations.get(1); - case 3 -> location = locations.get(2); - } - System.out.println("You chose " + location.getName() ); - return location; - } - - public static Enemy chooseEnemy(Location location) { - int random = new Random().nextInt(location.getEnemies().size()); - Enemy enemy = location.getEnemies().get(random); - System.out.println("The Enemy is: " + enemy.getClass().getSimpleName()); - return enemy; - } - - public static void combat(Player player, Enemy enemy, Scanner sc) { - int turn = 0; - int choice; - while (player.isAlive() && enemy.isAlive()) { - if (turn == 0) { - turn = 1; - System.out.println("choose your action:\n1.light attack 2.heavy attack " + - "3.defend 4.heal 5.special ability 6.flask"); - choice = sc.nextInt(); - switch (choice) { - case 1: - player.lightAttack(enemy); - break; - case 2: - player.heavyAttack(enemy); - if (player.isSuccessfulAction()) break; - else {turn = 0; continue;} - case 3: - player.defend(); - if (player.isSuccessfulAction()) break; - else {turn = 0; continue;} - case 4: - player.heal(10); - if (player.isSuccessfulAction()) break; - else {turn = 0; continue;} - - case 5: - player.specialAbility(enemy); - if (player.isSuccessfulAction()) break; - else {turn = 0; continue;} - - case 6: - player.getFlask().use(player); - break; - - default: - System.out.println("invalid choice"); - turn = 0; - continue; - } - System.out.println("[" + player.getClass().getSimpleName() + " - " + player.getHP() + "/" + player.getMaxHP() - + " HP | " + player.getMP() + "/" + player.getMaxMP() + " Mana" + "]"); - System.out.println("[" + enemy.getClass().getSimpleName() + " - " + enemy.getHP() + "/" + enemy.getMaxHP() - + " HP | " + enemy.getMP() + "/" + enemy.getMaxMP() + " Mana" + "]"); - - } - if (turn == 1) { - System.out.println("-".repeat(30)); - if (player instanceof Assassin) { - if (player.getUsingSpecialAbility()){ - player.setUsingSpecialAbility(false); - turn = 0; - System.out.println(enemy.getClass().getSimpleName() + " skipped it's turn!"); - continue; - } - } - System.out.println(enemy.getClass().getSimpleName() + "'s turn: "); - turn = 0; - choice = new Random().nextInt(3); - switch (choice) { - case 0: - enemy.attack(player); - if (enemy.isSuccessfulAction()) break; - else {turn = 1; continue;} - case 1: - enemy.defend(); - if (enemy.isSuccessfulAction()) break; - else {turn = 1; continue;} - case 2: - enemy.heal(10); - if (enemy.isSuccessfulAction()) break; - else {turn = 1; continue;} - } - } - System.out.println("[" + player.getClass().getSimpleName() + " - " + player.getHP() + "/" + player.getMaxHP() - + " HP | " + player.getMP() + "/" + player.getMaxMP() +" Mana" + "]"); - System.out.println("[" + enemy.getClass().getSimpleName() + " - " + enemy.getHP() + "/" + enemy.getMaxHP() - + " HP | " + enemy.getMP() + "/" + enemy.getMaxMP() + " Mana" + "]"); - - } - if (!player.isAlive()) { - System.out.println("You lost!"); - } - else { - System.out.println("You Won!"); - player.gainXP(Player.xpRewardFor(enemy)); - player.setHP(player.getMaxHP()); - player.setMP(player.getMaxMP()); - if (!(enemy instanceof Dragon) && enemy.dropKey()) { - System.out.println(enemy.getClass().getSimpleName() + "'s key dropped!"); - player.achieveKey(enemy); - } - } - } - } - - +} \ No newline at end of file diff --git a/Java-Knight/src/main/java/org/project/entity/players/Player.java b/Java-Knight/src/main/java/org/project/entity/players/Player.java index ee57d7b..149c265 100644 --- a/Java-Knight/src/main/java/org/project/entity/players/Player.java +++ b/Java-Knight/src/main/java/org/project/entity/players/Player.java @@ -1,9 +1,7 @@ package org.project.entity.players; import org.project.entity.Entity; -import org.project.entity.enemies.Dragon; -import org.project.entity.enemies.Goblin; -import org.project.entity.enemies.Skeleton; +import org.project.entity.enemies.*; import org.project.item.armors.Armor; import org.project.item.consumables.Consumable; import org.project.item.consumables.Flask; @@ -257,4 +255,10 @@ public abstract class Player implements Entity, CombatOptions { public int getXP() { return xp; } + public boolean hasKey(Enemy enemy) { + if (enemy instanceof Goblin) return hasGoblinKey; + if (enemy instanceof Skeleton) return hasSkeletonKey; + if (enemy instanceof Vampire) return hasVampireKey; + return false; + } } diff --git a/Java-Knight/target/classes/org/project/Main.class b/Java-Knight/target/classes/org/project/Main.class deleted file mode 100644 index 88934e8..0000000 Binary files a/Java-Knight/target/classes/org/project/Main.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/entity/Entity.class b/Java-Knight/target/classes/org/project/entity/Entity.class deleted file mode 100644 index c468ca1..0000000 Binary files a/Java-Knight/target/classes/org/project/entity/Entity.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/entity/enemies/Enemy.class b/Java-Knight/target/classes/org/project/entity/enemies/Enemy.class deleted file mode 100644 index 7aa7328..0000000 Binary files a/Java-Knight/target/classes/org/project/entity/enemies/Enemy.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/entity/enemies/Goblin.class b/Java-Knight/target/classes/org/project/entity/enemies/Goblin.class deleted file mode 100644 index 665930c..0000000 Binary files a/Java-Knight/target/classes/org/project/entity/enemies/Goblin.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/entity/enemies/Skeleton.class b/Java-Knight/target/classes/org/project/entity/enemies/Skeleton.class deleted file mode 100644 index 3010482..0000000 Binary files a/Java-Knight/target/classes/org/project/entity/enemies/Skeleton.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/entity/enemies/Vampire.class b/Java-Knight/target/classes/org/project/entity/enemies/Vampire.class deleted file mode 100644 index 0b56b74..0000000 Binary files a/Java-Knight/target/classes/org/project/entity/enemies/Vampire.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/entity/players/Assassin.class b/Java-Knight/target/classes/org/project/entity/players/Assassin.class deleted file mode 100644 index 4627003..0000000 Binary files a/Java-Knight/target/classes/org/project/entity/players/Assassin.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/entity/players/Knight.class b/Java-Knight/target/classes/org/project/entity/players/Knight.class deleted file mode 100644 index fe7fc1e..0000000 Binary files a/Java-Knight/target/classes/org/project/entity/players/Knight.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/entity/players/Player.class b/Java-Knight/target/classes/org/project/entity/players/Player.class deleted file mode 100644 index c33ffb6..0000000 Binary files a/Java-Knight/target/classes/org/project/entity/players/Player.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/entity/players/Wizard.class b/Java-Knight/target/classes/org/project/entity/players/Wizard.class deleted file mode 100644 index f27d4f2..0000000 Binary files a/Java-Knight/target/classes/org/project/entity/players/Wizard.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/item/Item.class b/Java-Knight/target/classes/org/project/item/Item.class deleted file mode 100644 index 16faf08..0000000 Binary files a/Java-Knight/target/classes/org/project/item/Item.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/item/armors/Armor.class b/Java-Knight/target/classes/org/project/item/armors/Armor.class deleted file mode 100644 index fbc5395..0000000 Binary files a/Java-Knight/target/classes/org/project/item/armors/Armor.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/item/armors/KnightArmor.class b/Java-Knight/target/classes/org/project/item/armors/KnightArmor.class deleted file mode 100644 index c7f1d24..0000000 Binary files a/Java-Knight/target/classes/org/project/item/armors/KnightArmor.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/item/consumables/Consumable.class b/Java-Knight/target/classes/org/project/item/consumables/Consumable.class deleted file mode 100644 index 5a9e76b..0000000 Binary files a/Java-Knight/target/classes/org/project/item/consumables/Consumable.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/item/consumables/Flask.class b/Java-Knight/target/classes/org/project/item/consumables/Flask.class deleted file mode 100644 index b1ebb43..0000000 Binary files a/Java-Knight/target/classes/org/project/item/consumables/Flask.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/item/weapons/Sword.class b/Java-Knight/target/classes/org/project/item/weapons/Sword.class deleted file mode 100644 index f22db40..0000000 Binary files a/Java-Knight/target/classes/org/project/item/weapons/Sword.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/item/weapons/Weapon.class b/Java-Knight/target/classes/org/project/item/weapons/Weapon.class deleted file mode 100644 index 0fd0e27..0000000 Binary files a/Java-Knight/target/classes/org/project/item/weapons/Weapon.class and /dev/null differ diff --git a/Java-Knight/target/classes/org/project/location/Location.class b/Java-Knight/target/classes/org/project/location/Location.class deleted file mode 100644 index 1b314d5..0000000 Binary files a/Java-Knight/target/classes/org/project/location/Location.class and /dev/null differ