Merge pull request 'Complete assignment core mechanics and add missing classes' (#1) from develop into main

Reviewed-on: #1

100 / 100
This commit was merged in pull request #1.
This commit is contained in:
2026-06-16 13:58:20 +00:00
41 changed files with 544 additions and 147 deletions
+1 -1
View File
@@ -8,7 +8,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" project-jdk-name="ms-25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+123 -6
View File
@@ -1,15 +1,132 @@
package org.project;
import org.project.location.Location;
import org.project.entity.enemies.*;
import org.project.entity.players.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO: ADD LOCATIONS TO YOUR GAME
List<Location> locations = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
Random random = new Random();
// TODO: IMPLEMENT GAMEPLAY
System.out.println("\u001B[33m=== The Legend of Javanest ===\u001B[0m");
System.out.println("Choose your Hero Class:");
System.out.println("1. Knight (High Damage)");
System.out.println("2. Wizard (High Health)");
System.out.println("3. Assassin (High Mana)");
int classChoice = scanner.nextInt();
Player player;
if (classChoice == 2) {
player = new Wizard("Merlin");
} else if (classChoice == 3) {
player = new Assassin("Ezio");
} else {
player = new Knight("Ser Duncan");
}
boolean hasGoblinKey = false;
boolean hasSkeletonKey = false;
boolean hasVampireKey = false;
boolean gameRunning = true;
while (gameRunning && player.isAlive()) {
boolean hasAllKeys = hasGoblinKey && hasSkeletonKey && hasVampireKey;
System.out.println("\n\u001B[35mYou arrived at a new location.\u001B[0m");
Enemy currentEnemy = spawnRandomEnemy(random);
System.out.println("A wild " + currentEnemy.getName() + " appears!");
boolean inLocation = true;
while (inLocation && player.isAlive()) {
System.out.println("\nWhat would you like to do?");
System.out.println("1. Fight the enemy");
System.out.println("2. Move to another location");
if (hasAllKeys) {
System.out.println("\u001B[31m3. Go to the Castle to fight the Dragon\u001B[0m");
}
int choice = scanner.nextInt();
if (choice == 1) {
fight(player, currentEnemy, scanner);
inLocation = false;
if (player.isAlive() && !currentEnemy.isAlive()) {
System.out.println("\u001B[32mVictory! " + currentEnemy.getName() + " was defeated.\u001B[0m");
player.gainXP(50);
player.heal(player.getMaxHP());
player.fillMana(player.getMaxMP());
if (currentEnemy instanceof Goblin && !hasGoblinKey && random.nextInt(100) < 20) {
System.out.println("\u001B[33m🗝️ You found the Goblin Key!\u001B[0m");
hasGoblinKey = true;
} else if (currentEnemy instanceof Skeleton && !hasSkeletonKey && random.nextInt(100) < 20) {
System.out.println("\u001B[33m🗝️ You found the Skeleton Key!\u001B[0m");
hasSkeletonKey = true;
} else if (currentEnemy instanceof Vampire && !hasVampireKey && random.nextInt(100) < 20) {
System.out.println("\u001B[33m🗝️ You found the Vampire Key!\u001B[0m");
hasVampireKey = true;
}
}
} else if (choice == 2) {
System.out.println("You moved to a new location.");
inLocation = false;
} else if (choice == 3 && hasAllKeys) {
System.out.println("\n\u001B[31m=== You entered the Castle! ===\u001B[0m");
Enemy dragon = new Dragon();
fight(player, dragon, scanner);
if (player.isAlive()) {
System.out.println("\u001B[33m🎉 You defeated the Dragon and saved Javanest! You Win!\u001B[0m");
}
gameRunning = false;
inLocation = false;
}
}
}
if (!player.isAlive()) {
System.out.println("\u001B[31m💀 Game Over. You died.\u001B[0m");
}
scanner.close();
}
private static Enemy spawnRandomEnemy(Random random) {
int r = random.nextInt(3);
if (r == 0) return new Goblin();
if (r == 1) return new Skeleton();
return new Vampire();
}
private static void fight(Player player, Enemy enemy, Scanner scanner) {
System.out.println("\u001B[31mYou chose to FIGHT!\u001B[0m");
while (player.isAlive() && enemy.isAlive()) {
System.out.println("\n[" + player.getName() + " - " + player.getHp() + "/" + player.getMaxHP() + " HP | " + player.getMp() + "/" + player.getMaxMP() + " Mana]");
System.out.println("[" + enemy.getName() + " - " + enemy.getHp() + "/" + enemy.getMaxHP() + " HP]");
System.out.println("\nYour Turn:");
System.out.println("1. Light Attack | 2. Heavy Attack (-8 Mana) | 3. Defend (-6 Mana) | 4. Heal (-12 Mana) | 5. Special Ability (-15 Mana)");
int action = scanner.nextInt();
if (player.isStunned()) {
System.out.println("\u001B[33mYou are stunned and cannot move!\u001B[0m");
player.setStunned(false);
} else {
switch (action) {
case 1: player.lightAttack(enemy); break;
case 2: player.heavyAttack(enemy); break;
case 3: player.defendAction(); break;
case 4: player.healAction(); break;
case 5: player.specialAbility(enemy); break;
default: player.lightAttack(enemy); break;
}
}
if (enemy.isAlive()) {
enemy.attackAction(player);
}
}
}
}
@@ -1,21 +1,15 @@
package org.project.entity;
public interface Entity {
void attack(Entity target);
void defend();
void heal(int health);
void fillMana(int mana);
void takeDamage(int damage);
void heal(int health);
void fillMana(int mana);
int getHp();
int getMaxHP();
int getMp();
int getMaxMP();
/*
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/
}
boolean isAlive();
String getName();
void setStunned(boolean stunned);
boolean isStunned();
}
@@ -0,0 +1,21 @@
package org.project.entity.enemies;
import org.project.entity.Entity;
public class Dragon extends Enemy {
public Dragon() {
super("Dragon", 250, 30);
}
@Override
public void attackAction(Entity target) {
if (!isStunned()) {
System.out.println("\u001B[31m🐉 Dragon breathes massive FIRE! (Bypasses shields)\u001B[0m");
target.takeDamage(baseDamage);
} else {
System.out.println("🐉 Dragon is stunned, but recovers quickly!");
setStunned(false);
}
}
}
@@ -1,34 +1,61 @@
package org.project.entity.enemies;
import org.project.item.weapons.Weapon;
import org.project.entity.Entity;
// TODO: UPDATE IMPLEMENTATION
public abstract class Enemy {
Weapon weapon;
private int hp;
private int mp;
public abstract class Enemy implements Entity {
protected String name;
protected int hp;
protected int maxHP;
protected int baseDamage;
protected boolean stunned;
public Enemy(int hp, int mp, Weapon weapon) {
public Enemy(String name, int hp, int baseDamage) {
this.name = name;
this.hp = hp;
this.mp = mp;
this.weapon = weapon;
this.maxHP = hp;
this.baseDamage = baseDamage;
this.stunned = false;
}
public abstract void attackAction(Entity target);
@Override
public void takeDamage(int damage) {
hp -= damage;
if (hp < 0) hp = 0;
System.out.println("\u001B[31m" + name + " took " + damage + " damage!\u001B[0m");
}
public int getHp() {
return hp;
@Override
public void heal(int health) {
hp += health;
if (hp > maxHP) hp = maxHP;
}
public int getMp() {
return mp;
}
@Override
public void fillMana(int mana) {}
public Weapon getWeapon() {
return weapon;
}
}
@Override
public int getHp() { return hp; }
@Override
public int getMaxHP() { return maxHP; }
@Override
public int getMp() { return 0; }
@Override
public int getMaxMP() { return 0; }
@Override
public boolean isAlive() { return hp > 0; }
@Override
public String getName() { return name; }
@Override
public void setStunned(boolean stunned) { this.stunned = stunned; }
@Override
public boolean isStunned() { return stunned; }
}
@@ -0,0 +1,29 @@
package org.project.entity.enemies;
import org.project.entity.Entity;
import java.util.Random;
public class Goblin extends Enemy {
private Random random;
public Goblin() {
super("Goblin", 30, 10);
this.random = new Random();
}
@Override
public void attackAction(Entity target) {
if (!isStunned()) {
if (random.nextInt(100) < 40) {
System.out.println("\u001B[31m👹 Goblin used Critical Strike!\u001B[0m");
target.takeDamage(baseDamage * 2);
} else {
System.out.println("👹 Goblin attacks normally!");
target.takeDamage(baseDamage);
}
} else {
System.out.println("👹 Goblin is stunned and skips the turn!");
setStunned(false);
}
}
}
@@ -1,6 +1,33 @@
package org.project.entity.enemies;
// TODO: UPDATE IMPLEMENTATION
public class Skeleton {
// TODO: DESIGN ENEMY AND IMPLEMENT THE CONSTRUCTOR
}
import org.project.entity.Entity;
public class Skeleton extends Enemy {
private boolean hasResurrected;
public Skeleton() {
super("Skeleton", 40, 8);
this.hasResurrected = false;
}
@Override
public void takeDamage(int damage) {
super.takeDamage(damage);
if (hp == 0 && !hasResurrected) {
System.out.println("\u001B[33m☠️ Skeleton resurrected with 50% HP!\u001B[0m");
hasResurrected = true;
hp = maxHP / 2;
}
}
@Override
public void attackAction(Entity target) {
if (!isStunned()) {
System.out.println("☠️ Skeleton uses Bone Strike!");
target.takeDamage(baseDamage);
} else {
System.out.println("☠️ Skeleton is stunned and skips the turn!");
setStunned(false);
}
}
}
@@ -0,0 +1,25 @@
package org.project.entity.enemies;
import org.project.entity.Entity;
public class Vampire extends Enemy {
public Vampire() {
super("Vampire", 50, 12);
}
@Override
public void attackAction(Entity target) {
if (!isStunned()) {
System.out.println("\u001B[31m🦇 Vampire uses Life Steal!\u001B[0m");
target.takeDamage(baseDamage);
int healAmount = baseDamage / 2;
heal(healAmount);
System.out.println("\u001B[32m🦇 Vampire healed for " + healAmount + " HP!\u001B[0m");
} else {
System.out.println("🦇 Vampire is stunned and skips the turn!");
setStunned(false);
}
}
}
@@ -0,0 +1,65 @@
package org.project.entity.players;
import org.project.entity.Entity;
public class Assassin extends Player {
private boolean isInvisible = false;
private boolean nextAttackCrit = false;
public Assassin(String name) {
super(name, 80, 120, 15);
}
@Override
public void takeDamage(int damage) {
if (isInvisible) {
System.out.println("\u001B[35m💨 " + name + " dodged the attack completely by being invisible!\u001B[0m");
isInvisible = false;
} else {
super.takeDamage(damage);
}
}
@Override
public void lightAttack(Entity target) {
int dmg = baseDamage;
if (nextAttackCrit) {
System.out.println("\u001B[31m💥 Critical Hit!\u001B[0m");
dmg *= 2;
nextAttackCrit = false;
}
target.takeDamage(dmg);
}
@Override
public void heavyAttack(Entity target) {
int cost = 8;
if (mp >= cost) {
mp -= cost;
int dmg = baseDamage * 2;
if (nextAttackCrit) {
System.out.println("\u001B[31m💥 Critical Hit!\u001B[0m");
dmg *= 2;
nextAttackCrit = false;
}
target.takeDamage(dmg);
} else {
lightAttack(target);
}
}
@Override
public void specialAbility(Entity target) {
int cost = 15;
if (mp >= cost) {
mp -= cost;
System.out.println("\u001B[36m🗡️ " + name + " (Assassin) used Shadow Step! (-15 Mana)\u001B[0m");
isInvisible = true;
nextAttackCrit = true;
System.out.println("\u001B[35m" + name + " turned invisible! Next attack will dodge and crit.\u001B[0m");
} else {
System.out.println("\u001B[34mNot enough mana! Defaulting to Light Attack.\u001B[0m");
lightAttack(target);
}
}
}
@@ -0,0 +1,11 @@
package org.project.entity.players;
import org.project.entity.Entity;
public interface ICombatActions {
void lightAttack(Entity target);
void heavyAttack(Entity target);
void defendAction();
void healAction();
void specialAbility(Entity target);
}
@@ -1,6 +1,25 @@
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;
public class Knight extends Player {
public Knight(String name) {
super(name, 100, 50, 20);
}
@Override
public void specialAbility(Entity target) {
int cost = 15;
if (mp >= cost) {
mp -= cost;
System.out.println("\u001B[36m⚔️ " + name + " (Knight) used Shield Bash! (-15 Mana)\u001B[0m");
target.takeDamage(baseDamage * 2);
target.setStunned(true);
System.out.println("\u001B[33m" + target.getName() + " is stunned for the next turn!\u001B[0m");
} else {
System.out.println("\u001B[34mNot enough mana! Defaulting to Light Attack.\u001B[0m");
lightAttack(target);
}
}
}
@@ -1,65 +1,123 @@
package org.project.entity.players;
import org.project.entity.Entity;
import org.project.item.armors.Armor;
import org.project.item.weapons.Weapon;
// TODO: UPDATE IMPLEMENTATION
public abstract class Player {
public abstract class Player implements Entity, ICombatActions {
protected String name;
Weapon weapon;
Armor armor;
private int hp;
private int maxHP;
private int mp;
private int maxMP;
protected int hp;
protected int maxHP;
protected int mp;
protected int maxMP;
protected int baseDamage;
protected boolean isDefending;
protected boolean stunned;
protected int level = 1;
protected int xp = 0;
public Player(String name, int hp, int mp, Weapon weapon, Armor armor) {
public Player(String name, int maxHP, int maxMP, int baseDamage) {
this.name = name;
this.hp = hp;
this.mp = mp;
this.weapon = weapon;
this.armor = armor;
this.maxHP = maxHP;
this.hp = maxHP;
this.maxMP = maxMP;
this.mp = maxMP;
this.baseDamage = baseDamage;
this.isDefending = false;
this.stunned = false;
}
@Override
public void attack(Entity target) {
target.takeDamage(weapon.getDamage());
}
@Override
public void defend() {
// TODO
}
@Override
public void takeDamage(int damage) {
hp -= damage - armor.getDefense();
if (isDefending) {
damage /= 2;
isDefending = false;
}
hp -= damage;
if (hp < 0) hp = 0;
}
@Override
public void heal(int health) {
hp += health;
if (hp > maxHP) {
hp = maxHP;
}
if (hp > maxHP) hp = maxHP;
}
@Override
public void fillMana(int mana) {
mp += mana;
if (mp > maxMP) {
mp = maxMP;
if (mp > maxMP) mp = maxMP;
}
@Override
public boolean isAlive() {
return hp > 0;
}
@Override
public void setStunned(boolean stunned) {
this.stunned = stunned;
}
@Override
public boolean isStunned() {
return stunned;
}
public void gainXP(int amount) {
xp += amount;
if (xp >= level * 100) {
levelUp();
}
}
protected void levelUp() {
level++;
maxHP += 20;
maxMP += 10;
baseDamage += 5;
hp = maxHP;
mp = maxMP;
}
@Override
public void lightAttack(Entity target) {
target.takeDamage(baseDamage);
}
@Override
public void heavyAttack(Entity target) {
int cost = 8;
if (mp >= cost) {
mp -= cost;
target.takeDamage(baseDamage * 2);
} else {
lightAttack(target);
}
}
@Override
public void defendAction() {
int cost = 6;
if (mp >= cost) {
mp -= cost;
isDefending = true;
}
}
@Override
public void healAction() {
int cost = 12;
if (mp >= cost) {
mp -= cost;
heal(maxHP / 3);
}
}
@Override
public String getName() {
return name;
}
@Override
public int getHp() {
return hp;
}
@@ -69,6 +127,7 @@ public abstract class Player {
return maxHP;
}
@Override
public int getMp() {
return mp;
}
@@ -77,13 +136,4 @@ public abstract class Player {
public int getMaxMP() {
return maxMP;
}
public Weapon getWeapon() {
return weapon;
}
public Armor getArmor() {
return armor;
}
}
}
@@ -0,0 +1,25 @@
package org.project.entity.players;
import org.project.entity.Entity;
public class Wizard extends Player {
public Wizard(String name) {
super(name, 150, 80, 12);
}
@Override
public void specialAbility(Entity target) {
int cost = 15;
if (mp >= cost) {
mp -= cost;
System.out.println("\u001B[36m🧙‍♂️ " + name + " (Wizard) cast Life Drain! (-15 Mana)\u001B[0m");
target.takeDamage(baseDamage * 2);
heal(maxHP / 4);
System.out.println("\u001B[32m" + name + " healed for " + (maxHP / 4) + " HP!\u001B[0m");
} else {
System.out.println("\u001B[34mNot enough mana! Defaulting to Light Attack.\u001B[0m");
lightAttack(target);
}
}
}
@@ -4,8 +4,4 @@ import org.project.entity.Entity;
public interface Item {
void use(Entity target);
/*
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/
}
}
@@ -1,17 +1,25 @@
package org.project.item.armors;
// TODO: UPDATE IMPLEMENTATION
public abstract class Armor {
import org.project.item.Item;
import org.project.entity.Entity;
public abstract class Armor implements Item {
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.maxDefense = defense;
this.durability = durability;
this.maxDurability = durability;
this.isBroke = false;
}
@Override
public void use(Entity target) {
}
public void checkBreak() {
@@ -21,7 +29,6 @@ public abstract class Armor {
}
}
// TODO: (BONUS) UPDATE THE REPAIR METHOD
public void repair() {
isBroke = false;
defense = maxDefense;
@@ -39,4 +46,9 @@ public abstract class Armor {
public boolean isBroke() {
return isBroke;
}
}
public void reduceDurability(int amount) {
durability -= amount;
checkBreak();
}
}
@@ -1,6 +1,7 @@
package org.project.item.armors;
// TODO: UPDATE IMPLEMENTATION
public class KnightArmor {
// TODO: DESIGN ARMOR'S ATTRIBUTES IMPLEMENT THE CONSTRUCTOR
public class KnightArmor extends Armor {
public KnightArmor() {
super(10, 100);
}
}
@@ -1,8 +1,6 @@
package org.project.item.consumables;
// TODO: UPDATE IMPLEMENTATION
public abstract class Consumable {
/*
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/
}
import org.project.item.Item;
public abstract class Consumable implements Item {
}
@@ -2,15 +2,9 @@ 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
public class Flask extends Consumable {
@Override
public void use(Entity target) {
target.heal(target.getMaxHP() / 10);
}
}
}
@@ -1,26 +1,20 @@
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 class Sword extends Weapon {
private int abilityCharge;
public Sword() {
// TODO: DESIGN SWORD'S ATTRIBUTES IMPLEMENT THE CONSTRUCTOR
super(15, 5);
this.abilityCharge = 0;
}
// TODO: (BONUS) UPDATE THE UNIQUE ABILITY
public void uniqueAbility(ArrayList<Entity> targets) {
abilityCharge += 2;
for (Entity target : targets) {
target.takeDamage(getDamage());
}
}
}
}
@@ -1,16 +1,12 @@
package org.project.item.weapons;
import org.project.entity.Entity;
import org.project.item.Item;
// TODO: UPDATE IMPLEMENTATION
public abstract class Weapon {
public abstract class Weapon implements Item {
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;
@@ -28,8 +24,4 @@ public abstract class Weapon {
public int getManaCost() {
return manaCost;
}
/*
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/
}
}
@@ -1,17 +1,17 @@
package org.project.location;
import org.project.entity.enemies.Enemy;
import java.util.ArrayList;
public class Location {
private String name;
private ArrayList<Location> locations;
private ArrayList<Enemy> enemies;
public Location(ArrayList<Location> locations, ArrayList<Enemy> enemies) {
this.locations = locations;
this.enemies = enemies;
public Location(String name, ArrayList<Location> locations, ArrayList<Enemy> enemies) {
this.name = name;
this.locations = locations != null ? locations : new ArrayList<>();
this.enemies = enemies != null ? enemies : new ArrayList<>();
}
public String getName() {
@@ -25,4 +25,4 @@ public class Location {
public ArrayList<Enemy> getEnemies() {
return enemies;
}
}
}
Binary file not shown.