\#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

//The base Entity class tyhat both Player and enemy will inherit from

class Entity {
	protected:
		string name;
		int health;
		int maxHealth;
		int attackPower;
	public:
		Entity(string n, int h, int a) {
			name = n;
			maxHealth = h;
			health = h;
			attackPower = a;
		}

		bool isAlive() const {
			return health > 0;
		}

		string getName() {
			return name;
		}

		int getHealth() const {
			return health;
		}

		void takeDamage(int damage) {
			health -= damage;
			if (health < 0) health = 0;
		}

		//Virtual function so derived classes can have their own attack logic

		virtual int attack() {
			//Simple random damage based on attack power
			return (rand() % attackPower) + 1;
		}
};

//Player class inheritng from entity
class Player : public Entity {
	private:
		int healthPotions;
	public:
		Player(string n): Entity(n, 100, 20) {
			healthPotions = 3;
		}

		void heal() {
			if (healthPotions > 0) {
				health += 30;
				if (health > maxHealth) 
					health = maxHealth;

					healthPotions--;

					cout <<"You drank a potion! Your current health is " << health << "/" << maxHealth <<".\n";
					cout<<"Your Potions remaining:"<< healthPotions <<"\n";
				} else {
					cout << ">>> Oh no! You are out of health potions!\n";
				}
			}

			void displayStats() const {
				cout << "\n=== " << name <<"'s Stats ===\n";
				cout << "HP: " << health << "/" << maxHealth << "\n";
				cout << "=====================\n";
			}

			int getPotions() const {
				return healthPotions;
			}
		};

		//Enemy Class inheriting from Entity
		class Enemy : public Entity {
		public:
			Enemy(string n, int h, int a) : Entity(n, h, a) {}

			//Enemies could have critical hits!
			int attack() override {
				int baseDamage = (rand() % attackPower) + 1;
				int critChance = rand() % 100;

				if (critChance > 80) { //20% chance to critically hit.
					cout << "*** CRITICAL HIT! *** \n";
					return baseDamage * 2;
				}

				return baseDamage;
			}
		};

		int main() {
			srand(time(NULL));

			cout << "Welcome to the Dungeon, hero!\n";
			cout << "Enter your name: ";
			string playerName;
			cin >> playerName;

			Player myPlayer(playerName);
			Enemy goblin("Vicious Goblin", 40, 12);

			cout << "\n A wild "<< goblin.getName() << " appears!\n";

			//Basic Battle Loop

			while (myPlayer.isAlive() && goblin.isAlive()) {
				myPlayer.displayStats();

				cout << "\n What will you do?\n";
				cout << "1. Attack\n";
				cout << "2. Heal\n";
				cout << "Choice: ";

				int choice;
				cin >> choice;

				if (choice == 1) {
					int dmg = myPlayer.attack();
					cout << "\nYou strike the " << goblin.getName() << " for " << dmg << " damage!\n";
					goblin.takeDamage(dmg);
				} else if (choice == 2) {
					myPlayer.heal();
				} else {
					cout << "Invalid choice. You tripped and missed your turn!\n";
				}
				
				//Enemy if is still alive!
				if (goblin.isAlive() && choice != 2) { //Give  player a break if they heal
					int enemyDmg = goblin.attack();
					cout << "The "<< goblin.getName() << " attacks you for "<< enemyDmg << " damage!\n";
					myPlayer.takeDamage(enemyDmg);
				}
			}

			if (myPlayer.isAlive()) {
				cout << "\n Victory! You defeated the "<< goblin.getName() << "!\n";
			} else {
				cout << "\nYou have fallen in battle... Game Over.\n";
			}

			return 0;
		}

