Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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.

edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
added 2 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
#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);
}
added 560 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Tweeted twitter.com/#!/StackCodeReview/status/488742050851196928
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /