update classes and fixed bugs

This commit is contained in:
2026-05-09 15:13:37 +03:30
parent 8d3bddbe0a
commit 95076fce9b
41 changed files with 585 additions and 118 deletions
+5
View File
@@ -16,5 +16,10 @@
<option name="name" value="JBoss Community repository" /> <option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" /> <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository> </remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://maven.myket.ir/" />
</remote-repository>
</component> </component>
</project> </project>
@@ -0,0 +1,387 @@
package org.project;
import org.project.entity.Entity;
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.Dagger;
import org.project.item.weapons.Sword;
import org.project.location.Location;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import static org.project.entity.enemies.Enemy.GREEN;
import static org.project.entity.players.Player.*;
public class Game {
private Player player;
private Scanner scanner;
private Location currentLocation;
private List<Location> allLocations;
private Enemy currentEnemy;
private Random random = new Random();
private boolean hasGoblinKey = false;
private boolean hasSkeletonKey = false;
private boolean hasVampireKey = false;
private List<Enemy> allEnemiesInGame = new ArrayList<>();
protected static final String GREEN_LIGHT = "\033[92m";
protected static final String PURPLE = "\033[35m";
public Game()
{
this.scanner = new Scanner(System.in);
this.allLocations = setupWorld();
}
public void start()
{
System.out.println("\n🏰 Welcome to Java Knight!");
this.player = createPlayer();
selectStartingLocation();
spawnRandomEnemy();
gameLoop();
}
public Player createPlayer()
{
System.out.println("Enter your name: ");
String name = scanner.nextLine();
System.out.println("1. ⚔️ Knight (High Damage, Balanced HP)");
System.out.println("2. 🧙 Wizard (High HP, Magic Power)");
System.out.println("3. 🗡️ Assassin (High Mana, Critical Hits)");
int choice = getIntInput("Choose your character: ");
Player newPlayer = null;
switch (choice)
{
case 1:
System.out.println("You chose the Knight!");
newPlayer = new Knight(name);
break;
case 2:
System.out.println("You chose the Wizard!");
newPlayer = new Wizard(name);
break;
case 3:
System.out.println("You chose the Assassin!");
newPlayer = new Assassin(name);
break;
default:
System.out.println("Invalid choice. Defaulting to Knight.");
newPlayer = new Knight(name);
break;
}
return newPlayer;
}
public void selectStartingLocation()
{
System.out.println("🗺️ Choose your starting location:");
for (int i = 0; i < allLocations.size(); i++)
{
System.out.println((i+1) + ". " + allLocations.get(i).getName());
}
int choice = getIntInput("Enter your choice: ");
if (choice > 0 && choice <= allLocations.size())
{
this.currentLocation = allLocations.get(choice - 1);
System.out.println("🚶 You have arrived at: " + currentLocation.getName());
}
else
{
System.out.println("Invalid choice. Defaulting to Forest.");
this.currentLocation = allLocations.get(0);
}
}
private List<Location> setupWorld()
{
List<Location> locations = new ArrayList<>();
ArrayList<Enemy> forestEnemies = new ArrayList<>();
forestEnemies.add(new Goblin(new Dagger()));
forestEnemies.add(new Goblin(new Dagger()));
forestEnemies.add(new Goblin(new Dagger()));
forestEnemies.add(new Goblin(new Dagger()));
forestEnemies.add(new Skeleton(new Sword()));
forestEnemies.add(new Vampire(new Dagger()));
forestEnemies.add(new Skeleton(new Sword()));
forestEnemies.add(new Vampire(new Dagger()));
forestEnemies.add(new Skeleton(new Sword()));
Location forest = new Location("Dark Forest", new ArrayList<>(), forestEnemies);
allEnemiesInGame.addAll(forestEnemies);
ArrayList<Enemy> caveEnemies = new ArrayList<>();
caveEnemies.add(new Skeleton(new Sword()));
caveEnemies.add(new Skeleton(new Sword()));
caveEnemies.add(new Skeleton(new Sword()));
caveEnemies.add(new Skeleton(new Sword()));
caveEnemies.add(new Goblin(new Dagger()));
caveEnemies.add(new Vampire(new Dagger()));
caveEnemies.add(new Goblin(new Dagger()));
caveEnemies.add(new Vampire(new Dagger()));
caveEnemies.add(new Vampire(new Dagger()));
Location cave = new Location("Spooky Cave", new ArrayList<>(), caveEnemies);
allEnemiesInGame.addAll(caveEnemies);
ArrayList<Enemy> swampEnemies = new ArrayList<>();
swampEnemies.add(new Vampire(new Dagger()));
swampEnemies.add(new Vampire(new Dagger()));
swampEnemies.add(new Vampire(new Dagger()));
swampEnemies.add(new Vampire(new Dagger()));
swampEnemies.add(new Goblin(new Dagger()));
swampEnemies.add(new Skeleton(new Sword()));
swampEnemies.add(new Goblin(new Dagger()));
swampEnemies.add(new Goblin(new Dagger()));
swampEnemies.add(new Skeleton(new Sword()));
Location swamp = new Location("Swamp", new ArrayList<>(), swampEnemies);
allEnemiesInGame.addAll(swampEnemies);
locations.add(forest);
locations.add(cave);
locations.add(swamp);
return locations;
}
public void spawnRandomEnemy()
{
ArrayList<Enemy> enemies = currentLocation.getEnemies();
if (enemies == null || enemies.isEmpty()) {
System.out.println("🌲 The area is quiet... no enemies nearby. Move to another location.");
this.currentEnemy = null;
return;
}
int index = random.nextInt(enemies.size());
this.currentEnemy = enemies.get(index);
this.currentEnemy = enemies.remove(index);
allEnemiesInGame.remove(this.currentEnemy);
System.out.println("⚠️ A wild " + currentEnemy.getClass().getSimpleName() + " appears in " + currentLocation.getName() + "!");
}
public void gameLoop()
{
while (player.isAlive())
{
System.out.println("\n========================================");
System.out.println("📍 Location: " + currentLocation.getName());
System.out.println("❤️ Player HP: " + player.getHp() + "/" + player.getMaxHP());
System.out.println("💎 Player MP: " + player.getMp() + "/" + player.getMaxMP());
if (currentEnemy != null && currentEnemy.isAlive())
{
System.out.println("Enemy: " + currentEnemy.getClass().getSimpleName() + " (HP: " + currentEnemy.getHp() + ")");
}
System.out.println("---- Actions ----");
System.out.println("1. 🥷 Fight enemy");
System.out.println("2. 🚶 Move to another location");
System.out.println("3. 🔒 Go to Dragon Castle");
System.out.println("4. 🏳️ Quit game");
int choice = getIntInput("Enter your choice: ");
switch (choice)
{
case 1: //fight
if (currentEnemy == null)
{
spawnRandomEnemy();
}
if (currentEnemy != null)
{
System.out.println("⚔️ Battle Started: " + player.getName() + " vs " + currentEnemy.getClass().getSimpleName());
fight(currentEnemy);
postCombat(currentEnemy);
spawnRandomEnemy();
}
else
{
System.out.println("⚠️ No enemy found in this location.");
}
break;
case 2: //move
moveLocation();
spawnRandomEnemy();
break;
case 3:
if (hasGoblinKey && hasSkeletonKey && hasVampireKey)
{
System.out.println(PURPLE+"🐉 Entering Dragon's lair..."+RESET);
startDragonFight();
}
else
{
System.out.println("🔒 Locked! Collect all 3 keys.");
}
break;
case 4:
System.out.println("Byyyeee!");
return;
default:
System.out.println("Invalid."); break;
}
}
System.out.println(RED+"==== 💀 Game Over! ===="+RESET);
}
private void fight(Enemy enemy)
{
while (player.isAlive() && enemy.isAlive())
{
handlePlayerTurn(enemy);
if (enemy.isStunned())
{
System.out.println("Enemy is stunned and skips its turn!");
enemy.unstun();
}
else
{
enemy.attack(player);
}
}
}
private void handlePlayerTurn(Entity target)
{
System.out.println(BLUE+"\n--- Your Turn ---"+RESET);
System.out.println("1. Light Attack | 2. Heavy Attack (-20 Mana) | 3. Defend (-15 Mana) | 4. Heal (-30 Mana) | 5. Special (-50 Mana)");
int action = getIntInput("Choose action: ");
switch (action)
{
case 1: player.lightAttack(target); break;
case 2: player.heavyAttack(target); break;
case 3: player.defend(); break;
case 4: player.heal(20); break;
case 5: player.specialAbility(target); break;
default: System.out.println("Invalid!"); break;
}
}
public void postCombat(Enemy enemy)
{
if (player.isAlive())
{
int xp = enemy.getXpReward();
player.addXp(xp);
checkKeyDrop(enemy);
player.setHp(player.getMaxHP());
player.fillMana(player.getMaxMP() - player.getMp());
System.out.println("💖 HP and MP fully restored!");
}
}
public void checkKeyDrop(Enemy enemy)
{
if (enemy instanceof Goblin && hasGoblinKey) return;
if (enemy instanceof Skeleton && hasSkeletonKey) return;
if (enemy instanceof Vampire && hasVampireKey) return;
String enemyType = enemy.getClass().getSimpleName();
boolean isLastOne = true;
for (Enemy e : allEnemiesInGame)
{
if (e.getClass().getSimpleName().equals(enemyType))
{
isLastOne = false;
break;
}
}
double dropChance;
if (isLastOne)
{
dropChance = 1.0;
System.out.println("✨ This is the last " + enemyType + " in the world!");
}
else
{
dropChance = 0.2; //20% chance
}
if (Math.random() < dropChance)
{
if (enemy instanceof Goblin)
{
hasGoblinKey = true;
System.out.println(GREEN + "🔑 You found the Goblin Key!" + RESET);
}
else if (enemy instanceof Skeleton)
{
hasSkeletonKey = true;
System.out.println(GREEN + "🔑 You found the Skeleton Key!" + RESET);
}
else if (enemy instanceof Vampire)
{
hasVampireKey = true;
System.out.println(GREEN + "🔑 You found the Vampire Key!" + RESET);
}
}
}
private void moveLocation()
{
System.out.println("🗺️ Available destinations:");
for (int i = 0; i < allLocations.size(); i++)
{
System.out.println((i + 1) + ". " + allLocations.get(i).getName());
}
int choice = getIntInput("Choose destination: ");
if (choice > 0 && choice <= allLocations.size())
{
currentLocation = allLocations.get(choice - 1);
System.out.println("🚶 You traveled to " + currentLocation.getName());
}
}
private void startDragonFight() {
System.out.println("🐉 THE DRAGON APPEARS! PREPARE FOR BATTLE!");
Dragon dragon = new Dragon(null);
fight(dragon);
if (player.isAlive())
{
System.out.println(GREEN_LIGHT + "\n== 🏆 VICTORY! You have defeated the Dragon and broken the curse! ==" + RESET);
System.out.println("🎉 You are the Hero of the Realm!");
}
else
{
System.out.println(RED + "\n==== 💀 You have been slain by the Dragon! ====" + RESET);
}
}
private int getIntInput(String prompt) {
System.out.print(prompt);
while (!scanner.hasNextInt()) {
System.out.println("⚠️ Please enter a valid number!");
scanner.next();
System.out.print(prompt);
}
return scanner.nextInt();
}
}
@@ -1,15 +1,24 @@
package org.project; package org.project;
import org.project.entity.players.Player;
import org.project.location.Location; import org.project.location.Location;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// TODO: ADD LOCATIONS TO YOUR GAME
List<Location> locations = new ArrayList<>();
// TODO: IMPLEMENT GAMEPLAY try
{
Game game = new Game();
game.start();
}
catch (Exception e) {
System.err.println("❌ Error: " + e.getMessage());
e.printStackTrace();
}
} }
} }
@@ -4,38 +4,57 @@ import org.project.entity.Entity;
import org.project.entity.players.Player; import org.project.entity.players.Player;
import org.project.item.weapons.Weapon; import org.project.item.weapons.Weapon;
import static org.project.entity.players.Player.BLUE;
import static org.project.entity.players.Player.RED;
public class Dragon extends Enemy{ public class Dragon extends Enemy{
private static final double BREATH_DAMAGE_PENETRATION = 0.5; private static final double BREATH_DAMAGE_PENETRATION = 0.5;
public Dragon(Weapon weapon) public Dragon(Weapon weapon)
{ {
super(200, 0, weapon); super(250, 0, weapon);
} }
@Override @Override
public void attack(Entity target) public void attack(Entity target)
{ {
if(!this.isAlive()) return; if(!this.isAlive()) return;
System.out.println(BLUE+"-- Dragon's Turn --"+RESET);
int damage = 25; int damage = 25;
if(super.getWeapon() != null) if(super.getWeapon() != null)
{ {
damage += super.getWeapon().getDamage(); damage += super.getWeapon().getDamage();
} }
target.takeDamage(damage);
System.out.println("🐉 Dragon used Fire Breath!");
if (target instanceof Player) if (target instanceof Player)
{ {
Player player = (Player) target; Player player = (Player) target;
if (player.isDefending()) if (player.isDefending())
{ {
int penetrationDamage = (int) (damage * BREATH_DAMAGE_PENETRATION); System.out.println("🔥 Dragon's fiery breath bypasses the shield!");
player.takeDamage(penetrationDamage); System.out.println("💥 " + player.getName() + " takes FULL damage!");
System.out.println("🔥 Dragon's fiery breath bypassed the shield!"); //instead of player.takeDamage (Bypassing Defense in takeDamage method)
System.out.println("💥 " + player.getName() + " took " + penetrationDamage + " additional damage from fire!"); player.setHp(player.getHp() - damage);
System.out.println("💥 " + player.getName() + " took " + RED + damage + RESET + " damage!");
System.out.println("❤️ " + player.getName() + " HP remaining: " + player.getHp() + "/" + player.getMaxHP());
player.setDefending(false);
} }
else
{
System.out.println("🐉 Dragon used Fire Breath!");
player.takeDamage(damage);
}
}
else
{
target.takeDamage(damage);
}
if (!target.isAlive())
{
Player player = (Player) target;
System.out.println("💀 " + player.getName() + " has been defeated!");
} }
} }
} }
@@ -9,7 +9,7 @@ public abstract class Enemy implements Entity{
private int hp; private int hp;
private int mp; private int mp;
private int maxHp; private int maxHp;
private boolean isAlive = true; protected boolean isAlive = true;
private int maxMp; private int maxMp;
private int xpReward; private int xpReward;
@@ -19,26 +19,32 @@ public abstract class Enemy implements Entity{
public static final String GREEN = "\u001B[32m"; public static final String GREEN = "\u001B[32m";
public static final String RESET = "\u001B[0m"; public static final String RESET = "\u001B[0m";
public static final String ORANGE = "\u001B[33m";
public Enemy(int hp, int mp, Weapon weapon) { public Enemy(int maxHp, int mp, Weapon weapon) {
this.hp = hp; this.maxHp = maxHp;
this.hp = maxHp;
this.mp = mp; this.mp = mp;
this.weapon = weapon; this.weapon = weapon;
this.maxHp = maxHp;
} }
@Override @Override
public void takeDamage(int damage) { public void takeDamage(int damage) {
if (!isAlive) return; if (!isAlive) return;
hp -= damage; hp -= damage;
if (this.hp < 0)
{
this.hp = 0;
}
System.out.println("❤️ " + this.getClass().getSimpleName() + " HP remaining: " + hp + "/" + maxHp); System.out.println("❤️ " + this.getClass().getSimpleName() + " HP remaining: " + hp + "/" + maxHp);
if (this.hp <= 0) { if (this.hp <= 0) {
this.hp = 0; this.hp = 0;
this.isAlive = false; this.isAlive = false;
System.out.println("💀 "+ ORANGE+ this.getClass().getSimpleName() + " has been defeated!"+RESET);
} }
} }
@@ -86,6 +92,10 @@ public abstract class Enemy implements Entity{
this.isStunned = b; this.isStunned = b;
} }
public boolean isStunned() {
return isStunned;
}
public int getXpReward() public int getXpReward()
{ {
return xpReward; return xpReward;
@@ -128,4 +138,9 @@ public abstract class Enemy implements Entity{
public int getMaxMP() { public int getMaxMP() {
return maxHp; return maxHp;
} }
public void unstun()
{
this.isStunned = false;
}
} }
@@ -3,17 +3,20 @@ package org.project.entity.enemies;
import org.project.entity.Entity; import org.project.entity.Entity;
import org.project.item.weapons.Weapon; import org.project.item.weapons.Weapon;
import static org.project.entity.players.Player.BLUE;
public class Goblin extends Enemy{ public class Goblin extends Enemy{
public Goblin(Weapon weapon) public Goblin(Weapon weapon)
{ {
super(30, 0, weapon); super(50, 0, weapon);
} }
@Override @Override
public void attack(Entity target) public void attack(Entity target)
{ {
if (!this.isAlive()) return; if (!this.isAlive()) return;
System.out.println(BLUE+"-- Goblin's Turn --"+RESET);
int baseDamage = 0; int baseDamage = 0;
if (super.getWeapon() != null) if (super.getWeapon() != null)
{ {
@@ -26,13 +29,19 @@ public class Goblin extends Enemy{
if (Math.random() < 0.4) if (Math.random() < 0.4)
{ {
int criticalDamage = baseDamage*2; int criticalDamage = baseDamage*2;
target.takeDamage(criticalDamage);
System.out.println("👹 Goblin used Critical Strike!"); System.out.println("👹 Goblin used Critical Strike!");
target.takeDamage(criticalDamage);
} }
else else
{ {
target.takeDamage(baseDamage);
System.out.println("👹 Goblin used Slash!"); System.out.println("👹 Goblin used Slash!");
target.takeDamage(baseDamage);
} }
} }
@Override
public int getXpReward()
{
return 25;
}
} }
@@ -3,6 +3,8 @@ package org.project.entity.enemies;
import org.project.entity.Entity; import org.project.entity.Entity;
import org.project.item.weapons.Weapon; import org.project.item.weapons.Weapon;
import static org.project.entity.players.Player.BLUE;
public class Skeleton extends Enemy{ public class Skeleton extends Enemy{
private boolean hasResurrected = false; private boolean hasResurrected = false;
@@ -10,8 +12,8 @@ public class Skeleton extends Enemy{
public Skeleton(Weapon weapon) public Skeleton(Weapon weapon)
{ {
super(40, 0, weapon); super(60, 0, weapon);
this.resurrectionThreshold = 20; this.resurrectionThreshold = 30;
} }
@Override @Override
@@ -29,6 +31,7 @@ public class Skeleton extends Enemy{
public void attack(Entity target) public void attack(Entity target)
{ {
if (!this.isAlive()) return; if (!this.isAlive()) return;
System.out.println(BLUE+"-- Skeleton's Turn --"+RESET);
int damage = 0; int damage = 0;
if (super.getWeapon() != null) if (super.getWeapon() != null)
{ {
@@ -44,10 +47,17 @@ public class Skeleton extends Enemy{
public void resurrect() public void resurrect()
{ {
int healAmount = super.getMaxHP() / 2; this.isAlive = true;
super.heal(healAmount);
this.hasResurrected = true; this.hasResurrected = true;
System.out.println("👻 Skeleton is rising from the grave! HP restored to " + super.getHp()); int currentMaxHP = (this instanceof Enemy) ? ((Enemy) this).getMaxHP() : 60;
this.setHp(currentMaxHP / 2);
System.out.println("👻 Skeleton is rising from the grave! HP restored to " + this.getHp());
}
@Override
public int getXpReward()
{
return 40;
} }
} }
@@ -3,19 +3,21 @@ package org.project.entity.enemies;
import org.project.entity.Entity; import org.project.entity.Entity;
import org.project.item.weapons.Weapon; import org.project.item.weapons.Weapon;
import static org.project.entity.players.Player.BLUE;
public class Vampire extends Enemy{ public class Vampire extends Enemy{
private static final double lifeStealPercent = 0.3; private static final double lifeStealPercent = 0.3;
public Vampire(Weapon weapon) public Vampire(Weapon weapon)
{ {
super(60, 0, weapon); super(80, 0, weapon);
} }
@Override @Override
public void attack(Entity target) { public void attack(Entity target) {
if (!this.isAlive()) return; if (!this.isAlive()) return;
System.out.println(BLUE+"-- Vampire's Turn --"+RESET);
int damage = 0; int damage = 0;
if (super.getWeapon() != null) { if (super.getWeapon() != null) {
damage = super.getWeapon().getDamage(); damage = super.getWeapon().getDamage();
@@ -30,12 +32,19 @@ public class Vampire extends Enemy{
int healAmount = (int) (damage * lifeStealPercent); int healAmount = (int) (damage * lifeStealPercent);
if (healAmount > 0) if (healAmount > 0)
{ {
System.out.println("🦇 Vampire used Life Drain!");
super.heal(healAmount); super.heal(healAmount);
System.out.println("🦇 Vampire used Life Drain and drained " + healAmount + " HP!");
} }
else else
{ {
System.out.println("🦇 Vampire used Bite!"); System.out.println("🦇 Vampire used Bite!");
} }
} }
@Override
public int getXpReward()
{
return 60;
}
} }
@@ -23,6 +23,7 @@ public class Assassin extends Player {
@Override @Override
public void specialAbility(Entity target) public void specialAbility(Entity target)
{ {
isDefending = false;
if (this.getMp() < costSpecial) if (this.getMp() < costSpecial)
{ {
System.out.println(YELLOW+"Not enough Mana for Vanish!"+RESET); System.out.println(YELLOW+"Not enough Mana for Vanish!"+RESET);
@@ -37,7 +38,9 @@ public class Assassin extends Player {
} }
@Override @Override
public void heavyAttack(Entity target) { public void heavyAttack(Entity target)
{
isDefending = false;
int baseDmg = getBaseDamage() * 2; int baseDmg = getBaseDamage() * 2;
int weaponDmg = (weapon != null) ? weapon.getDamage() : 0; int weaponDmg = (weapon != null) ? weapon.getDamage() : 0;
int totalDmg = baseDmg + weaponDmg; int totalDmg = baseDmg + weaponDmg;
@@ -54,14 +57,15 @@ public class Assassin extends Player {
} }
this.setMp(this.getMp() - costHeavy); this.setMp(this.getMp() - costHeavy);
target.takeDamage(totalDmg);
System.out.println("🔪 " + name + " (Assassin) used Heavy Attack! " + "( " + BLUE + costHeavy + RESET + " Mana)"); System.out.println("🔪 " + name + " (Assassin) used Heavy Attack! " + "( " + BLUE + costHeavy + RESET + " Mana)");
System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+totalDmg+RESET + " damage!"); System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+totalDmg+RESET + " damage!");
target.takeDamage(totalDmg);
} }
@Override @Override
public void lightAttack(Entity target) public void lightAttack(Entity target)
{ {
isDefending = false;
int baseDmg = getBaseDamage(); int baseDmg = getBaseDamage();
int weaponDmg = (weapon != null) ? weapon.getDamage() : 0; int weaponDmg = (weapon != null) ? weapon.getDamage() : 0;
int totalDmg = baseDmg + weaponDmg; int totalDmg = baseDmg + weaponDmg;
@@ -71,19 +75,28 @@ public class Assassin extends Player {
setNextAttackCritical(false); setNextAttackCritical(false);
} }
target.takeDamage(totalDmg);
System.out.println("️🗡️ " + name + " (Assassin) used Light Attack! " + "( " + BLUE+costLight+RESET + " Mana)"); System.out.println("️🗡️ " + name + " (Assassin) used Light Attack! " + "( " + BLUE+costLight+RESET + " Mana)");
System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+totalDmg+RESET + " damage!"); System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+totalDmg+RESET + " damage!");
target.takeDamage(totalDmg);
} }
@Override @Override
public void heal(int health) public void heal(int health)
{ {
setHp(getHp() + health); isDefending = false;
if (this.getHp() > getMaxHP()) { if (this.getMp() >= costHeal)
setHp(getMaxHP()); {
setHp(getHp() + health);
if (this.getHp() > getMaxHP()) {
setHp(getMaxHP());
}
this.setMp(getMp() - costHeal);
System.out.println("💚" + name + " (Assassin) healed " + health + " HP!" + " ( " + BLUE+costHeal+RESET + " Mana)");
}
else
{
System.out.println(YELLOW+"Not enough Mana!"+RESET);
} }
System.out.println("💚" + name + " (Assassin) healed " + health + " HP!" );
} }
@Override @Override
public void defend() public void defend()
@@ -93,6 +106,7 @@ public class Assassin extends Player {
this.setMp(getMp() - costDefend); this.setMp(getMp() - costDefend);
this.setDefending(true); this.setDefending(true);
System.out.println("🛡️" + name + " (Assassin) is defending!" + "( " + BLUE+costDefend+RESET + " Mana)"); System.out.println("🛡️" + name + " (Assassin) is defending!" + "( " + BLUE+costDefend+RESET + " Mana)");
isDefending = true;
} }
else else
{ {
@@ -24,6 +24,7 @@ public class Knight extends Player{
@Override @Override
public void specialAbility(Entity target) public void specialAbility(Entity target)
{ {
isDefending = false;
if (this.getMp() < costSpecial) if (this.getMp() < costSpecial)
{ {
System.out.println(YELLOW+"Not enough Mana for shield bash!"+RESET); System.out.println(YELLOW+"Not enough Mana for shield bash!"+RESET);
@@ -35,16 +36,16 @@ public class Knight extends Player{
Enemy enemyTarget = (Enemy) target; Enemy enemyTarget = (Enemy) target;
enemyTarget.setStunned(true); enemyTarget.setStunned(true);
System.out.println("🛡️ " + PURPLE+getName() + " used Shield Bash!" +RESET+ "( " + BLUE+costSpecial+RESET + " Mana)"); System.out.println("🛡️ " + PURPLE+getName() + " used Shield Bash!" +RESET+ "( " + BLUE+costSpecial+RESET + " Mana)");
System.out.println("Enemy is Stunned and will skip their next turn!");
} }
int damage = getBaseDamage() + (weapon != null ? weapon.getDamage() : 0) + 10; int damage = getBaseDamage() + (weapon != null ? weapon.getDamage() : 0) + 10;
target.takeDamage(damage);
System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+damage+RESET + " damage!"); System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+damage+RESET + " damage!");
target.takeDamage(damage);
} }
public void heavyAttack(Entity target) public void heavyAttack(Entity target)
{ {
isDefending = false;
int baseDmg = getBaseDamage() * 2; int baseDmg = getBaseDamage() * 2;
int weaponDmg = (weapon != null) ? weapon.getDamage() : 0; int weaponDmg = (weapon != null) ? weapon.getDamage() : 0;
int totalDmg = baseDmg + weaponDmg; int totalDmg = baseDmg + weaponDmg;
@@ -56,29 +57,39 @@ public class Knight extends Player{
} }
this.setMp(this.getMp() - costHeavy); this.setMp(this.getMp() - costHeavy);
target.takeDamage(totalDmg);
System.out.println("💥 " + name + " (Knight) used Heavy Attack! " + "( " + BLUE+costHeavy+RESET + " Mana)"); System.out.println("💥 " + name + " (Knight) used Heavy Attack! " + "( " + BLUE+costHeavy+RESET + " Mana)");
System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+totalDmg+RESET + " damage!"); System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+totalDmg+RESET + " damage!");
target.takeDamage(totalDmg);
} }
public void lightAttack(Entity target) public void lightAttack(Entity target)
{ {
isDefending = false;
int baseDmg = getBaseDamage(); int baseDmg = getBaseDamage();
int weaponDmg = (weapon != null) ? weapon.getDamage() : 0; int weaponDmg = (weapon != null) ? weapon.getDamage() : 0;
int totalDmg = baseDmg + weaponDmg; int totalDmg = baseDmg + weaponDmg;
target.takeDamage(totalDmg);
System.out.println("⚔️ " + name + " (Knight) used Light Attack! " + "( " + BLUE+costLight+RESET + " Mana)"); System.out.println("⚔️ " + name + " (Knight) used Light Attack! " + "( " + BLUE+costLight+RESET + " Mana)");
System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+totalDmg+RESET + " damage!"); System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+totalDmg+RESET + " damage!");
target.takeDamage(totalDmg);
} }
@Override @Override
public void heal(int health) public void heal(int health)
{ {
setHp(getHp() + health); isDefending = false;
if (this.getHp() > getMaxHP()) { if (this.getMp() >= costHeal)
setHp(getMaxHP()); {
setHp(getHp() + health);
if (this.getHp() > getMaxHP()) {
setHp(getMaxHP());
}
this.setMp(getMp() - costHeal);
System.out.println("💚 " + name + " (Knight) healed " + health + " HP!" + " ( " + BLUE+costHeal+RESET + " Mana)");
}
else
{
System.out.println(YELLOW+"Not enough Mana!"+RESET);
} }
System.out.println("💚" + name + " (Knight) healed " + health + " HP!" );
} }
@Override @Override
public void defend() public void defend()
@@ -88,10 +99,12 @@ public class Knight extends Player{
this.setMp(getMp() - costDefend); this.setMp(getMp() - costDefend);
this.setDefending(true); this.setDefending(true);
System.out.println("🛡️" + name + " (Knight) is defending!" + "( " + BLUE+costDefend+RESET + " Mana)"); System.out.println("🛡️" + name + " (Knight) is defending!" + "( " + BLUE+costDefend+RESET + " Mana)");
isDefending = true;
} }
else else
{ {
System.out.println(YELLOW+"Not enough Mana!"+RESET); System.out.println(YELLOW+"Not enough Mana!"+RESET);
} }
} }
} }
@@ -21,7 +21,7 @@ public abstract class Player implements Entity {
protected static final int costDefend = 15; protected static final int costDefend = 15;
protected static final int costHeal = 30; protected static final int costHeal = 30;
protected static final int costSpecial = 50; protected static final int costSpecial = 50;
private boolean isDefending = false; protected boolean isDefending = false;
public static final String BLUE = "\u001B[34m"; public static final String BLUE = "\u001B[34m";
public static final String RED = "\033[31m"; public static final String RED = "\033[31m";
@@ -31,6 +31,8 @@ public abstract class Player implements Entity {
public Player(String name, int maxHP, int maxMP, Weapon weapon, Armor armor) { public Player(String name, int maxHP, int maxMP, Weapon weapon, Armor armor) {
this.name = name; this.name = name;
this.maxHP = maxHP;
this.maxMP = maxMP;
this.hp = maxHP; this.hp = maxHP;
this.mp = maxMP; this.mp = maxMP;
this.weapon = weapon; this.weapon = weapon;
@@ -38,19 +40,39 @@ public abstract class Player implements Entity {
} }
@Override @Override
public void takeDamage(int damage) { public void takeDamage(int rawDamage) {
int actualDamage = rawDamage;
String logMessage = "";
if (isDefending) if (isDefending)
{ {
damage = (int) (damage * 0.1); int defenseReduction = (int) (actualDamage * 0.5);
actualDamage -= defenseReduction;
isDefending = false; isDefending = false;
logMessage += "🛡️ You braced for impact! (-50% damage) ";
} }
int actualDamage = Math.max(0, damage - (armor != null ? armor.getDefense() : 0));
hp -= actualDamage; int armorDefense = 0;
if (armor != null)
{
armorDefense = armor.getDefense();
int damageAfterArmor = Math.max(0, actualDamage - armorDefense);
actualDamage = damageAfterArmor;
if (armorDefense > 0)
{
logMessage += "🛡️ " + armor.getName() +" blocked "+ armorDefense + " damage! ";
}
}
this.hp -= actualDamage;
if (this.hp < 0) if (this.hp < 0)
{ {
this.hp = 0; this.hp = 0;
} }
System.out.println("💥 " + name + " took " + RED+damage+RESET + " damage!");
System.out.println(logMessage);
System.out.println("💥 " + name + " took " + RED + actualDamage + RESET + " damage!");
System.out.println("❤️ " + name + " HP remaining: " + hp + "/" + maxHP); System.out.println("❤️ " + name + " HP remaining: " + hp + "/" + maxHP);
} }
@@ -21,6 +21,7 @@ public class Wizard extends Player{
@Override @Override
public void specialAbility(Entity target) public void specialAbility(Entity target)
{ {
isDefending = false;
if (this.getMp() < costSpecial) if (this.getMp() < costSpecial)
{ {
System.out.println(YELLOW+"Not enough Mana for Soul Siphon!"+RESET); System.out.println(YELLOW+"Not enough Mana for Soul Siphon!"+RESET);
@@ -28,17 +29,18 @@ public class Wizard extends Player{
} }
this.setMp(this.getMp() - costSpecial); this.setMp(this.getMp() - costSpecial);
int damage = getBaseDamage() + (weapon != null ? weapon.getDamage() : 0) + 25; int damage = getBaseDamage() + (weapon != null ? weapon.getDamage() : 0) + 25;
target.takeDamage(damage);
int selfHeal = 15; int selfHeal = 15;
System.out.println("🪄 " +PURPLE+ name + " (Wizard) used soul siphon!"+RESET + " (" + BLUE+costSpecial+RESET + " Mana)"); System.out.println("🪄 " +PURPLE+ name + " (Wizard) used soul siphon!"+RESET + " (" + BLUE+costSpecial+RESET + " Mana)");
System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+damage+RESET + " damage!"); System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+damage+RESET + " damage!");
target.takeDamage(damage);
heal(selfHeal); heal(selfHeal);
} }
@Override @Override
public void heavyAttack(Entity target) public void heavyAttack(Entity target)
{ {
isDefending = false;
int baseDmg = getBaseDamage() * 2; int baseDmg = getBaseDamage() * 2;
int weaponDmg = (weapon != null) ? weapon.getDamage() : 0; int weaponDmg = (weapon != null) ? weapon.getDamage() : 0;
int totalDmg = baseDmg + weaponDmg; int totalDmg = baseDmg + weaponDmg;
@@ -50,30 +52,40 @@ public class Wizard extends Player{
} }
this.setMp(this.getMp() - costHeavy); this.setMp(this.getMp() - costHeavy);
target.takeDamage(totalDmg);
System.out.println("💥 " + name + " (Wizard) used Heavy Attack! " + "( " + BLUE+costHeavy+RESET + " Mana)"); System.out.println("💥 " + name + " (Wizard) used Heavy Attack! " + "( " + BLUE+costHeavy+RESET + " Mana)");
System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+totalDmg+RESET + " fire damage!"); System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+totalDmg+RESET + " fire damage!");
target.takeDamage(totalDmg);
} }
@Override @Override
public void lightAttack(Entity target) public void lightAttack(Entity target)
{ {
isDefending = false;
int baseDmg = getBaseDamage(); int baseDmg = getBaseDamage();
int weaponDmg = (weapon != null) ? weapon.getDamage() : 0; int weaponDmg = (weapon != null) ? weapon.getDamage() : 0;
int totalDmg = baseDmg + weaponDmg; int totalDmg = baseDmg + weaponDmg;
target.takeDamage(totalDmg);
System.out.println("️🧙 " + name + " (Wizard) used Light Attack! " + "( " + BLUE+costLight+RESET + " Mana)"); System.out.println("️🧙 " + name + " (Wizard) used Light Attack! " + "( " + BLUE+costLight+RESET + " Mana)");
System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+totalDmg+RESET + " magical damage!"); System.out.println("💥 " + target.getClass().getSimpleName() + " took " + RED+totalDmg+RESET + " magical damage!");
target.takeDamage(totalDmg);
} }
@Override @Override
public void heal(int health) public void heal(int health)
{ {
setHp(getHp() + health); isDefending = false;
if (this.getHp() > getMaxHP()) { if (this.getMp() >= costHeal)
setHp(getMaxHP()); {
setHp(getHp() + health);
if (this.getHp() > getMaxHP()) {
setHp(getMaxHP());
}
this.setMp(getMp() - costHeal);
System.out.println("💚 " + name + " (Wizard) healed " + health + " HP!" + " ( " + BLUE+costHeal+RESET + " Mana)");
}
else
{
System.out.println(YELLOW+"Not enough Mana!"+RESET);
} }
System.out.println("💚" + name + " (Wizard) healed " + health + " HP!" );
} }
@Override @Override
public void defend() public void defend()
@@ -83,6 +95,7 @@ public class Wizard extends Player{
this.setMp(getMp() - costDefend); this.setMp(getMp() - costDefend);
this.setDefending(true); this.setDefending(true);
System.out.println("🛡️" + name + " (Wizard) is defending!" + "( " + BLUE+costDefend+RESET + " Mana)"); System.out.println("🛡️" + name + " (Wizard) is defending!" + "( " + BLUE+costDefend+RESET + " Mana)");
isDefending = true;
} }
else else
{ {
@@ -1,59 +0,0 @@
package org.project.entity.players;
import java.util.Scanner;
public class game {
private Player player;
private Scanner scanner;
public game()
{
this.scanner = new Scanner(System.in);
this.player = createPlayer();
this.location
}
public Player createPlayer()
{
System.out.println("🏰 Welcome to Java Knight!");
System.out.println("Enter your name: ");
String name = scanner.nextLine();
System.out.println("1. ⚔️ Knight (High Damage, Balanced HP)");
System.out.println("2. 🧙 Wizard (High HP, Magic Power)");
System.out.println("3. 🗡️ Assassin (High Mana, Critical Hits)");
int choice = getIntInput("Choose your character: ");
Player newPlayer = null;
switch (choice)
{
case 1:
System.out.println("You chose the Knight!");
newPlayer = new Knight(name);
break;
case 2:
System.out.println("You chose the Wizard!");
newPlayer = new Wizard(name);
break;
case 3:
System.out.println("You chose the Assassin!");
newPlayer = new Assassin(name);
default:
System.out.println("Invalid choice. Defaulting to Knight.");
newPlayer = new Knight(name);
break;
}
}
private int getIntInput(String prompt) {
System.out.print(prompt);
while (!scanner.hasNextInt()) {
System.out.println("⚠️ Please enter a valid number!");
scanner.next();
System.out.print(prompt);
}
return scanner.nextInt();
}
}
@@ -6,7 +6,7 @@ public class KnightArmor extends Armor{
public KnightArmor() public KnightArmor()
{ {
super(1, "Plate Armor", 15, 100); super(1, "Plate Armor", 5, 100);
} }
@Override @Override
@@ -6,7 +6,7 @@ public class LeatherArmor extends Armor{
public LeatherArmor() public LeatherArmor()
{ {
super(3, "Light Leather", 8, 70); super(3, "Light Leather", 3, 70);
} }
@Override @Override
@@ -5,7 +5,7 @@ import org.project.entity.Entity;
public class Robe extends Armor{ public class Robe extends Armor{
public Robe() public Robe()
{ {
super(2, "magic robe", 5, 50); super(2, "magic robe", 1, 50);
} }
@Override @Override
@@ -9,9 +9,10 @@ public class Location {
private ArrayList<Location> locations; private ArrayList<Location> locations;
private ArrayList<Enemy> enemies; private ArrayList<Enemy> enemies;
public Location(ArrayList<Location> locations, ArrayList<Enemy> enemies) { public Location(String name, ArrayList<Location> locations, ArrayList<Enemy> enemies) {
this.locations = locations; this.locations = locations;
this.enemies = enemies; this.enemies = enemies;
this.name = name;
} }
public String getName() { public String getName() {
Binary file not shown.
Binary file not shown.