Files
HW-04-JAVA-KNIGHT/Java-Knight/src/main/java/org/project/Main.java
T
2026-05-16 10:14:04 +03:30

380 lines
17 KiB
Java

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.Scanner;
import java.util.Random;
import java.util.ArrayList;
import java.util.List;
import static org.project.colors.ConsoleColor.*;
public class Main {
static boolean hasGoblinKey = false;
static boolean hasSkeletonKey = false;
static boolean hasVampireKey = false;
static boolean unlockedCastleL = false;
static int key = 0;
static int currentLocation = 0; // 0 = first location
public static void main(String[] args) {
List<Location> locations = new ArrayList<>();
ArrayList<Player> players = new ArrayList<>();
Player p1 = new Knight("Joan of Arc", 120, 80);
players.add(p1);
Player p2 = new Knight("Galahad", 120, 80);
players.add(p2);
Player p3 = new Wizard("Harry Potter", 90, 70);
players.add(p3);
Player p4 = new Assassin("Creed from Office", 70, 50);
players.add(p4);
ArrayList<Enemy> ghostTownEnemies = new ArrayList<>();
ghostTownEnemies.add(new Goblin(50, 10));
ghostTownEnemies.add(new Skeleton(50, 20));
ArrayList<Enemy> wreckShipEnemies = new ArrayList<>();
wreckShipEnemies.add(new Skeleton(55, 18));
wreckShipEnemies.add(new Vampire(75, 30));
wreckShipEnemies.add(new Vampire(75, 30));
wreckShipEnemies.add(new Vampire(75, 30));
wreckShipEnemies.add(new Vampire(75, 30));
wreckShipEnemies.add(new Vampire(75, 30));
ArrayList<Enemy> darkWoodEnemies = new ArrayList<>();
darkWoodEnemies.add(new Vampire(70, 28));
darkWoodEnemies.add(new Skeleton(60, 22));
darkWoodEnemies.add(new Vampire(70, 30));
darkWoodEnemies.add(new Vampire(75, 30));
darkWoodEnemies.add(new Vampire(55, 25));
darkWoodEnemies.add(new Vampire(60, 30));
darkWoodEnemies.add(new Vampire(75, 10));
darkWoodEnemies.add(new Goblin(45, 12));
Location l1 = new Location("GHOST TOWN", ghostTownEnemies);
locations.add(l1);
Location l2 = new Location("WRACK SHIP", wreckShipEnemies);
locations.add(l2);
Location l3 = new Location("DARK WOOD", darkWoodEnemies);
locations.add(l3);
Scanner scanner = new Scanner(System.in);
Random rand = new Random();
boolean player_isAlive;
boolean enemy_isAlive;
boolean player_isAlive_dragon;
boolean dragon_isAlive;
welcomeP();
System.out.println();
System.out.println(YELLOW + "Pick your player!" + RESET);
System.out.println();
System.out.println("1) " + p1.getName());
System.out.println("2) " + p2.getName());
System.out.println("3) " + p3.getName());
System.out.println("4) " + p4.getName());
System.out.println();
int pnum = scanner.nextInt();
System.out.println();
Player currentPlayer = players.get(pnum - 1);
while (true) {
showMenu();
int choose = scanner.nextInt();
switch (choose) {
case 1:
System.out.println(PURPLE + "You chose to Fight!" + RESET);
System.out.println();
Location selectedLocation = locations.get(currentLocation);
ArrayList<Enemy> locationEnemies = selectedLocation.getEnemies();
if (locationEnemies.isEmpty()) {
System.out.println("No enemies left in " + selectedLocation.getName() + "!");
System.out.println("Move to another location using option 2.");
break;
}
Enemy selectedEnemy = locationEnemies.get(rand.nextInt(locationEnemies.size()));
System.out.println("You as \"" + currentPlayer + "\" (" + currentPlayer.getClass().getSimpleName() + ')' + " and enemy as \"" +
selectedEnemy.getClass().getSimpleName() +
"\" will fight in " + selectedLocation + '!');
System.out.println();
enemy_isAlive = true;
player_isAlive = true;
while (enemy_isAlive && player_isAlive) {
System.out.println();
System.out.println(GREEN + "Your turn:" + RESET);
System.out.println("1) Light Attack | 2) Heavy Attack " + RED + "(-10 Mana) " + RESET + " | 3) Defend " + RED + "(-5 Mana)" + RESET + " | 4) Heal " + RED + "(-10 Mana)" + RESET + " | 5) Special Ability " + RED + "(-15 Mana)" + RESET);
int input = scanner.nextInt();
actions(selectedEnemy, currentPlayer, input);
if (selectedEnemy.getHp() <= 0) {
enemy_isAlive = false;
}
if (enemy_isAlive) {
System.out.println();
System.out.println(RED + selectedEnemy + "'s turn:" + RESET);
int enemyAction = rand.nextInt(4);
enemyAction(selectedEnemy, currentPlayer, enemyAction);
if (currentPlayer.getHp() <= 0) {
player_isAlive = false;
}
}
}
if (!enemy_isAlive) {
// Reset player HP/MP after battle
currentPlayer.setHp(currentPlayer.getMaxHP());
currentPlayer.setMp(currentPlayer.getMaxMP());
boolean dropped = keyDrop(selectedEnemy);
if (dropped) {
System.out.println(GREEN + "✨ A key dropped from the " + selectedEnemy.getClass().getSimpleName() + "! ✨" + RESET);
}
locationEnemies.remove(selectedEnemy);
if (key == 3) {
unlockedCastleL = true;
}
}
break;
case 2:
System.out.println();
System.out.println("Current location: " + locations.get(currentLocation).getName());
System.out.println();
for (int i = 0; i < locations.size(); i++) {
System.out.println((i + 1) + ") " + locations.get(i).getName());
}
System.out.println();
System.out.print("Choose location (1-3): ");
int newLoc = scanner.nextInt() - 1;
if (newLoc >= 0 && newLoc < locations.size()) {
currentLocation = newLoc;
System.out.println();
System.out.println("✨ You traveled to " + locations.get(currentLocation).getName() + "! ✨");
} else {
System.out.println("Invalid choice! You stay where you are.");
}
System.out.println();
break;
case 3:
if (key >= 3) {
System.out.println(GREEN + "You have all 3 keys! The castle gate opens..." + RESET);
System.out.println();
Dragon dragon = new Dragon(150, 50);
System.out.println("You as \"" + currentPlayer.getName() + "\" will fight the " + dragon + "!");
System.out.println();
player_isAlive_dragon = true;
dragon_isAlive = true;
while (dragon_isAlive && player_isAlive_dragon) {
System.out.println();
System.out.println(GREEN + "Your turn:" + RESET);
System.out.println("1) Light Attack | 2) Heavy Attack " + RED + "(-10 Mana) " + RESET + " | 3) Defend " + RED + "(-5 Mana)" + RESET + " | 4) Heal " + RED + "(-10 Mana)" + RESET + " | 5) Special Ability " + RED + "(-15 Mana)" + RESET);
int input = scanner.nextInt();
actions(dragon, currentPlayer, input);
if (dragon.getHp() <= 0) {
dragon_isAlive = false;
}
if (dragon_isAlive) {
System.out.println();
System.out.println(RED + "🐉 Dragon's turn:" + RESET);
int dragonAction = rand.nextInt(4);
enemyAction(dragon, currentPlayer, dragonAction);
System.out.println(currentPlayer.getName() + " HP: " + currentPlayer.getHp() + "/" + currentPlayer.getMaxHP());
if (currentPlayer.getHp() <= 0) {
player_isAlive_dragon = false;
}
}
}
if (!dragon_isAlive) {
System.out.println();
System.out.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
System.out.println("🎉" + GREEN + " VICTORY! YOU DEFEATED THE DRAGON! " + RESET + "🎉");
System.out.println();
System.out.println(BOLD_GREEN + "✨ " + "GAME COMPLETED" + " ✨" + RESET);
System.out.println();
System.out.println(BOLD_BLUE + "Thanks for playing JAVAKNIGHT!" + RESET);
System.exit(0);
}
if (!player_isAlive_dragon) {
System.out.println();
System.out.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
System.out.println("💀" + RED + " GAME OVER " + RESET + "💀");
System.out.println();
System.out.println("You were defeated by the Dragon...");
System.out.println("- Press Enter to exit");
scanner.next();
System.exit(0);
}
}
else {
System.out.println(RED + "You need all 3 keys first! (" + key + "/3 collected)" + RESET);
}
break;
default:
System.out.println(RED + "Invalid choice! Try again." + RESET);
break;
}
}
}
public static void showMenu() {
System.out.println(YELLOW + "Choose your next step" + RESET);
System.out.println();
System.out.println("1) Fight the enemy");
System.out.println("2) Move to another location");
System.out.println("3) Go to the castle to Fight the Dragon");
System.out.println();
}
public static void welcomeP() {
System.out.println();
System.out.println(BOLD_BLUE + " WELCOME TO JAVAKNIGHT" + RESET);
System.out.println();
System.out.println("To overcome this game you should first find all three keys by fighting" +
"\n 3 kinds of enemies. As you found them the final game" +
BOLD_RED + "\n \"Fighting the Dragon\"" + RESET +
"\n starts!");
}
public static void showLocations(List<Location> locations) {
System.out.println("Now in which location you would rather fight:");
for (int i = 1; i <= locations.size(); i++) {
System.out.println(i + ") " + locations.get(i - 1));
}
System.out.println();
}
public static void actions(Enemy enemy, Player player, int input) {
if (input == 1) {
System.out.println(YELLOW + " →" + RESET + BLUE + " choose:" + RESET + " Light Attack");
System.out.println(player.getName() + " (" + player.getClass().getSimpleName() + ") used Light Attack! (0 Mana)");
player.attack(enemy);
} else if (input == 2) {
System.out.println(YELLOW + " →" + RESET + BLUE + " choose:" + RESET + " Heavy Attack");
System.out.println(player.getName() + " (" + player.getClass().getSimpleName() + ") used Heavy Attack! (10 Mana)");
player.heavyAttack(enemy);
System.out.println(player.getName() + " Mana: " + player.getMp() + '/' + player.getMaxMP());
} else if (input == 3) {
System.out.println(YELLOW + " →" + RESET + BLUE + " choose:" + RESET + " Defend");
System.out.println(player.getName() + " (" + player.getClass().getSimpleName() + ") used Defend! (5 Mana)");
player.defend();
System.out.println(player.getName() + " Mana: " + player.getMp() + '/' + player.getMaxMP());
} else if (input == 4) {
System.out.println(YELLOW + " →" + RESET + BLUE + " choose:" + RESET + " Heal");
System.out.println(player.getName() + " (" + player.getClass().getSimpleName() + ") used Heal! (10 Mana)");
player.heal(15);
System.out.println(player.getName() + " HP: " + player.getHp() + '/' + player.getMaxHP());
System.out.println(player.getName() + " Mana: " + player.getMp() + '/' + player.getMaxMP());
} else if (input == 5) {
System.out.println(YELLOW + " →" + RESET + BLUE + " choose:" + RESET + " Special Ability");
System.out.println(player.getName() + " (" + player.getClass().getSimpleName() + ") used Special Ability! (15 Mana)");
player.specialAbility(enemy);
System.out.println(enemy.getClass().getSimpleName() + " has " + enemy.getHp() + '/' + enemy.getMaxHP() + " HP remaining.");
System.out.println(player.getName() + " Mana: " + player.getMp() + '/' + player.getMaxMP());
} else {
System.out.println(RED + "Invalid input!" + RESET);
}
}
public static boolean keyDrop(Enemy enemy) {
String enemyType = enemy.getClass().getSimpleName();
if (enemyType.equals("Goblin")) {
if (!hasGoblinKey) {
int randN = (int)(Math.random() * 100) + 1;
if (randN <= 50) {
hasGoblinKey = true;
key++;
return true;
}
}
} else if (enemyType.equals("Skeleton")) {
if (!hasSkeletonKey) {
int randN = (int)(Math.random() * 100) + 1;
if (randN <= 25) {
hasSkeletonKey = true;
key++;
return true;
}
}
} else if (enemyType.equals("Vampire")) {
if (!hasVampireKey) {
int randN = (int)(Math.random() * 100) + 1;
if (randN <= 20) {
hasVampireKey = true;
key++;
return true;
}
}
}
return false;
}
public static void enemyAction(Enemy enemy, Player currentPlayer, int enemyAction) {
if (enemyAction == 0) {
enemy.resetDefend();
enemy.attack(currentPlayer);
System.out.println(currentPlayer.getName() + " has " + currentPlayer.getHp() + '/' + currentPlayer.getMaxHP() + " HP remaining.");
} else if (enemyAction == 1) {
enemy.defend();
if (enemy.isDefending()) {
System.out.println(enemy + " defended!");
} else {
System.out.println(enemy + " tried to defend but failed!");
}
} else if (enemyAction == 2) {
System.out.println(enemy + " tries to heal!");
enemy.heal(10);
System.out.println(enemy + " HP: " + enemy.getHp() + '/' + enemy.getMaxHP());
} else {
System.out.println(enemy + " uses SPECIAL ABILITY!");
enemy.specialAbility(currentPlayer);
System.out.println(currentPlayer.getName() + " has " + currentPlayer.getHp() + '/' + currentPlayer.getMaxHP() + " HP remaining.");
}
}
}