Merge pull request 'develop' (#1) from develop into main

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2026-05-21 21:02:55 +00:00
57 changed files with 1255 additions and 316 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://mirror-maven.runflare.com/maven2" />
</remote-repository>
</component> </component>
</project> </project>
+3
View File
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="MarkdownSettings"> <component name="MarkdownSettings">
<option name="previewPanelProviderInfo">
<ProviderInfo name="Compose (experimental)" className="com.intellij.markdown.compose.preview.ComposePanelProvider" />
</option>
<option name="showProblemsInCodeBlocks" value="false" /> <option name="showProblemsInCodeBlocks" value="false" />
</component> </component>
</project> </project>
@@ -0,0 +1,274 @@
package org.project;
import org.project.entity.players.Player;
import org.project.entity.players.Knight;
import org.project.entity.players.Wizard;
import org.project.entity.players.Assassin;
import org.project.entity.enemies.Enemy;
import org.project.entity.enemies.Goblin;
import org.project.entity.enemies.Skeleton;
import org.project.entity.enemies.Vampire;
import org.project.entity.enemies.Dragon;
import org.project.item.Item;
import org.project.location.Location;
import java.util.*;
public class GameEngine {
private Player player;
private Location currentLocation;
private Location castle;
private Scanner scanner;
private Map<Location, Enemy[]> possibleEnemiesMap = new HashMap<>();
private Enemy currentEnemy;
public GameEngine() {
scanner = new Scanner(System.in);
setupGame();
}
private void setupGame() {
System.out.println("Choose your class:");
System.out.println("1. Knight");
System.out.println("2. Mage");
System.out.println("3. Assassin");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
player = new Knight("Hero");
break;
case 2:
player = new Wizard("Hero");
break;
case 3:
player = new Assassin("Hero");
break;
default:
System.out.println("Invalid choice, defaulting to Knight.");
player = new Knight("Hero");
}
Location village = new Location("Village");
Location forest = new Location("Forest");
Location dungeon = new Location("Dungeon");
Location graveyard = new Location("Graveyard");
castle = new Location("Castle");
village.connectLocation(forest);
forest.connectLocation(village);
forest.connectLocation(dungeon);
dungeon.connectLocation(forest);
forest.connectLocation(graveyard);
graveyard.connectLocation(forest);
village.connectLocation(castle);
castle.connectLocation(village);
currentLocation = village;
possibleEnemiesMap.put(forest, new Enemy[] { new Goblin() });
possibleEnemiesMap.put(dungeon, new Enemy[] { new Skeleton(), new Vampire() });
possibleEnemiesMap.put(graveyard, new Enemy[] { new Skeleton(), new Vampire() });
possibleEnemiesMap.put(castle, new Enemy[] { new Dragon() });
spawnEnemyForLocation(currentLocation);
}
private void spawnEnemyForLocation(Location loc) {
Enemy[] possibilities = possibleEnemiesMap.get(loc);
if (possibilities == null || possibilities.length == 0) {
currentEnemy = null;
return;
}
Random rand = new Random();
Enemy template = possibilities[rand.nextInt(possibilities.length)];
currentEnemy = createEnemyInstance(template);
}
private Enemy createEnemyInstance(Enemy e) {
if (e instanceof Goblin) return new Goblin();
if (e instanceof Skeleton) return new Skeleton();
if (e instanceof Vampire) return new Vampire();
if (e instanceof Dragon) return new Dragon();
return null;
}
public void startGame() {
System.out.println("=== RPG Game Started ===");
boolean running = true;
while (running) {
System.out.println("\n===== MENU =====");
System.out.println("Location: " + currentLocation.getName());
System.out.println("HP: " + player.getHp());
System.out.println("Mana: " + player.getMana());
System.out.println("Level: " + player.getLevel());
System.out.println("XP: " + player.getXp());
System.out.println("Keys: " + player.getKeyCount());
System.out.println("\n1. Move");
System.out.println("2. Fight");
System.out.println("3. Show Inventory");
System.out.println("4. Exit");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
move();
break;
case 2:
fight();
break;
case 3:
player.getInventory().showInventory();
break;
case 4:
running = false;
System.out.println("Game Over.");
break;
default:
System.out.println("Invalid choice.");
}
if (!player.isAlive()) {
System.out.println("\nYou died. Game Over.");
running = false;
}
}
}
private void move() {
if (currentLocation.getConnectedLocations().isEmpty()) {
System.out.println("No connected locations.");
return;
}
System.out.println("\nWhere do you want to go?");
for (int i = 0; i < currentLocation.getConnectedLocations().size(); i++) {
System.out.println((i + 1) + ". " + currentLocation.getConnectedLocations().get(i).getName());
}
int choice = scanner.nextInt();
if (choice < 1 || choice > currentLocation.getConnectedLocations().size()) {
System.out.println("Invalid choice.");
return;
}
Location nextLocation = currentLocation.getConnectedLocations().get(choice - 1);
if (nextLocation == castle && player.getKeyCount() < 3) {
System.out.println("\nThe castle gate is locked!");
System.out.println("You need 3 keys.");
return;
}
currentLocation = nextLocation;
spawnEnemyForLocation(currentLocation);
System.out.println("\nYou moved to " + currentLocation.getName());
if (currentLocation == castle) {
System.out.println("\nThe final battle awaits...");
}
}
private void fight() {
if (currentEnemy == null || !currentEnemy.isAlive()) {
System.out.println("No enemies here.");
return;
}
Enemy enemy = currentEnemy;
System.out.println("\nA wild " + enemy.getName() + " appeared!");
while (player.isAlive() && enemy.isAlive()) {
System.out.println("\n===== COMBAT =====");
System.out.println(player.getName() + " HP: " + player.getHp());
System.out.println(enemy.getName() + " HP: " + enemy.getHp());
System.out.println("\nChoose action:");
System.out.println("1. Light Attack");
System.out.println("2. Heavy Attack");
System.out.println("3. Defend");
System.out.println("4. Heal");
System.out.println("5. Special Ability");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
player.lightAttack(enemy);
break;
case 2:
player.heavyAttack(enemy);
break;
case 3:
player.defend();
break;
case 4:
player.heal();
break;
case 5:
player.specialAbility(enemy);
break;
default:
System.out.println("Invalid choice.");
continue;
}
if (enemy.isAlive()) {
System.out.println("\n" + enemy.getName() + "'s turn!");
enemy.attack(player);
}
}
if (!enemy.isAlive()) {
System.out.println("\n" + enemy.getName() + " was defeated!");
player.gainXP(enemy.getXpReward());
Item loot = enemy.dropLoot();
if (loot != null) {
player.getInventory().addItem(loot);
System.out.println(enemy.getName() + " dropped: " + loot.getName());
}
currentEnemy = null;
if (enemy instanceof Dragon) {
System.out.println("\n====================");
System.out.println("YOU DEFEATED THE DRAGON!");
System.out.println("====== YOU WIN ======");
System.exit(0);
}
}
}
}
@@ -0,0 +1,46 @@
package org.project.inventory;
import org.project.item.Item;
import java.util.ArrayList;
public class Inventory {
private ArrayList<Item> items;
public Inventory() {
items = new ArrayList<>();
}
public void addItem(Item item) {
items.add(item);
System.out.println(item.getName() + " added to inventory.");
}
public void removeItem(Item item) {
items.remove(item);
}
public ArrayList<Item> getItems() {
return items;
}
public void showInventory() {
if(items.isEmpty()) {
System.out.println("Inventory is empty.");
return;
}
System.out.println("=== Inventory ===");
for(int i = 0; i < items.size(); i++) {
System.out.println(
(i + 1) + ". " + items.get(i).getName()
);
}
}
}
@@ -1,15 +1,10 @@
package org.project; package org.project;
import org.project.location.Location; import org.project.GameEngine;
import java.util.ArrayList;
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 GameEngine game = new GameEngine();
List<Location> locations = new ArrayList<>(); game.startGame();
// TODO: IMPLEMENT GAMEPLAY
} }
} }
@@ -1,21 +1,128 @@
package org.project.entity; package org.project.entity;
public interface Entity { import org.project.item.weapons.Weapon;
void attack(Entity target);
void defend(); public abstract class Entity {
void heal(int health); protected String name;
protected int hp;
protected int maxHp;
void fillMana(int mana); protected int mana;
protected int maxMana;
void takeDamage(int damage); protected Weapon weapon;
int getMaxHP(); protected boolean isDefending;
int getMaxMP(); public Entity(String name, int hp, int mana, Weapon weapon) {
/* this.name = name;
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/ this.hp = hp;
this.maxHp = hp;
this.mana = mana;
this.maxMana = mana;
this.weapon = weapon;
this.isDefending = false;
}
public boolean useMana(int amount) {
if (mana >= amount) {
mana -= amount;
return true;
}
return false;
}
public void takeDamage(int damage) {
if (isDefending) {
damage /= 2;
}
hp -= damage;
if (hp < 0) {
hp = 0;
}
System.out.println(name + " took " + damage + " damage.");
}
public void heal(int amount) {
hp += amount;
if (hp > maxHp) {
hp = maxHp;
}
System.out.println(name + " healed for " + amount + " HP.");
}
public boolean isAlive() {
return hp > 0;
}
public void defend() {
isDefending = true;
System.out.println(name + " is defending.");
}
public void resetDefending() {
isDefending = false;
}
public String getName() {
return name;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getMaxHp() {
return maxHp;
}
public int getMana() {
return mana;
}
public void setMana(int mana) {
this.mana = mana;
}
public int getMaxMana() {
return maxMana;
}
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public boolean isDefending() {
return isDefending;
}
public void setDefending(boolean defending) {
isDefending = defending;
}
} }
@@ -0,0 +1,24 @@
package org.project.entity.enemies;
import org.project.entity.Entity;
import org.project.item.weapons.Staff;
public class Dragon extends Enemy {
public Dragon() {
super("Dragon", 300, 100, new Staff() , 150);
}
@Override
public void specialAbility(Entity target) {
int damage = getWeapon().getDamage() * 3;
target.takeDamage(damage);
System.out.println(getName() + " uses Breath Fire!");
System.out.println(target.getName() + " takes " + damage + " fire damage.");
}
}
@@ -1,34 +1,69 @@
package org.project.entity.enemies; package org.project.entity.enemies;
import org.project.entity.Entity;
import org.project.item.Item;
import org.project.item.Key;
import org.project.item.consumables.Flask;
import org.project.item.weapons.Weapon; import org.project.item.weapons.Weapon;
// TODO: UPDATE IMPLEMENTATION import java.util.Random;
public abstract class Enemy {
Weapon weapon;
private int hp;
private int mp;
public Enemy(int hp, int mp, Weapon weapon) { public abstract class Enemy extends Entity {
this.hp = hp;
this.mp = mp;
this.weapon = weapon; protected int xpReward;
public Enemy(String name, int hp, int mana, Weapon weapon, int xpReward) {
super(name, hp, mana, weapon);
this.xpReward = xpReward;
} }
@Override public int getXpReward() {
public void takeDamage(int damage) { return xpReward;
hp -= damage;
} }
public int getHp() { public void attack(Entity target) {
return hp;
int damage = 0;
if (weapon != null) {
damage = weapon.getDamage();
} }
public int getMp() { target.takeDamage(damage);
return mp;
System.out.println(name + " attacks " + target.getName());
target.resetDefending();
} }
public Weapon getWeapon() { public void defend() {
return weapon;
setDefending(true);
System.out.println(name + " is defending.");
} }
public Item dropLoot() {
Random random = new Random();
int chance = random.nextInt(100);
if (chance < 30) {
return new Flask();
}
if (chance >= 30 && chance < 40) {
return new Key("Castle Key");
}
return null;
}
public abstract void specialAbility(Entity target);
} }
@@ -0,0 +1,22 @@
package org.project.entity.enemies;
import org.project.entity.Entity;
import org.project.item.weapons.Dagger;
public class Goblin extends Enemy {
public Goblin() {
super("Goblin", 80, 0, new Dagger() , 20);
}
@Override
public void specialAbility(Entity target) {
int damage = getWeapon().getDamage() + 5;
target.takeDamage(damage);
System.out.println("Goblin uses Sneak Attack!");
System.out.println(target.getName() + " takes " + damage + " damage.");
}
}
@@ -1,6 +1,18 @@
package org.project.entity.enemies; package org.project.entity.enemies;
// TODO: UPDATE IMPLEMENTATION import org.project.entity.Entity;
public class Skeleton { import org.project.item.weapons.Sword;
// TODO: DESIGN ENEMY AND IMPLEMENT THE CONSTRUCTOR
public class Skeleton extends Enemy {
public Skeleton() {
super("Skeleton", 100, 0, new Sword() , 30);
}
@Override
public void specialAbility(Entity target) {
defend();
System.out.println("Skeleton raises its shield.");
}
} }
@@ -0,0 +1,24 @@
package org.project.entity.enemies;
import org.project.entity.Entity;
import org.project.item.weapons.Dagger;
public class Vampire extends Enemy {
public Vampire() {
super("Vampire", 120, 50, new Dagger() , 50);
}
@Override
public void specialAbility(Entity target) {
int damage = getWeapon().getDamage() * 2;
target.takeDamage(damage);
setHp(getHp() + damage / 2);
System.out.println("Vampire uses Life Drain!");
System.out.println(target.getName() + " takes " + damage + " damage.");
}
}
@@ -0,0 +1,76 @@
package org.project.entity.players;
import org.project.entity.Entity;
import org.project.item.armors.AssassinArmor;
import org.project.item.weapons.Dagger;
public class Assassin extends Player {
public Assassin(String name) {
super(name, 120, 80, new Dagger());
}
@Override
public void heavyAttack(Entity target) {
int manaCost = 15;
if (getMana() < manaCost) {
System.out.println("Not enough mana!");
return;
}
useMana(manaCost);
int damage = weapon.getDamage() * 2;
if (target.isDefending()) {
damage /= 2;
}
target.takeDamage(damage);
System.out.println(name + " performs a deadly strike!");
System.out.println(target.getName() + " takes " + damage + " damage.");
}
@Override
public void heal() {
int healAmount = 20;
int manaGain = 20;
setHp(Math.min(getHp() + healAmount, getMaxHp()));
setMana(Math.min(getMana() + manaGain, getMaxMana()));
System.out.println(name + " regains " + healAmount + " HP and " + manaGain + " mana.");
}
@Override
public void specialAbility(Entity target) {
int manaCost = 30;
if (getMana() < manaCost) {
System.out.println("Not enough mana!");
return;
}
useMana(manaCost);
int damage = weapon.getDamage() * 3;
if (target.isDefending()) {
damage /= 2;
}
target.takeDamage(damage);
System.out.println(name + " uses Shadow Strike!");
System.out.println(target.getName() + " takes " + damage + " damage.");
}
}
@@ -0,0 +1,16 @@
package org.project.entity.players;
import org.project.entity.Entity;
public interface CombatActions {
void lightAttack(Entity target);
void heavyAttack(Entity target);
void defend();
void heal();
void specialAbility(Entity target);
}
@@ -1,6 +1,54 @@
package org.project.entity.players; package org.project.entity.players;
// TODO: UPDATE IMPLEMENTATION import org.project.entity.Entity;
public class Knight { import org.project.item.armors.KnightArmor;
// TODO: DESIGN KNIGHT'S WEAPON AND ARMOR AND IMPLEMENT THE CONSTRUCTOR import org.project.item.weapons.Sword;
public class Knight extends Player {
public Knight(String name) {
super(name, 150, 50, new Sword());
}
@Override
public void heavyAttack(Entity target) {
int damage = weapon.getDamage() * 2;
if (target.isDefending()) {
damage /= 2;
}
target.takeDamage(damage);
System.out.println(name + " performs a heavy sword strike!");
System.out.println(target.getName() + " takes " + damage + " damage.");
}
@Override
public void heal() {
int healAmount = 30;
setHp(Math.min(getHp() + healAmount, getMaxHp()));
System.out.println(name + " recovers " + healAmount + " HP.");
}
@Override
public void specialAbility(Entity target) {
int damage = weapon.getDamage() + 20;
if (target.isDefending()) {
damage /= 2;
}
target.takeDamage(damage);
System.out.println(name + " uses Shield Bash!");
System.out.println(target.getName() + " takes " + damage + " damage.");
}
} }
@@ -1,89 +1,138 @@
package org.project.entity.players; package org.project.entity.players;
import org.project.entity.Entity; import org.project.entity.Entity;
import org.project.item.armors.Armor; import org.project.inventory.Inventory;
import org.project.item.weapons.Weapon; import org.project.item.weapons.Weapon;
// TODO: UPDATE IMPLEMENTATION public abstract class Player extends Entity implements CombatActions {
public abstract class Player {
protected String name;
Weapon weapon;
Armor armor;
private int hp;
private int maxHP;
private int mp;
private int maxMP;
public Player(String name, int hp, int mp, Weapon weapon, Armor armor) { protected Inventory inventory;
this.name = name;
this.hp = hp;
this.mp = mp;
this.weapon = weapon; protected int level;
this.armor = armor; protected int xp;
protected int xpToNextLevel;
protected int baseDamage;
public Player(String name,
int hp,
int mana,
Weapon weapon) {
super(name, hp, mana, weapon);
this.inventory = new Inventory();
this.level = 1;
this.xp = 0;
this.xpToNextLevel = 100;
this.baseDamage = 10;
}
public Inventory getInventory() {
return inventory;
}
public int getLevel() {
return level;
}
public int getXp() {
return xp;
}
public int getBaseDamage() {
return baseDamage;
}
public void gainXP(int amount) {
xp += amount;
System.out.println(name + " gained " + amount + " XP!");
while (xp >= xpToNextLevel) {
levelUp();
}
}
private void levelUp() {
level++;
xp -= xpToNextLevel;
xpToNextLevel += 50;
maxHp += 20;
hp = maxHp;
maxMana += 10;
mana = maxMana;
baseDamage += 5;
System.out.println("\n=== LEVEL UP ===");
System.out.println(name + " reached level " + level);
System.out.println("HP increased!");
System.out.println("Mana increased!");
System.out.println("Damage increased!");
} }
@Override @Override
public void attack(Entity target) { public void lightAttack(Entity target) {
target.takeDamage(weapon.getDamage());
System.out.println(
this.name +
" uses a light attack on " +
target.getName()
);
if (weapon != null) {
weapon.use(target);
} else {
target.takeDamage(baseDamage);
System.out.println("Basic attack dealt " +
baseDamage +
" damage.");
}
target.resetDefending();
} }
@Override @Override
public void defend() { public void defend() {
// TODO
isDefending = true;
System.out.println(name + " is defending!");
}
public int getKeyCount() {
int count = 0;
for (var item : inventory.getItems()) {
if (item.getName().equals("Castle Key")) {
count++;
}
}
return count;
} }
@Override @Override
public void takeDamage(int damage) { public abstract void heavyAttack(Entity target);
hp -= damage - armor.getDefense();
}
@Override @Override
public void heal(int health) { public abstract void heal();
hp += health;
if (hp > maxHP) {
hp = maxHP;
}
}
@Override @Override
public void fillMana(int mana) { public abstract void specialAbility(Entity target);
mp += mana;
if (mp > maxMP) {
mp = maxMP;
}
}
public String getName() {
return name;
}
public int getHp() {
return hp;
}
@Override
public int getMaxHP() {
return maxHP;
}
public int getMp() {
return mp;
}
@Override
public int getMaxMP() {
return maxMP;
}
public Weapon getWeapon() {
return weapon;
}
public Armor getArmor() {
return armor;
}
} }
@@ -0,0 +1,80 @@
package org.project.entity.players;
import org.project.entity.Entity;
import org.project.item.weapons.Staff;
public class Wizard extends Player {
public Wizard(String name) {
super(name, 100, 150, new Staff());
}
@Override
public void heavyAttack(Entity target) {
int manaCost = 20;
if (getMana() < manaCost) {
System.out.println("Not enough mana!");
return;
}
useMana(manaCost);
int damage = weapon.getDamage() * 2;
if (target.isDefending()) {
damage /= 2;
}
target.takeDamage(damage);
System.out.println(name + " casts a powerful fire spell!");
System.out.println(target.getName() + " takes " + damage + " damage.");
}
@Override
public void heal() {
int manaCost = 15;
if (getMana() < manaCost) {
System.out.println("Not enough mana!");
return;
}
useMana(manaCost);
int healAmount = 40;
setHp(Math.min(getHp() + healAmount, getMaxHp()));
System.out.println(name + " casts a healing spell and restores " + healAmount + " HP.");
}
@Override
public void specialAbility(Entity target) {
int manaCost = 40;
if (getMana() < manaCost) {
System.out.println("Not enough mana!");
return;
}
useMana(manaCost);
int damage = weapon.getDamage() * 3;
if (target.isDefending()) {
damage /= 2;
}
target.takeDamage(damage);
System.out.println(name + " unleashes Lightning Storm!");
System.out.println(target.getName() + " takes " + damage + " damage.");
}
}
@@ -3,9 +3,8 @@ package org.project.item;
import org.project.entity.Entity; import org.project.entity.Entity;
public interface Item { public interface Item {
void use(Entity target);
/* String getName();
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/ void use(Entity target);
} }
@@ -0,0 +1,24 @@
package org.project.item;
import org.project.entity.Entity;
import org.project.item.Item;
public class Key implements Item {
private String name;
public Key(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public void use(Entity target) {
System.out.println("This key is used automatically to unlock the castle.");
}
}
@@ -1,7 +1,7 @@
package org.project.item.armors; package org.project.item.armors;
// TODO: UPDATE IMPLEMENTATION
public abstract class Armor { public abstract class Armor {
private int defense; private int defense;
private int maxDefense; private int maxDefense;
private int durability; private int durability;
@@ -12,16 +12,25 @@ public abstract class Armor {
public Armor(int defense, int durability) { public Armor(int defense, int durability) {
this.defense = defense; this.defense = defense;
this.durability = durability; this.durability = durability;
this.maxDefense = defense;
this.maxDurability = durability;
this.isBroke = false;
} }
public void checkBreak() { public void checkBreak() {
if (durability <= 0) { if (durability <= 0) {
durability = 0;
isBroke = true; isBroke = true;
defense = 0; defense = 0;
} }
} }
// TODO: (BONUS) UPDATE THE REPAIR METHOD public void takeDamage(int amount) {
durability -= amount;
checkBreak();
}
public void repair() { public void repair() {
isBroke = false; isBroke = false;
defense = maxDefense; defense = maxDefense;
@@ -29,6 +38,9 @@ public abstract class Armor {
} }
public int getDefense() { public int getDefense() {
if (isBroke) {
return 0;
}
return defense; return defense;
} }
@@ -0,0 +1,7 @@
package org.project.item.armors;
public class AssassinArmor extends Armor {
public AssassinArmor(){
super(5,25);
}
}
@@ -1,6 +1,9 @@
package org.project.item.armors; package org.project.item.armors;
// TODO: UPDATE IMPLEMENTATION public class KnightArmor extends Armor {
public class KnightArmor {
// TODO: DESIGN ARMOR'S ATTRIBUTES IMPLEMENT THE CONSTRUCTOR public KnightArmor() {
super(8, 40);
}
} }
@@ -0,0 +1,7 @@
package org.project.item.armors;
public class MageArmor extends Armor{
public MageArmor(){
super(2,15);
}
}
@@ -1,8 +1,17 @@
package org.project.item.consumables; package org.project.item.consumables;
// TODO: UPDATE IMPLEMENTATION import org.project.item.Item;
public abstract class Consumable {
/* public abstract class Consumable implements Item {
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/ private String name;
public Consumable(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
} }
@@ -2,15 +2,33 @@ package org.project.item.consumables;
import org.project.entity.Entity; import org.project.entity.Entity;
// TODO: UPDATE IMPLEMENTATION public class Flask extends Consumable {
public class Flask {
/* private int healAmount;
THIS IS AN EXAMPLE OF A CONSUMABLE DESIGN.
*/ public Flask() {
super("Flask");
this.healAmount = 30;
}
// TODO: UPDATE USE METHOD
@Override @Override
public void use(Entity target) { public void use(Entity target) {
target.heal(target.getMaxHP() / 10);
target.heal(healAmount);
System.out.println(
target.getName() +
" drinks a flask and restores " +
healAmount + " HP."
);
}
public int getHealAmount() {
return healAmount;
}
public void setHealAmount(int healAmount) {
this.healAmount = healAmount;
} }
} }
@@ -0,0 +1,28 @@
package org.project.item.weapons;
import org.project.entity.Entity;
public class Dagger extends Weapon {
public Dagger() {
super(10, 0);
}
@Override
public String getName() {
return "Dagger";
}
@Override
public void use(Entity target) {
int damage = getDamage();
if (target.isDefending()) {
damage /= 2;
}
target.setHp(target.getHp() - damage);
System.out.println("Dagger deals " + damage + " damage to " + target.getName());
}
}
@@ -0,0 +1,28 @@
package org.project.item.weapons;
import org.project.entity.Entity;
public class Staff extends Weapon {
public Staff() {
super(12, 5);
}
@Override
public String getName() {
return " Staff";
}
@Override
public void use(Entity target) {
int damage = getDamage();
if (target.isDefending()) {
damage /= 2;
}
target.setHp(target.getHp() - damage);
System.out.println("Staff deals " + damage + " damage to " + target.getName());
}
}
@@ -2,25 +2,27 @@ package org.project.item.weapons;
import org.project.entity.Entity; import org.project.entity.Entity;
import java.util.ArrayList; public class Sword extends Weapon {
// TODO: UPDATE IMPLEMENTATION
public class Sword {
/*
THIS IS AN EXAMPLE OF A WEAPON DESIGN.
*/
int abilityCharge;
public Sword() { public Sword() {
// TODO: DESIGN SWORD'S ATTRIBUTES IMPLEMENT THE CONSTRUCTOR super(15, 0);
} }
// TODO: (BONUS) UPDATE THE UNIQUE ABILITY @Override
public void uniqueAbility(ArrayList<Entity> targets) { public String getName() {
abilityCharge += 2; return "Sword";
for (Entity target : targets) {
target.takeDamage(getDamage());
} }
@Override
public void use(Entity target) {
int damage = getDamage();
if (target.isDefending()) {
damage /= 2;
}
target.setHp(target.getHp() - damage);
System.out.println("Sword deals " + damage + " damage to " + target.getName());
} }
} }
@@ -1,26 +1,18 @@
package org.project.item.weapons; package org.project.item.weapons;
import org.project.entity.Entity; import org.project.entity.Entity;
import org.project.item.Item;
public abstract class Weapon implements Item {
// TODO: UPDATE IMPLEMENTATION
public abstract class Weapon {
private int damage; private int damage;
private int manaCost; private int manaCost;
/*
TODO: ADD OTHER REQUIRED AND BONUS ATTRIBUTES
*/
public Weapon(int damage, int manaCost) { public Weapon(int damage, int manaCost) {
this.damage = damage; this.damage = damage;
this.manaCost = manaCost; this.manaCost = manaCost;
} }
@Override
public void use(Entity target) {
target.takeDamage(damage);
}
public int getDamage() { public int getDamage() {
return damage; return damage;
} }
@@ -29,7 +21,14 @@ public abstract class Weapon {
return manaCost; return manaCost;
} }
/* public void setDamage(int damage) {
TODO: ADD OTHER REQUIRED AND BONUS METHODS this.damage = damage;
*/ }
public void setManaCost(int manaCost) {
this.manaCost = manaCost;
}
public abstract void use(Entity target);
} }
@@ -5,24 +5,40 @@ import org.project.entity.enemies.Enemy;
import java.util.ArrayList; import java.util.ArrayList;
public class Location { public class Location {
private String name; private String name;
private ArrayList<Location> connectedLocations;
private ArrayList<Enemy> enemies; private ArrayList<Enemy> enemies;
public Location(ArrayList<Location> locations, ArrayList<Enemy> enemies) { public Location(String name) {
this.locations = locations; this.name = name;
this.enemies = enemies; this.connectedLocations = new ArrayList<>();
this.enemies = new ArrayList<>();
} }
public String getName() { public String getName() {
return name; return name;
} }
public ArrayList<Location> getLocations() { public ArrayList<Location> getConnectedLocations() {
return locations; return connectedLocations;
} }
public ArrayList<Enemy> getEnemies() { public ArrayList<Enemy> getEnemies() {
return enemies; return enemies;
} }
public void connectLocation(Location location) {
connectedLocations.add(location);
}
public void addEnemy(Enemy enemy) {
enemies.add(enemy);
}
public void removeEnemy(Enemy enemy) {
enemies.remove(enemy);
}
} }
Binary file not shown.
+125 -154
View File
@@ -1,175 +1,146 @@
# Fourth Assignment - Java Knight ⚔️ # Java Knight Game
A turn-based RPG with Roguelike elements which can be run in the terminal.
### **Prologue: The Legend of Javanest** ## Project Overview
*For centuries, the land of Javanest lived in peace, until a magical Dragon attacked, plunging the realm into absolute darkness. With a wicked curse, the Dragon transformed the innocent people into horrific monsters: Goblins, Skeletons, and Vampires. Retreating to its impenetrable Castle, the Dragon divided the three keys to its lair and hid them among these cursed creatures. Now, it is your duty to step up and save the land. You must battle these monsters, recover the unique key from each monster type, and finally slay the Dragon to break the curse and restore peace to Javanest!* Java Knight is a text-based RPG game developed using Object-Oriented Programming (OOP) principles in Java. The player selects a character class, explores different locations, fights enemies, collects items, and tries to unlock the Castle by gathering keys.
### **Introduction** The goal of the project is to demonstrate important OOP concepts such as inheritance, abstraction, polymorphism, and encapsulation while building a simple turn-based RPG system.
Welcome to **Java knight**, a turn-based RPG inspired by Roguelike games! In this assignment, you will develop a **text-based role-playing game (RPG)**. This project is designed to rigorously test your understanding of **Object-Oriented Programming (OOP) principles**.
⚠️ **REQUIREMENT:** You **must** utilize all the OOP concepts you have learned so far—including *Inheritance, Interfaces, Abstract Classes, Encapsulation, Polymorphism, Overloading, and Overriding*. It is extremely important that you use everything in its right place. Your design and architecture will be graded based on how well you apply these principles to avoid code duplication and maintain a clean structure.
🎯 **Your goal is not just to complete the assignment but to learn and apply OOP effectively!**
### **What is a Turn-Based Game?**
In this combat system, two sides - which are usually the player's side and the enemy's side - attack each other in turns. The side which is not attacking can perform actions to avoid or deflect the enemy's attack.
### **Core Mechanics:**
- **Turn-based combat** Players and monsters take turns attacking each other.
- **Character classes with Unique Traits** Players can choose from archetypes like **Knight, Assassin, or Wizard**, each starting with distinctly different base stats.
- **Unified Mana/Stamina System** All player classes use a unified resource (Mana/Stamina) to perform actions.
- **Standardized Action Set** Every player character has exactly 5 specific actions available during their turn.
- **Experience & Leveling System** Earn XP based on enemy strength to automatically level up and increase your base stats.
- **Progression System** You cannot fight the Dragon immediately. You must farm enemies for a chance to drop their specific key, collect all three, and grow stronger first.
--- ---
## Tasks 📝 ## Game Flow
### 1️⃣ Step 1: Fork & Setup 🍴 ### 1. Starting the Game
1. **Fork** this repository and clone it to your local machine. When the program starts, the player enters their name and selects a character class. The available classes are:
```bash
git clone https://git.meshcomp.ir/AdvancedProgramming1404/HW-04-JAVA-KNIGHT.git
```
2. Create a new branch named `develop` and switch to it.
```bash
git checkout -b develop
```
### 2️⃣ Step 2: Implement the Class Hierarchy 🌲
A well-structured OOP hierarchy is crucial. Avoid duplicating code by placing shared logic in abstract classes. - Knight
- Assassin
- Wizard
- **Entities & Locations:** You have `Entity`, `Item`(Bonus) , and `Location`. Each class has different attributes such as health, mana, and combat abilities.
- **Players:** `Player` is an abstract class implementing `Entity`. Subclasses: `Wizard`, `Knight`, `Assassin`.
- **Base Stat Differences:** Each class must have distinct starting stats. For example:
- **Knight:** Highest Base Damage.
- **Wizard:** Highest Max Health (HP).
- **Assassin:** Highest Max Stamina/Mana.
- **Enemies:** `Enemy` is an abstract class implementing `Entity`. Subclasses: `Skeleton`, `Goblin`, `Vampire`, and **`Dragon`**.
- **The Boss:** Even though `Dragon` is the final boss, it **must** be a subclass of `Enemy` to inherit common combat properties, while possessing extremely high stats and unique mechanics.
- **Item (Bonus):** `Consumable`, `Armor`, `Weapon` are abstract classes implementing `Item`. example :
- KnightArmor extends Armor - you can add more subclasses of Armor for extra score
- Sword extends Weapon - you can add more subclasses of Weapon for extra score
- Flask extends Consumable - you can add more subclasses of Consumable for extra score
![structure](Readme_Pictures/structure.png)
### 3️⃣ Step 3: Implement Player & Monster Methods 🏹
**Player Actions (The Rule of Five):**
Every player class **must** implement exactly the following 5 actions (You can use an interface like `ICombatActions`). Every action (except Light Attack) consumes a specific amount of Mana/Stamina. The **Special Ability** must consume the *highest* amount of Mana compared to the others.
1. **Light Attack:** Deals moderate damage and costs **NO Mana**. (Note: If the player runs out of Mana/Stamina, this is the ONLY action they can perform.)
2. **Heavy Attack:** Deals high damage, medium Mana cost.
3. **Defend:** Completely blocks or significantly reduces the damage of the enemy's *next* strike. Medium Mana cost.
4. **Heal:** Restores a portion of the player's HP. Medium-high Mana cost.
5. **Special Ability:** A unique class-based ultimate move (Highest Mana cost):
- **Wizard** 🧙‍♂️: Casts a devastating spell that damages the enemy while simultaneously replenishing some HP.
- **Assassin** 🗡️: Turns invisible, dodging the next incoming attack completely and guaranteeing a *Critical Hit* on their next turn.
- **Knight** ⚔️: Performs a shield bash that stuns the enemy, forcing them to skip their next turn while dealing heavy damage.
**Monster Abilities:**
- **Goblin** 👹: High critical hit chance but low health.
- **Skeleton** ☠️: Can resurrect once per battle with 50% HP.
- **Vampire** 🦇: Lifesteal ability a portion of the damage it deals to the player is added back to its own health.
- **Dragon (Final Boss)** 🐉: Immune to normal defense. Its fiery breath bypasses shields and deals massive damage.
🔹 Make sure each entity **prints messages** when performing actions. example output (while in combat) :
```bash
You chose to FIGHT!
[Ser Duncan - 45/45 HP | 40/40 Mana]
[Goblin - 30/30 HP]
--- ---
Your Turn: ### 2. Main Game Menu
1. Light Attack | 2. Heavy Attack (-8 Mana) | 3. Defend (-6 Mana) | 4. Heal (-12 Mana) | 5. Shield Bash (-15 Mana) After character selection, the game enters a continuous game loop where the player can choose between several actions.
```
```bash The menu options are:
→ Chose: Light Attack
⚔️ Ser Duncan (Knight) used Light Attack! (0 Mana)
Goblin took 10 damage!
Goblin has 20/30 HP remaining.
Ser Duncan Mana: 40/40
```
```
Goblin's Turn :
👹 Goblin used Critical Strike!
💥 Critical hit! Ser Duncan took 20 damage!
Ser Duncan has 25/45 HP remaining.
```
🔹 *Narrative Console:* Use ANSI escape codes to print colorful narrative logs (e.g., Red for damage, Blue for Mana usage, Green for healing).
1. Move
2. Fight
3. Show Inventory
4. Exit
### 4️⃣ Step 4: Implement the Game Loop & Progression 🎮 These options allow the player to explore the world, battle enemies, manage items, or quit the game.
1. **The Core Loop:** The game starts with the player entering a location. A random standard enemy (`Goblin`, `Skeleton`, or `Vampire`) spawns immediately.
2. **Player Choices:** Before engaging, the player is presented with the following options:
- **1. Fight the enemy:** Enter the turn-based combat sequence.
- **2. Move to another location:** Skip the current enemy and spawn a new random one.
- **3. Go to the Castle to fight the Dragon:** *(Note: This option must remain strictly hidden or locked until the player has successfully collected all 3 keys).*
3. **The Key Drop Logic (RNG Gatekeeping):**
- When the player defeats an enemy, there is a **specific percentage chance (e.g., 20%)** that it will drop the unique key associated with its species (Goblin Key, Skeleton Key, Vampire Key).
- **One Key Per Species:** Once a player obtains a specific key (e.g., Goblin Key), subsequent enemies of that same type (other Goblins) will **never** drop a key again.
- The player **must collect all 3 distinct keys** to unlock Option 3 and enter the Castle.
4. **Post-Combat Recovery:** After each successful battle, the player's HP and Mana bars must automatically replenish (either fully or partially) to their base amounts so they are ready for the next encounter.
5. **Experience & Leveling System:**
- Defeating an enemy grants **XP**. The amount of XP must scale proportionally to the enemy's power level.
- Upon reaching an XP threshold, the player levels up. **Leveling up must automatically increase the player's Max HP and Max Stamina/Mana**, making them strong enough to eventually face the Dragon.
6. **Final Boss Fight:** Once the 3 Keys are obtained and the player chooses to go to the Castle, they will face the Dragon. Defeating the Dragon breaks the curse, resulting in **Victory**. Dying at any point results in **Game Over**.
🔹 Example game loop structure:
```java
while (player.isAlive() && enemy.isAlive())
player.attack(enemy);
if (enemy.isAlive()) {
enemy.attack(player);
}
}
```
### 5️⃣ Step 5: Extra Features & Bonus Tasks ⭐
*(Optional for extra credit)*
**Dynamic Economy & Merchant System:** Implement coins that drop from enemies. Add a "Visit Merchant" option to the main loop where players can spend coins to buy specific weapons, armors, or consumables.
**Multiple Weapons & Inventory:** Players can buy, store, and swap between multiple weapons or use consumables mid-combat.
**Multiplayer/Party Mode:** Allow multiple players to team up and fight multiple enemies together. The Dragon's breath attack will damage the entire party simultaneously.
**PvP Mode:** Implement a **Player vs. Player** combat system.
### 6️⃣ Step 6: Write a Comprehensive README 📄
As the final mandatory step of your development, you must replace the default `README.md` with your own comprehensive documentation. Your README should include:
- A brief introduction to the game.
- How to compile and run your project from the terminal.
- An explanation of the classes, design patterns, and OOP principles you used.
- A brief guide on how to play (controls, stats, classes).
--- ---
## Evaluation Criteria ⚖ ## Locations
| **Criteria** | **Points** | The game world contains several locations that the player can visit:
|-------------------------------------------------------------|------------|
| Proper use of OOP principles | **50** |
| Working combat mechanics, Leveling System & Enemy abilities | **20** |
| Clear and Comprehensive `README.md` | **20** |
| Code readability, documentation, and comments | **10** |
| Meaningful, interactive, & colored console outputs | **10** |
| Inventory (item) and Merchant System (bonus) | **20** |
| Other Extra features (bonus tasks) | **20** |
| **Total Score** | **150** |
## Tips 🚀 - Village
- **Follow OOP principles**: Avoid redundant code by using inheritance properly. Think carefully about what belongs in an abstract class vs. a specific subclass. Make sure you use overriding and overloading correctly. - Forest
- **Test your code**: Run different scenarios (fighting, running out of mana, leveling up, dying) to ensure everything works as expected. - Cave
- **Ask for help**: If you're stuck, reach out to your classmates or mentors. - Castle
## Submission ⌛ Different locations may contain different enemies and items. The Castle acts as a special location that can only be accessed after collecting the required keys.
- **Deadline**: Submit your assignment before **21 Ordibehesht (May 11th, 2026)**.
- **Submission Format**: Push your code to your forked repository, create a PR, and ensure your comprehensive `README.md` is included in the root directory. ---
## Combat System
Combat in the game is turn-based. When the player encounters an enemy and chooses to fight, they are presented with five possible actions:
1. Light Attack
2. Heavy Attack
3. Defend
4. Heal
5. Special Ability
The player performs an action, and then the enemy responds with its own attack. The battle continues until either the player or the enemy reaches zero HP.
---
## Enemy Types
The game includes several enemy types with different behaviors.
### Goblin
A fast enemy that focuses on critical strikes.
### Skeleton
Can resurrect once during battle with half of its maximum health.
### Vampire
Has a lifesteal ability that restores health when it attacks.
### Dragon
A powerful enemy capable of bypassing normal defense.
---
## Inventory System
Players can collect and manage items using an inventory system. The inventory stores different types of items such as:
- Weapons
- Armors
- Consumables
- Keys
Examples of items include swords, daggers, staffs, armor sets, healing flasks, and keys required to unlock the Castle.
---
## Object-Oriented Design
### Inheritance
The project uses inheritance to organize game entities.
Examples:
- Player → Knight, Assassin, Wizard
- Enemy → Goblin, Skeleton, Vampire, Dragon
- Item → Weapon, Armor, Consumable
---
### Abstraction
Several base classes are abstract to define shared behavior without implementing full functionality.
Examples:
- Player
- Enemy
- Item
Subclasses provide specific implementations.
---
### Polymorphism
Different characters and enemies override methods such as attacks or special abilities, allowing each class to behave differently during combat.
---
### Encapsulation
Attributes such as health, mana, defense, and damage are protected within classes and accessed through getters and setters.
---
## Random Enemy Spawning
Enemies appear randomly depending on the location the player moves to. Each location has its own pool of possible enemies, and one may spawn when the player enters that location.
---
## Winning Condition
To win the game, the player must:
1. Explore locations and defeat enemies
2. Collect the required keys
3. Unlock the Castle
The game ends either when the player defeats the final challenge or when the player's HP reaches zero.
---
![cover](Readme_Pictures/image.png)
###### - Born of God and Void. You shall seal the blinding light that plagues their dreams. You are the Vessel. You are the Java Knight.