Files
HW-04-JAVA-KNIGHT/Java-Knight/src/main/java/org/project/Main.java
T
2026-05-15 23:50:32 +03:30

302 lines
10 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.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Enemies
Enemy goblin = new Goblin();
Enemy skeleton = new Skeleton();
Enemy vampire = new Vampire();
Enemy[] enemies = {goblin, vampire, skeleton}; // An array of the enemies except Dragon which is the final boss
// Locations, one for each enemy
Location forest = new Location("Whispering forest", goblin);
Location catacombs = new Location("forgotten catacombs", skeleton);
Location castle = new Location("crimson castle", vampire);
Location[] locations = {forest, catacombs, castle}; // An array of the location except peak where is the Dragon(final boss) located
System.out.println("\t=============== Java Knight ===============\n");
boolean running = true;
while(running){
System.out.println("\tChoose your option:");
System.out.println("\t1. Start game" +
" 2. Exit");
int option = scanner.nextInt();
if(option == 1){
startGame(locations);
} else if (option == 2) {
running = false;
break;
}
}
}
public static void startGame(Location[] locations){
//
Scanner scanner = new Scanner(System.in);
Random random = new Random();
// Set up
System.out.print("\tEnter your name: ");
String name = scanner.next();
Player player = null;
int option = 0;
while(option > 3 || option < 1){
System.out.println("\n Select your character: (Enter number)");
System.out.println("\t1. Knight (fights with a sword)\n" +
"\t2. Assassin (fights with dual daggers)\n" +
"\t3. Wizard (fights with his magic staff)");
if (scanner.hasNextInt()) {
option = scanner.nextInt();
} else {
System.out.println("\tInvalid input! Please enter a number.");
scanner.next();
}
}
switch (option){
case 1:
player = new Knight(name);
System.out.printf("\n\tYour Character: %s, Your Name: %s\n\n", "Knight", player.getName());
break;
case 2:
player = new Assassin(name);
System.out.printf("\n\tYour Character: %s, Your Name: %s\n\n", "Assassin", player.getName());
break;
case 3:
player = new Wizard(name);
System.out.printf("\n\tYour Character: %s, Your Name: %s\n\n", "Wizard", player.getName());
break;
default:
player = null;
break;
}
// Choosing location and enemy
Location loc = locations[random.nextInt(locations.length)];
loc = setLocation(loc, locations, player, random, scanner);
Enemy enemy = loc.getEnemy();
System.out.println("\t=============== Prepare to fight ===============");
while(player.isAlive()){
while(enemy.isAlive()){
playerMove(player, enemy, scanner);
if(!enemy.isStunned() && enemy.getAlive()) {
enemyMove(enemy, player, random);
}
else{
enemy.stun(false);
}
}
player.addXP(30);
enemy.heal();
// Key dropping (50% chance)
boolean[] logics = {false, true};
boolean drop = true;
if(enemy.hasKey() && drop){
player.addKey(enemy);
}
// resetting player and enemy for the next battle
player.fillMana();
enemy.fillHP();
// the final battle, Dragon fight
if(player.getKeys() == 3){
System.out.println("\t=============== Entering Ashen Peak to fight the Dragon ===============");
enemy = new Dragon();
loc = new Location("ashen peak", enemy);
while(enemy.isAlive() && player.isAlive()){
playerMove(player, enemy, scanner);
if(!enemy.isStunned() && enemy.isAlive()) {
enemyMove(enemy, player, random);
}
else{
enemy.stun(false);
}
}
break;
}
else{
loc = setLocation(loc, locations, player, random, scanner);
enemy = loc.getEnemy();
}
}
}
public static Location setLocation(Location currentLocation, Location[] locations, Player player, Random random, Scanner scanner){
System.out.printf("\t%s should fight %s who fights with %s in %s\n",
player.getName(), currentLocation.getEnemy().getName(), currentLocation.getEnemy().getWeapon().getName(), currentLocation.getName());
int option = 0;
while(option > 2 || option < 1){
System.out.println("\tChoose your option: ");
System.out.println("\t1. Fight " + currentLocation.getEnemy().getName() +
"\t2. Change location");
if (scanner.hasNextInt()) {
option = scanner.nextInt();
} else {
System.out.println("\tInvalid input! Please enter a number.");
scanner.next();
}
}
if(option == 1){
return currentLocation;
}
else{
Location[] temp_locations = new Location[locations.length - 1];
int counter = 0;
for(Location l : locations){
if (l != currentLocation) {
temp_locations[counter] = l;
counter++;
}
}
currentLocation = temp_locations[random.nextInt(temp_locations.length)];
System.out.println("\tChanging locations...\n");
setLocation(currentLocation, locations, player, random, scanner);
}
return currentLocation;
}
public static void playerMove(Player player, Enemy enemy, Scanner scanner){
player.defend(false);
int option = 0;
while(option > 5 || option < 1){
System.out.println("\n Your turn. Choose your move: \n" +
" 1. Light attack" +
" 2. Heavy attack" +
" 3. Defend" +
" 4. Heal" +
" 5. Special Ability");
if (scanner.hasNextInt()) {
option = scanner.nextInt();
if(option != 1 && player.getMp() <= 0){ // when mana runs out the only available action is light attack
System.out.println("\tYou are out of Mana. You can only choose \"1. Light attack\"");
option = 0;
}
else if(option == 4 && player.getHp() == player.getMaxHP()){
System.out.println("\tYour HP is at its maximum. Choose another option.");
option = 0;
}
} else {
System.out.println("\tInvalid input! Please enter a number.");
scanner.next();
}
}
switch (option){
case 1: // light attack
if(!enemy.isDefending()){
player.lightAttack(enemy);
}
break;
case 2: // heavy attack
if(player.getMp() >= player.getWeapon().getManaCost()) {
player.heavyAttack(enemy);
}
else{
System.out.println("\tYou don\'t have enough Mana");
playerMove(player, enemy, scanner);
}
break;
case 3: // defend
if(player.getMp() >= player.getDefendMP()) {
player.defend(true);
}
else{
System.out.println("\tYou don\'t have enough Mana");
playerMove(player, enemy, scanner);
}
break;
case 4: // heal
if(player.getMp() >= player.getDefendMP()) {
player.heal( (int)((0.2)*player.getMaxHP()) );
}
else{
System.out.println("\tYou don\'t have enough Mana");
playerMove(player, enemy, scanner);
}
break;
case 5: // special ability
if(player.getMp() >= player.getSpecialAbilityMP()) {
player.specialAbility(enemy);
}
else{
System.out.println("\tYou don\'t have enough Mana");
playerMove(player, enemy, scanner);
}
break;
}
showStatus(player, enemy);
}
public static void enemyMove(Enemy enemy, Player player, Random random){
enemy.defend(false);
enemy.stun(false); // have been checked before calling this method
System.out.println("\t" + enemy.getName() + "'s turn: ");
int move_id = random.nextInt(5) + 1;
switch (move_id){
case 1: // attack (light attack)
enemy.attack(player);
break;
case 2: // special ability (heavy attack)
if(enemy instanceof Skeleton && enemy.isRevived()){
enemyMove(enemy, player, random);
}
enemy.specialAbility(player);
break;
case 3: // defend
enemy.defend(true);
break;
case 4: // heal
enemy.heal( (int)((0.2)*player.getMaxHP()) );
break;
}
showStatus(player, enemy);
}
public static void showStatus(Player player, Enemy enemy){
System.out.printf("\t[%s - %d/%d HP | %d/%d Mana]\t[%s - %d/%d HP]\n\n",
player.getName(), player.getHp(), player.getMaxHP(), player.getMp(), player.getMaxMP(),
enemy.getName(), enemy.getHp(), enemy.getMaxHP()
);
}
}