How would you improve on this? It's been fairly long since I last did something slightly more extensive in C++, so I'm looking for ways to be more compatible with modern C++ ways, and this style of OOP. I think especially the user action part and new opponent generation might be what needs the most work?
main.cpp
#include <iostream>
#include "player.hpp"
#include "enemy.hpp"
int main() {
Player pc;
Enemy monster;
while(pc.isAlive()) {
// Player has the first round
std::cout << pc.getName() << " attacks " << monster.getName() << "!" << std::endl;
int dmg = pc.getAttackDmg();
monster.takeDamage(dmg);
// The monster strikes back unless dead
if(monster.isAlive()) {
std::cout << "Monster retaliates!" << std::endl;
int monsterDmg = monster.getAttackDmg();
pc.takeDamage(monsterDmg);
} else {
// Player receives a reward for slaying the enemy successfully!
int experience = monster.getXP();
std::cout << monster.getName() << " has died! You earn " << experience << " XP." << std::endl;
std::cout << "-- Battle ends --\n" << std::endl;
pc.addXP(experience);
monster.getNew(pc.getLevel()); // Reset enemy and adjust to PC level
}
// You choose what to do.
std::cout << "\nWhat is your action? (A)ttack / (H)eal / (R)un away and end the quest?";
std::string pcChoice;
std::cin >> pcChoice;
if(pcChoice == "A" || pcChoice == "a") {
continue;
} else if(pcChoice == "H" || pcChoice == "h") {
pc.heal();
} else {
break;
}
}
return 0;
}
player.hpp
#ifndef PLAYER_H
#define PLAYER_H
#include "character.hpp"
class Player : public Character {
private:
int experience;
void checkLevel();
public:
Player();
void addXP(int n);
int getLevel();
void heal();
};
#endif
player.cpp
#include <iostream>
#include <string>
#include "player.hpp"
Player::Player() {
attack = 5;
defense = 20;
health = 40;
experience = 0;
level = 1;
std::cout << "Player enters! What is your name?" << std::endl;
std::string pcName;
std::cin >> pcName;
name = pcName;
}
int Player::getLevel() {
return level;
}
void Player::heal() {
health += 20 * level;
}
// Whenever PC gets experience, we check for level-up
void Player::addXP(int n) {
experience += n;
checkLevel();
}
// XP needed for level-up increases according to PC level
void Player::checkLevel() {
int targetExperience = 2000 * level;
if(experience >= targetExperience) {
level += 1;
std::cout << name << " has leveled up to level " << level << "!" << std::endl;
experience = 0;
int attackBonus = level;
int defenseBonus = level*2;
int healthBonus = level*4;
health = 40 + healthBonus;
}
}
enemy.hpp
#ifndef ENEMY_H
#define ENEMY_H
#include "character.hpp"
class Enemy : public Character {
private:
int xp;
public:
Enemy();
int getAttackDmg();
int getXP();
void getNew(int pcLevel);
};
#endif
enemy.cpp
#include <iostream>
#include "enemy.hpp"
Enemy::Enemy() {
std::cout << "I\'m a monsterman!" << std::endl;
name = "Monster";
attack = 3;
defense = 10;
health = 30;
xp = health * defense + attack;
}
// Monsters have a unique attack method
int Enemy::getAttackDmg() {
return attack;
}
int Enemy::getXP() {
return xp;
}
void Enemy::getNew(int pcLevel) {
std::cout << "** GRAAARR!! ** A new monster appears!" << std::endl;
attack = pcLevel * 3;
defense = pcLevel * 10;
health = pcLevel * 30;
xp = health * defense + attack;
}
character.hpp
#ifndef CHARACTER_H
#define CHARACTER_H
class Character {
protected:
std::string name;
int attack;
int defense;
int health;
int level;
public:
Character();
virtual bool isAlive();
int getHealth();
std::string getName();
int getAttackDmg();
int takeDamage(int d);
};
#endif
character.cpp
#include <iostream>
#include "character.hpp"
Character::Character() {
name = "Dummy";
attack = 1;
defense = 1;
health = 1;
level = 1;
}
bool Character::isAlive() {
return health > 0;
}
int Character::getHealth() {
return health;
}
std::string Character::getName() {
return name;
}
int Character::getAttackDmg() {
int dmg = attack * level;
return dmg;
}
int Character::takeDamage(int d) {
int effectiveDamage = d - (defense/100);
std::cout << name << " takes " << effectiveDamage << " DMG! ";
health -= effectiveDamage;
std::cout << "After the attack, " << health << " HP remains." << std::endl;
}