Developed a simple role-playing game (RPG) using the concepts of Object-Oriented Programming (OOP). The game includes a hero, enemies, and items, allowing the hero to fight enemies and collect items. The implementation covers the following OOP concepts:
Use private fields for classes and provide getter/setter methods.
Create a base class Character and derive subclasses Hero and Enemy.
Use method overriding to allow different behaviours for the attack() method in Hero and Enemy classes.
Create abstract methods in the Character class for actions like attack(), which are implemented differently in Hero and Enemy.
Handle exceptions when the hero tries to use an item that is not in their inventory. Throw a custom exception ItemNotFoundException. Ensure that finally is used to display a message like "Action completed."
a static method to keep track of the total number of enemies defeated in the game.
certain types of items (e.g., HealthPotion) are type-cast from a base class Item to restore health. String Functions:
Use string functions for hero and enemy names. For example, check if a hero’s name contains certain characters, or modify the hero’s name when they collect a special item.
An array of Item objects in the hero’s inventory. The array can store different types of items (e.g., Weapon, Armor, Potion). Association and Aggregation:
The Hero class should be associated with an Enemy during a battle. ###Aggregation: The Hero class aggregates multiple Item objects.
Attributes: name, health, attackPower Methods: attack(), takeDamage(), isAlive()
Additional Attributes: inventory (Array of Item objects) Methods: useItem(), addItem(), removeItem(), attack() Enemy Class (Inherits from Character)
Different attack behaviour than the Hero. Item Class (Base class for items)
Attributes: name, type HealthPotion Class (Inherits from Item)
Special behaviour for restoring health. Weapon and Armor Classes (Inherit from Item)
Weapons increase the hero’s attack power; Armor decreases damage taken. Custom Exceptions:
Thrown when the hero tries to use an item not in their inventory.
Manages the game loop, handles battles between the hero and enemies, and displays the current game status.
The hero battles an enemy, attacking and taking damage. After defeating the enemy, the hero collects a prize which can be an armor, weapon or health potion. The hero tries to use the potion, and the program handles an exception if the potion is not in the inventory. The game tracks how many enemies have been defeated in total. Different items in the inventory (like potions and weapons) are cast to their specific types to apply their effects. The game gets over if all enemies are defeated or the hero is defeated