Text-based RPG game using classes Text-based RPG game using classes
I am proud to declare that my code appears more encapsulated than the original code, which was full of getters and setters. Mine does not have any at all, and instead uses inheritance, which was inspired by this answer to the same question this answer to the same question. As it's my first real attempt at it, I'd especially like that aspect reviewed. I feel that my attempt isn't quite right, also because the derived classes don't have their own functions.
Text-based RPG game using classes
I am proud to declare that my code appears more encapsulated than the original code, which was full of getters and setters. Mine does not have any at all, and instead uses inheritance, which was inspired by this answer to the same question. As it's my first real attempt at it, I'd especially like that aspect reviewed. I feel that my attempt isn't quite right, also because the derived classes don't have their own functions.
Text-based RPG game using classes
I am proud to declare that my code appears more encapsulated than the original code, which was full of getters and setters. Mine does not have any at all, and instead uses inheritance, which was inspired by this answer to the same question. As it's my first real attempt at it, I'd especially like that aspect reviewed. I feel that my attempt isn't quite right, also because the derived classes don't have their own functions.
#include "Game.hpp"
#include <iostream>
Game::Game() : monsters(50) {}
void Game::play()
{
std::cout << "Number of monsters: " << monsters.size() << "\n\n";
while (!player.health_depleted() && !monsters.empty())
{
std::cout << "\nPlayer\n" << player;
battle();
if (all_monsters_dead())
{
std::cout << "\n\n> All monsters killed!!!";
}
if (player.health_depleted())
{
std::cout << "> Player dead...\n\n";
}
}
}
void Game::battle()
{
std::cout << "\n\n> Player attacks!\n";
player.attack(monsters.backcurrent_monster());
if (current_monster().health_depleted())
{
std::cout << "> Monster killed!\n";
player.add_EXP(current_monster());
monsters.pop_back();
}
std::cout << "\nMonster " << monsters.size() << "\n" << current_monster();
std::cout << "\n\n> Monster attacks!\n";
current_monster().attack(player);
}
#include "Game.hpp"
#include <iostream>
Game::Game() : monsters(50) {}
void Game::play()
{
std::cout << "Number of monsters: " << monsters.size() << "\n\n";
while (!player.health_depleted() && !monsters.empty())
{
std::cout << "\nPlayer\n" << player;
battle();
if (all_monsters_dead())
{
std::cout << "\n\n> All monsters killed!!!";
}
if (player.health_depleted())
{
std::cout << "> Player dead...\n\n";
}
}
}
void Game::battle()
{
std::cout << "\n\n> Player attacks!\n";
player.attack(monsters.back());
if (current_monster().health_depleted())
{
std::cout << "> Monster killed!\n";
player.add_EXP(current_monster());
monsters.pop_back();
}
std::cout << "\nMonster " << monsters.size() << "\n" << current_monster();
std::cout << "\n\n> Monster attacks!\n";
current_monster().attack(player);
}
#include "Game.hpp"
#include <iostream>
Game::Game() : monsters(50) {}
void Game::play()
{
std::cout << "Number of monsters: " << monsters.size() << "\n\n";
while (!player.health_depleted() && !monsters.empty())
{
std::cout << "\nPlayer\n" << player;
battle();
if (all_monsters_dead())
{
std::cout << "\n\n> All monsters killed!!!";
}
if (player.health_depleted())
{
std::cout << "> Player dead...\n\n";
}
}
}
void Game::battle()
{
std::cout << "\n\n> Player attacks!\n";
player.attack(current_monster());
if (current_monster().health_depleted())
{
std::cout << "> Monster killed!\n";
player.add_EXP(current_monster());
monsters.pop_back();
}
std::cout << "\nMonster " << monsters.size() << "\n" << current_monster();
std::cout << "\n\n> Monster attacks!\n";
current_monster().attack(player);
}