Java Knight - Turn-Based RPG
Introduction
Java Knight is a turn-based RPG that runs in the terminal. The land of Javanest is cursed by a Dragon. People have turned into Goblins, Skeletons, and Vampires. You must defeat these monsters, collect their three keys, and finally kill the Dragon to break the curse.
How to Compile and Run
Prerequisites
- Java Development Kit (JDK) 8 or later
Compilation
Open a terminal in the root folder of the project (where the org folder is located). Run this command:
javac org/project/**/.java org/project/.java
Execution
After compilation, run:
java org.project.Main
OOP Principles and Design
The project uses the following object-oriented principles:
- Encapsulation – Fields like
hp,mp,damageare private or protected. Access is done through getter methods. - Inheritance –
Player(abstract) is extended byKnight,Assassin,Wizard.Enemy(abstract) is extended byGoblin,Skeleton,Vampire,Dragon. - Polymorphism – The
Entityinterface allows the game to treat players and enemies the same way. TheSpecialAbility()method works differently for each class. - Abstraction –
PlayerandEnemyhave abstract methods that subclasses must implement, likeSpecialAbility(). - Method Overriding – Every subclass overrides
SpecialAbility(),LightAttack(),heal(), etc.
Design Patterns
- Template Method Pattern – The combat loop in
Maindefines the basic turn structure, while specific actions are handled by overridden methods. - Factory Method Pattern – The method
spawnRandomEnemyForLocation()creates different enemy types without exposing the creation logic. - Strategy Pattern – The five player actions (Light Attack, Heavy Attack, Defend, Heal, Special Ability) are interchangeable strategies chosen at runtime.
Key Classes
| Class | Description |
|---|---|
Entity (interface) |
Defines combat methods: LightAttack, HeavyAttack, defend, heal, etc. |
Player (abstract) |
Manages HP, mana, defense stance, and common actions. |
Knight |
High base damage. Special: shield bash (stuns enemy). |
Assassin |
Highest mana. Special: invisible (dodge next attack + guaranteed critical hit). |
Wizard |
Highest HP. Special: spell that damages enemy and heals self. |
Enemy (abstract) |
Base for all monsters. |
Goblin |
30% critical hit chance on light attacks. |
Skeleton |
Revives once with 50% HP. |
Vampire |
Lifesteals 30% of damage dealt. |
Dragon |
Final boss. Fiery breath ignores defense and deals double damage. |
Main |
Game loop, location navigation, key collection, victory/loss logic. |
How to Play
Starting the Game
Run Main.java. You will see a menu to choose your class:
- Knight – High damage, good HP, stunning special ability.
- Assassin – High mana pool, can turn invisible to dodge and land critical hits.
- Wizard – Highest HP, special ability damages enemy and heals self.
Location and Exploration
You begin in a random location (Forest, Cemetery, Crypt, or Ruins). An enemy appears automatically. At each location, you have these options:
- 1. Fight the enemy – Start turn-based combat.
- 2. Move to another location – Skip the current enemy and go to a different location (a new random enemy will appear).
- 3. Go to the Castle – This option only appears after you collect all three keys. It starts the final boss fight against the Dragon.
Combat Actions (Your Turn)
During combat, you have five actions. Mana costs are shown in the menu:
| Action | Mana Cost | Effect |
|---|---|---|
| 1. Light Attack | 0 | Moderate damage |
| 2. Heavy Attack | 8 | High damage |
| 3. Defend | 6 | Reduces next enemy attack by 50% |
| 4. Heal | 10 | Restores a portion of your HP |
| 5. Special Ability | 15 | Class-unique move (see below) |
Special Abilities
- Knight – Shield Bash – Deals heavy damage and stuns the enemy. The enemy skips its next turn.
- Assassin – Invisibility – Dodges the next enemy attack completely. Your next hit becomes a critical hit (double damage).
- Wizard – Devastating Spell – Damages the enemy and heals the caster.
After Winning a Fight
- Your HP and mana are fully restored.
- The defeated enemy has a 20% chance to drop its unique key (Goblin Key, Skeleton Key, or Vampire Key).
- Each key can be obtained only once. Killing more Goblins will not give another Goblin Key.
Winning the Game
- Collect all three keys. Then the option "Go to the Castle" appears.
- Fight the Dragon. The Dragon has 180 HP. Its normal attack deals 45 damage. Its fiery breath deals double damage and ignores your defense.
- Defeat the Dragon to break the curse and save Javanest.
Losing the Game
- If your HP reaches zero during any fight, the game ends with "Game Over".
Good luck, hero.