develop #1

Open
bitahajati wants to merge 4 commits from develop into main
39 changed files with 795 additions and 402 deletions
+3
View File
@@ -9,5 +9,8 @@
<module name="Java-Knight" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="README" target="25" />
</bytecodeTargetLevel>
</component>
</project>
+2
View File
@@ -3,5 +3,7 @@
<component name="Encoding">
<file url="file://$PROJECT_DIR$/Java-Knight/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/Java-Knight/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/README/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/README/src/main/resources" charset="UTF-8" />
</component>
</project>
+6
View File
@@ -5,8 +5,14 @@
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/Java-Knight/pom.xml" />
<option value="$PROJECT_DIR$/README/pom.xml" />
</list>
</option>
<option name="ignoredFiles">
<set>
<option value="$PROJECT_DIR$/README/pom.xml" />
</set>
</option>
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
@@ -1,15 +1,57 @@
package org.project;
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.GameDisplay;
import org.project.location.GameEngine;
import org.project.location.Location;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO: ADD LOCATIONS TO YOUR GAME
List<Location> locations = new ArrayList<>();
public class Main
{
public static void main(String[] args)
{
while (true)
{
GameDisplay display = new GameDisplay();
String choice = display.mainMenu();
// TODO: IMPLEMENT GAMEPLAY
Scanner scanner;
if (choice.equals("1"))
{
scanner = new Scanner(System.in);
System.out.println(display.BLUE + "Enter your name :" + display.RESET);
String n = scanner.nextLine();
String c = display.chooseCharacter();
Player p = null;
if (c.equals("1")) p = new Knight(n);
else if (c.equals("2")) p = new Assassin(n);
else if (c.equals("3")) p = new Wizard(n);
else
{
System.out.println("Invalid option");
return;
}
GameEngine gameEngine = new GameEngine(p);
gameEngine.runGame();
}
else if (choice.equals("2")) display.help();
else if (choice.equals("3"))
{
System.out.println("GOOD BYE!");
break;
}
else
{
System.out.println("Invalid option");
return;
}
}
}
}
}
@@ -1,21 +1,16 @@
package org.project.entity;
public interface Entity {
public interface Entity
{
void attack(Entity target);
void defend();
void heal(int health);
void heal();
void fillMana(int mana);
boolean isAlive();
void takeDamage(int damage);
int getMaxHP();
int getMaxMP();
/*
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/
void specialAbility(Entity target);
}
@@ -0,0 +1,22 @@
package org.project.entity.enemies;
import org.project.entity.Entity;
import org.project.location.GameDisplay;
public class Dragon extends Enemy
{
public Dragon(int hp, int mp, String name) { super(hp, mp,name); }
@Override
public void specialAbility(Entity target)
{
if(getMp() >= 5)
{
super.specialAbility(target);
System.out.println(GameDisplay.CYAN + "The Dragon threw a huge fire at the player 🔥🔥" + GameDisplay.RESET);
target.takeDamage(10);
this.setMp(this.getMp() - 5);
}
else System.out.println(GameDisplay.RED + "Dragon does not have enough MP tp use special ability!" + GameDisplay.RESET);
}
}
@@ -1,34 +1,79 @@
package org.project.entity.enemies;
import org.project.item.weapons.Weapon;
import org.project.entity.Entity;
import org.project.location.GameDisplay;
// TODO: UPDATE IMPLEMENTATION
public abstract class Enemy {
Weapon weapon;
public abstract class Enemy implements Entity {
protected boolean isDefending = false;
private String name;
private int hp;
private int mp;
public Enemy(int hp, int mp, Weapon weapon) {
public Enemy(int hp, int mp, String name) {
this.hp = hp;
this.mp = mp;
this.name = name;
}
this.weapon = weapon;
public void resetDefendState() { this.isDefending = false; }
@Override
public void attack(Entity target) {
System.out.println(GameDisplay.RED + "Enemy attacks! ⚔️" + GameDisplay.RESET);
target.takeDamage(2);
}
@Override
public void defend() {
if (getMp() >= 5) {
this.mp -= 5;
this.isDefending = true;
System.out.println(GameDisplay.BLUE + "Enemy defends! 🛡️" + GameDisplay.RESET);
} else {
System.out.println(GameDisplay.RED + "Enemy cannot defend! " + GameDisplay.RESET);
}
}
@Override
public void takeDamage(int damage) {
hp -= damage;
if (isDefending) {
damage = 0;
this.isDefending = false;
System.out.println(GameDisplay.GREEN + "Enemy blocks damage! 🛡️" + GameDisplay.RESET);
}
else System.out.println(GameDisplay.RED + "Enemy takes " + damage + " damage 🩸" + GameDisplay.RESET);
this.hp -= damage;
if (this.hp < 0) this.hp = 0;
}
public int getHp() {
return hp;
@Override
public void specialAbility(Entity target) {
System.out.println(GameDisplay.MAGENTA + "Enemy uses special ability! " + GameDisplay.RESET);
}
public int getMp() {
return mp;
@Override
public void heal() {
if (getMp() >= 6) {
this.hp += 10;
if (this.hp > 100) this.hp = 100;
this.mp -= 6;
System.out.println(GameDisplay.GREEN + "Enemy heals (+10 HP) 💊" + GameDisplay.RESET);
} else {
System.out.println(GameDisplay.RED + "Enemy cannot heal" + GameDisplay.RESET);
}
}
public Weapon getWeapon() {
return weapon;
}
}
@Override
public boolean isAlive() { return hp > 0; }
public String getName() {return name; }
public int getHp() { return hp; }
public void setHp(int newHp) { this.hp = newHp; }
public int getMp() { return mp; }
public void setMp(int newMp) { this.mp = newMp; }
}
@@ -0,0 +1,22 @@
package org.project.entity.enemies;
import org.project.entity.Entity;
import org.project.location.GameDisplay;
public class Goblin extends Enemy
{
public Goblin(int hp, int mp, String name) { super(hp, mp, name); }
@Override
public void specialAbility(Entity target)
{
if(getMp() >= 5)
{
super.specialAbility(target);
this.setMp(this.getMp() - 5);
System.out.println(GameDisplay.CYAN + "The Goblin struck a powerful below 👊" + GameDisplay.RESET);
target.takeDamage(7);
}
else System.out.println(GameDisplay.RED + "Goblin does not have enough MP to use Special ability" + GameDisplay.RESET);
}
}
@@ -1,6 +1,23 @@
package org.project.entity.enemies;
// TODO: UPDATE IMPLEMENTATION
public class Skeleton {
// TODO: DESIGN ENEMY AND IMPLEMENT THE CONSTRUCTOR
}
import org.project.entity.Entity;
import org.project.location.GameDisplay;
public class Skeleton extends Enemy
{
public Skeleton(int hp, int mp, String name) { super(hp, mp, name); }
@Override
public void specialAbility(Entity target)
{
if(getMp() >= 4)
{
super.specialAbility(target);
System.out.println(GameDisplay.CYAN + "Skeleton is regenerating its bones(+ %50 HP)! 🦴" + GameDisplay.RESET);
this.setMp(this.getMp() - 4);
this.setHp(this.getHp() / 2);
}
else System.out.println(GameDisplay.RED + "Skeleton does not have enough MP to use special ability!" + GameDisplay.RESET);
}
}
@@ -0,0 +1,22 @@
package org.project.entity.enemies;
import org.project.entity.Entity;
import org.project.location.GameDisplay;
public class Vampire extends Enemy
{
public Vampire(int hp, int mp, String name) { super(hp, mp, name); }
@Override
public void specialAbility(Entity target)
{
if(getMp() >= 8)
{
super.specialAbility(target);
System.out.println(GameDisplay.CYAN + "The Vampire strikes with a powerful bite! 🦷" + GameDisplay.RESET);
target.takeDamage(10);
this.setMp(this.getMp() - 8);
}
else System.out.println(GameDisplay.RED + "Vampire does not have enough MP for special ability." + GameDisplay.RESET);
}
}
@@ -0,0 +1,23 @@
package org.project.entity.players;
import org.project.entity.Entity;
import org.project.location.GameDisplay;
public class Assassin extends Player
{
public Assassin (String name) { super(name , 40, 60, 0); }
@Override
public void specialAbility(Entity target)
{
if (this.getMp() >= 9)
{
super.specialAbility(target);
System.out.println(GameDisplay.CYAN + "Assassin picks up the axe and deals a heavy below to the enemy ⚒️" + GameDisplay.RESET);
this.setMp(this.getMp() - 9);
target.takeDamage(12);
}
else System.out.println(GameDisplay.RED + "Assassin does not have enough MP to use special ability 😓" + GameDisplay.RESET);
}
}
@@ -1,6 +1,22 @@
package org.project.entity.players;
// TODO: UPDATE IMPLEMENTATION
public class Knight {
// TODO: DESIGN KNIGHT'S WEAPON AND ARMOR AND IMPLEMENT THE CONSTRUCTOR
}
import org.project.entity.Entity;
import org.project.location.GameDisplay;
public class Knight extends Player
{
public Knight(String name) { super(name, 50, 50, 0); }
@Override
public void specialAbility(Entity target)
{
if (this.getMp() >= 7)
{
super.specialAbility(target);
System.out.println(GameDisplay.CYAN + "The Knight attacks with a sharp sword! 🗡️💪" + GameDisplay.RESET);
target.takeDamage(8);
this.setMp(this.getMp() - 7);
}
else System.out.println(GameDisplay.RED + "Knight does not have enough MP to use special ability 😓" + GameDisplay.RESET);
}
}
@@ -1,89 +1,126 @@
package org.project.entity.players;
import org.project.entity.Entity;
import org.project.item.armors.Armor;
import org.project.item.weapons.Weapon;
import org.project.location.GameDisplay;
// TODO: UPDATE IMPLEMENTATION
public abstract class Player {
public abstract class Player implements Entity {
protected String name;
Weapon weapon;
Armor armor;
protected boolean isDefending = false;
private int hp;
private int maxHP;
private int maxHP = 100;
private int mp;
private int maxMP;
private int maxMP = 100;
private int xp;
public Player(String name, int hp, int mp, Weapon weapon, Armor armor) {
public Player(String name, int hp, int mp, int xp) {
this.name = name;
this.hp = hp;
this.mp = mp;
this.xp = xp;
}
this.weapon = weapon;
this.armor = armor;
public void gainXp(int newXp) {
this.xp += newXp;
System.out.println(GameDisplay.YELLOW + getName() + " gained " + newXp + " XP 🪙" + GameDisplay.RESET);
}
@Override
public void attack(Entity target) {
target.takeDamage(weapon.getDamage());
System.out.println(GameDisplay.CYAN + getName() + " attacks! ⚔️" + GameDisplay.RESET);
target.takeDamage(2);
}
public void heavyAttack(Entity target) {
if (getMp() >= 2) {
System.out.println(GameDisplay.CYAN + getName() + " uses Heavy Attack! 💣" + GameDisplay.RESET);
target.takeDamage(4);
this.mp -= 2;
} else {
System.out.println(GameDisplay.RED + getName() + " does not have enough MP for Heavy Attack! 😓" + GameDisplay.RESET);
}
}
@Override
public void defend() {
// TODO
if (getMp() >= 5) {
this.mp -= 5;
this.isDefending = true;
System.out.println(GameDisplay.BLUE + getName() + " defends ! 🛡️" + GameDisplay.RESET);
} else {
System.out.println(GameDisplay.RED + getName() + "does not have enough MP to defend! 😓" + GameDisplay.RESET);
}
}
@Override
public void takeDamage(int damage) {
hp -= damage - armor.getDefense();
if (isDefending) {
damage = 0;
this.isDefending = false;
System.out.println(GameDisplay.GREEN + getName() + " blocks damage!🛡️" + GameDisplay.RESET);
}
else System.out.println(GameDisplay.RED + getName() + " takes " + damage + " damage 🩸"+ GameDisplay.RESET);
this.hp -= damage;
if (this.hp < 0) this.hp = 0;
}
@Override
public void heal(int health) {
hp += health;
if (hp > maxHP) {
hp = maxHP;
public void heal() {
if (getMp() >= 6) {
this.hp += 10;
if (this.hp > maxHP) this.hp = maxHP;
this.mp -= 6;
System.out.println(GameDisplay.GREEN + getName() + " heals! 🩹" + GameDisplay.RESET);
} else {
System.out.println(GameDisplay.RED + getName() + " cannot heal due to lack of MP! 😓" + GameDisplay.RESET);
}
}
public void fillMana() {
if (xp >= 10) {
this.mp = getMaxMP();
this.xp -= 10;
System.out.println(GameDisplay.GREEN + getName() + " restored full Mana 🧪!" + GameDisplay.RESET);
} else {
System.out.println(GameDisplay.RED + getName() + " does not have enough XP to restore Mana! 😓" + GameDisplay.RESET);
}
}
public void fillHp() {
if (xp >= 50) {
this.hp = getMaxHP();
this.xp -= 50;
System.out.println(GameDisplay.GREEN + getName() + " restored 20 HP using 50 XP! 💪🤩" + GameDisplay.RESET);
} else {
System.out.println(GameDisplay.RED + getName() + " does not have enough XP to restore HP! 😓" + GameDisplay.RESET);
}
}
@Override
public void fillMana(int mana) {
mp += mana;
if (mp > maxMP) {
mp = maxMP;
}
}
public String getName() {
return name;
}
public int getHp() {
return hp;
public void specialAbility(Entity target) {
System.out.println(GameDisplay.MAGENTA +getName() + " uses special ability 😎" + GameDisplay.RESET);
}
@Override
public int getMaxHP() {
return maxHP;
public boolean isAlive() {
return hp > 0;
}
public int getMp() {
return mp;
}
public String getName() {return name; }
@Override
public int getMaxMP() {
return maxMP;
}
public int getHp() { return hp; }
public Weapon getWeapon() {
return weapon;
}
public void setHp(int newHp) { this.hp = newHp; }
public Armor getArmor() {
return armor;
}
public int getXp() { return xp; }
}
public void setXp(int newXp) { this.xp = newXp; }
public int getMaxHP() { return maxHP; }
public int getMp() { return mp; }
public void setMp(int newMp) { this.mp = newMp; }
public int getMaxMP() { return maxMP; }
}
@@ -0,0 +1,22 @@
package org.project.entity.players;
import org.project.entity.Entity;
import org.project.location.GameDisplay;
public class Wizard extends Player
{
public Wizard (String name) { super(name , 60, 40,0); }
@Override
public void specialAbility(Entity target)
{
if (this.getMp() >= 8)
{
super.specialAbility(target);
this.setMp(this.getMp() - 8);
System.out.println(GameDisplay.CYAN + "Amazing magic is comingggg, Bibbidi bobbidi boooooooo ⭐🪄" + GameDisplay.RESET);
target.takeDamage(10);
}
else System.out.println(GameDisplay.RED + "Wizard does not have enough MP to use special ability 😓" + GameDisplay.RESET);
}
}
@@ -1,11 +0,0 @@
package org.project.item;
import org.project.entity.Entity;
public interface Item {
void use(Entity target);
/*
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/
}
@@ -1,42 +0,0 @@
package org.project.item.armors;
// TODO: UPDATE IMPLEMENTATION
public abstract class Armor {
private int defense;
private int maxDefense;
private int durability;
private int maxDurability;
private boolean isBroke;
public Armor(int defense, int durability) {
this.defense = defense;
this.durability = durability;
}
public void checkBreak() {
if (durability <= 0) {
isBroke = true;
defense = 0;
}
}
// TODO: (BONUS) UPDATE THE REPAIR METHOD
public void repair() {
isBroke = false;
defense = maxDefense;
durability = maxDurability;
}
public int getDefense() {
return defense;
}
public int getDurability() {
return durability;
}
public boolean isBroke() {
return isBroke;
}
}
@@ -1,6 +0,0 @@
package org.project.item.armors;
// TODO: UPDATE IMPLEMENTATION
public class KnightArmor {
// TODO: DESIGN ARMOR'S ATTRIBUTES IMPLEMENT THE CONSTRUCTOR
}
@@ -1,8 +0,0 @@
package org.project.item.consumables;
// TODO: UPDATE IMPLEMENTATION
public abstract class Consumable {
/*
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/
}
@@ -1,16 +0,0 @@
package org.project.item.consumables;
import org.project.entity.Entity;
// TODO: UPDATE IMPLEMENTATION
public class Flask {
/*
THIS IS AN EXAMPLE OF A CONSUMABLE DESIGN.
*/
// TODO: UPDATE USE METHOD
@Override
public void use(Entity target) {
target.heal(target.getMaxHP() / 10);
}
}
@@ -1,26 +0,0 @@
package org.project.item.weapons;
import org.project.entity.Entity;
import java.util.ArrayList;
// TODO: UPDATE IMPLEMENTATION
public class Sword {
/*
THIS IS AN EXAMPLE OF A WEAPON DESIGN.
*/
int abilityCharge;
public Sword() {
// TODO: DESIGN SWORD'S ATTRIBUTES IMPLEMENT THE CONSTRUCTOR
}
// TODO: (BONUS) UPDATE THE UNIQUE ABILITY
public void uniqueAbility(ArrayList<Entity> targets) {
abilityCharge += 2;
for (Entity target : targets) {
target.takeDamage(getDamage());
}
}
}
@@ -1,35 +0,0 @@
package org.project.item.weapons;
import org.project.entity.Entity;
// TODO: UPDATE IMPLEMENTATION
public abstract class Weapon {
private int damage;
private int manaCost;
/*
TODO: ADD OTHER REQUIRED AND BONUS ATTRIBUTES
*/
public Weapon(int damage, int manaCost) {
this.damage = damage;
this.manaCost = manaCost;
}
@Override
public void use(Entity target) {
target.takeDamage(damage);
}
public int getDamage() {
return damage;
}
public int getManaCost() {
return manaCost;
}
/*
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/
}
@@ -0,0 +1,101 @@
package org.project.location;
import org.project.entity.enemies.Enemy;
import org.project.entity.players.Player;
import java.util.Scanner;
public class GameDisplay
{
public static final Scanner scanner = new Scanner(System.in);
public static final String RED = "\u001B[31m";
public static final String GREEN = "\u001B[32m";
public static final String YELLOW = "\u001B[33m";
public static final String BLUE = "\u001B[34m";
public static final String MAGENTA = "\u001B[35m";
public static final String CYAN = "\u001B[36m";
public static final String PINK = "\u001B[38;5;206m";
public static final String RESET = "\u001B[0m";
public void showPlayerTurn (Player player) { System.out.println("\n--- " + player.getName() + "'s Turn ---"); }
public void showStatus(Player player, Enemy enemy, GameEngine gameEngine)
{
System.out.println(BLUE + "\n==========================================================" + RESET);
System.out.println("[ Name: " + player.getName() + ", HP: " + player.getHp() + "/" + player.getMaxHP()
+ ", MP: "+ player.getMp() + "/" + player.getMaxMP() + " XP: " + player.getXp()
+", Keys:" + gameEngine.keys + " ]");
System.out.println("[ Enemy: " + enemy.getName() + ", HP: " + enemy.getHp() + ", MP: " + enemy.getMp() + "]");
System.out.println(BLUE + "==========================================================" + RESET);
}
public String battleChoices()
{
System.out.println("Your turn :");
System.out.println(PINK + "1. Attack (Free) " + RESET + YELLOW + "6. Restore Mana (10 XP) " + RESET);
System.out.println(PINK + "2. Heavy Attack (-2 MP) " + RESET + YELLOW +"7. Restore HP (50 XP)" + RESET);
System.out.println(PINK + "3. Defend (-5 MP)" );
System.out.println("4. Special Ability (Knight -7, Wizard -8, Assassin -9 MP)");
System.out.println("5. Heal (-6 MP, +10 HP)" + RESET);
return scanner.nextLine();
}
public String mainMenu()
{
System.out.println(BLUE + "==== Main Menu ====" + RESET);
System.out.println("1. New Game");
System.out.println("2. help");
System.out.println("3. Exit");
return scanner.nextLine();
}
public String chooseCharacter()
{
System.out.println(BLUE + "Choose your character:" + RESET);
System.out.println("1. Knight (50 HP, 50 MP)");
System.out.println("2. Assassin (40 HP, 60 MP)");
System.out.println("3. Wizard (60 HP, 40 MP)");
return scanner.nextLine();
}
public void help()
{
System.out.println("");
System.out.println("================================================================================");
System.out.println(" WELCOME TO THE DARK REALM ");
System.out.println("================================================================================");
System.out.println("");
System.out.println("You have entered a land shrouded in darkness, where evil creatures roam freely.");
System.out.println("Your mission is clear: defeat the forces of darkness and save the world from destruction.");
System.out.println("");
System.out.println(" HOW TO PLAY ");
System.out.println("1. CHOOSE YOUR HERO:");
System.out.println(" Before your journey begins, select one of the following classes:");
System.out.println(" 1.Knight 2.Wizard 3.Assassin");
System.out.println(" Each class has unique abilities and strengths. Choose wisely!");
System.out.println("");
System.out.println("2. EXPLORE AND COMBAT:");
System.out.println(" As you venture into the realm, you will encounter hostile monsters such as Goblins, Vampires, and Skeletons.");
System.out.println(" Engage them in battle to gain experience points (XP) and level up your character.");
System.out.println(" Higher levels mean stronger stats and better chances of survival.");
System.out.println("");
System.out.println("3. COLLECT KEYS:");
System.out.println(" Defeating specific types of monsters drops special keys:");
System.out.println(" - Killing Goblins drops the Goblin Key.");
System.out.println(" - Slaying Vampires yields the Vampire Key.");
System.out.println(" - Destroying Skeletons grants the Skeleton Key.");
System.out.println(" You must collect all three keys to unlock the final challenge.");
System.out.println("");
System.out.println("4. THE FINAL BOSS: THE DRAGON:");
System.out.println(" Once you possess all three keys, the path to the Dragon's Lair will open.");
System.out.println(" The Dragon is the ultimate ruler of evil and possesses immense power.");
System.out.println(" This is the final battle. If you defeat the Dragon, you will restore peace to the world and win the game.");
System.out.println(" If you fail, darkness will prevail forever.");
System.out.println("");
System.out.println("GOOD LUCK, HERO! MAY YOUR BLADE BE SHARP AND YOUR MAGIC STRONG :) ");
System.out.println("================================================================================");
System.out.println("");
}
}
@@ -0,0 +1,236 @@
package org.project.location;
import org.project.entity.enemies.*;
import org.project.entity.players.Player;
import java.util.*;
public class GameEngine
{
int keys = 0;
boolean goblinKey = false;
boolean skeletonKey = false;
boolean vampireKey = false;
GameDisplay display = new GameDisplay();
Player player;
Random random = new Random();
List<Location> locations = new ArrayList<>();
public GameEngine(Player player)
{
this.player = player;
createLocations();
}
public void runGame()
{
Location dragonLoc = locations.get(locations.size() - 1);
List<Location> enemyLoc = new ArrayList<>(locations);
enemyLoc .remove(enemyLoc .size() - 1);
Collections.shuffle(enemyLoc , random);
while (true)
{
if (keys >= 3)
{
System.out.println(display.YELLOW + "\n>>> You have collected all 3 Keys!🔑" + display.RESET);
System.out.println(">>> 🔓 Final battle : ");
System.out.println("----------------------------------------\n");
fightEnemies(dragonLoc.getEnemies(), dragonLoc.getName());
if(!player.isAlive())
{
System.out.println(display.RED + "\n ❌ GAME OVER! ❌ " + display.RESET);
return;
}
else
{
System.out.println(display.YELLOW + "\n🎉 CONGRATULATIONS! 🎉" + display.RESET);
return;
}
}
for (Location loc : enemyLoc)
{
System.out.println(">>> Entering: " + loc.getName());
System.out.println("----------------------------------------");
fightEnemies(loc.getEnemies(), loc.getName());
if (!player.isAlive())
{
System.out.println(display.RED + "\n ❌ You died! GAME OVER ❌" + display.RESET);
return;
}
if (keys >= 3) break;
}
}
}
private void fightEnemies(List<Enemy> enemies, String locationName)
{
for (int i = 0; i < enemies.size(); i++)
{
Enemy enemy = enemies.get(i);
System.out.println("❗An enemy is coming : " + enemy.getName() + "");
while (enemy.isAlive() && player.isAlive())
{
display.showStatus(player, enemy, this);
String choice = display.battleChoices();
switch (choice)
{
case "1":
display.showPlayerTurn(player);
player.attack(enemy);
break;
case "2":
display.showPlayerTurn(player);
player.heavyAttack(enemy);
break;
case "3":
display.showPlayerTurn(player);
player.defend();
break;
case "4":
display.showPlayerTurn(player);
player.specialAbility(enemy);
break;
case "5":
display.showPlayerTurn(player);
player.heal();
break;
case "6":
display.showPlayerTurn(player);
player.fillMana();
break;
case "7":
display.showPlayerTurn(player);
player.fillHp();
break;
default:
System.out.println("Invalid option. Try again.");
continue;
}
if (!enemy.isAlive())
{
System.out.println(display.BLUE + ">> Enemy died ! ✔️" + display.RESET);
break;
}
enemyTurn(enemy);
if (!player.isAlive()) break;
}
if (player.isAlive() && !enemy.isAlive())
{
int xp= 0;
boolean isLastEnemyInLocation = (i == enemies.size() - 1);
if (enemy instanceof Goblin)
{
xp = 10;
if (!goblinKey)
{
if (isLastEnemyInLocation || random.nextDouble() < 0.4)
{
goblinKey = true;
keys++;
System.out.println(display.YELLOW + ">> You found the Goblin Key! 🔑" + display.RESET);
}
}
}
else if (enemy instanceof Skeleton)
{
xp = 15;
if (!skeletonKey)
{
if (isLastEnemyInLocation || random.nextDouble() < 0.4)
{
skeletonKey = true;
keys++;
System.out.println(display.YELLOW + ">> You found the Skeleton Key!🔑" + display.RESET);
}
}
}
else if (enemy instanceof Vampire)
{
xp = 20;
if (!vampireKey)
{
if (isLastEnemyInLocation || random.nextDouble() < 0.4)
{
vampireKey = true;
keys++;
System.out.println(display.YELLOW + ">> You found the Vampire Key!🔑" + display.RESET);
}
}
}
player.gainXp(xp);
}
if (keys >= 3) return;
}
}
private void enemyTurn(Enemy enemy)
{
enemy.resetDefendState();
System.out.println("\n--- Enemy's Turn ---");
int action = random.nextInt(4);
switch (action)
{
case 0:
enemy.attack(player);
break;
case 1:
enemy.defend();
break;
case 2:
enemy.specialAbility(player);
break;
case 3:
enemy.heal();
break;
}
}
private void createLocations()
{
List<Enemy> goblins = new ArrayList<>();
for (int i = 0; i < 4; i++)
{
goblins.add(new Goblin(10, 10, "Goblin👹"));
}
List<Enemy> skeletons = new ArrayList<>();
for (int i = 0; i < 4; i++)
{
skeletons.add(new Skeleton(20, 20, "Skeleton💀"));
}
List<Enemy> vampires = new ArrayList<>();
for (int i = 0; i < 4; i++)
{
vampires.add(new Vampire(30, 30, "Vampire🧛‍♂️"));
}
List<Enemy> dragon = new ArrayList<>();
dragon.add(new Dragon(100, 100, "Dragon🐉"));
locations.add(new Location("Goblin Forest", goblins));
locations.add(new Location("Skeleton Graveyard", skeletons));
locations.add(new Location("Vampire Castle", vampires));
locations.add(new Location("Dragon Cave", dragon));
}
}
@@ -2,15 +2,16 @@ package org.project.location;
import org.project.entity.enemies.Enemy;
import java.util.ArrayList;
import java.util.List;
public class Location {
public class Location
{
private String name;
private List<Enemy> enemies;
private ArrayList<Enemy> enemies;
public Location(ArrayList<Location> locations, ArrayList<Enemy> enemies) {
this.locations = locations;
public Location(String name,List<Enemy> enemies)
{
this.name = name;
this.enemies = enemies;
}
@@ -18,11 +19,7 @@ public class Location {
return name;
}
public ArrayList<Location> getLocations() {
return locations;
}
public ArrayList<Enemy> getEnemies() {
public List<Enemy> getEnemies() {
return enemies;
}
}
Binary file not shown.
+88 -159
View File
@@ -1,175 +1,104 @@
# Fourth Assignment - Java Knight ⚔️
# Java Knight ⚔️
A turn-based RPG with Roguelike elements which can be run in the terminal.
### **Prologue: The Legend of Javanest**
*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!*
### The Legend of Javanest :
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!
### **Introduction**
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.
### Your Mission :
1️⃣ Explore dangerous lands inhabited by Goblins, Skeletons, and Vampires.</br>
2️⃣ Defeat these enemies to gain experience points (XP) and collect special Keys.</br>
3️⃣ Collect all three keys (Goblin Key, Skeleton Key, Vampire Key).</br>
4️⃣ Once all keys are collected, the path to the Dragons Lair will open.</br>
5️⃣ Face the Dragon in a final, epic battle. Victory means saving the world; failure means eternal darkness.</br>
---
## How to play ?
#### 1. Character Selection 👤</br>
At the start of the game, choose one of the following classes:</br>
**💂‍♀️Knight :** Balanced stats with high defense. Good for players who prefer a tank-like playstyle.</br>
**🥷Assassin :** High damage output but lower HP. Requires careful management of MP for critical hits.</br>
**🧙‍♂️ Wizard :** Powerful magic abilities capable of dealing massive area damage, but fragile in close combat.</br>
#### 2. Combat System🎮
Turn-Based Battles:</br>
Engage in turn-based combat against various enemies.
## Tasks 📝
### 1️⃣ Step 1: Fork & Setup 🍴
1. **Fork** this repository and clone it to your local machine.
```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.
- **Entities & Locations:** You have `Entity`, `Item`(Bonus) , and `Location`.
- **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]
**Actions:**</br>
- Attack: Basic free attack.</br>
- Heavy Attack: Costs MP but deals double damage.</br>
- Defend: Blocks incoming damage completely (Costs MP).</br>
- Special Ability: Each class has a unique powerful move (e.g., Wizards fireball, Assassins backstab).</br>
- Heal: Restore HP using MP.</br>
- Restore Mana/HP: Use accumulated XP to refill resources.</br>
- Enemy AI: Enemies can attack, defend, use special abilities, or heal themselves.</br>
#### 3. Progression & Keys🔑
Killing specific types of enemies drops a corresponding key:</br>
Kill Goblins → Get Goblin Key.</br>
Kill Skeletons → Get Skeleton Key.</br>
Kill Vampires → Get Vampire Key.</br>
You must collect all 3 Keys to unlock the final stage.</br>
XP gained from battles allows you to restore health and mana during fights.
---
Your Turn:
1. Light Attack | 2. Heavy Attack (-8 Mana) | 3. Defend (-6 Mana) | 4. Heal (-12 Mana) | 5. Shield Bash (-15 Mana)
```
## 💻 Object-Oriented Programming Concepts Used
This project is built using the main concepts of Object-Oriented Programming (OOP) in Java. Lets explain each one in a simple way.
### 1️⃣ Abstraction
Abstraction means defining the important structure first, and leaving the details for later.
**In this project :**</br>
The Entity interface defines what every living being in the game must be able to do (like attack, defend, and take damage).</br>
The Player and Enemy classes are abstract.</br>
They contain shared logic.</br>
But they leave some methods (like specialAbility) for subclasses to implement.
### 2️⃣ Inheritance
Inheritance allows one class to reuse properties and behaviors from another class.
In this project:</br>
Knight, Assassin, and Wizard inherit from Player.</br>
Goblin, Skeleton, Vampire, and Dragon inherit from Enemy.</br>
This means:</br>
All players can attack and defend (because they inherit from Player).</br>
But each one has its own unique special ability.
### 3️⃣ Polymorphism
Polymorphism means we can treat different objects as the same general type, but they behave differently.
**Example :**</br>
```bash
→ 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
player.specialAbility(target);
```
Java decides at runtime which version to execute :</br>
- Knights ability</br>
- Wizards spell</br>
- Assassins strike
### 4️⃣ Encapsulation
Encapsulation means protecting the internal data of a class.
In this project:
- Private Fields: Attributes like hp, mp, and xp in Player and Enemy classes are marked as private.
- Getters/Setters: Access to these variables is controlled via public methods like getHp(), setHp(), etc.
### 5️⃣ Interface
An interface works like a contract.
The Entity interface says that every entity in the game must implement:
```bash
attack()
takeDamage()
isAlive()
```
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).
### 4️⃣ Step 4: Implement the Game Loop & Progression 🎮
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).
It doesnt matter if its a Player or an Enemy — they must follow these rules.
---
## How to Run 🚀
- Clone the repository.</br>
- Open the project in your preferred Java IDE.</br>
- Ensure all packages (org.project.entity, org.project.location, etc.) are correctly structured.</br>
- Run the Main class.</br>
## Evaluation Criteria ⚖
**Follow the on-screen instructions to begin your adventure and enjoy the game :)**
| **Criteria** | **Points** |
|-------------------------------------------------------------|------------|
| 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 🚀
- **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.
- **Test your code**: Run different scenarios (fighting, running out of mana, leveling up, dying) to ensure everything works as expected.
- **Ask for help**: If you're stuck, reach out to your classmates or mentors.
## Submission ⌛
- **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.
![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.