I've been on a quest to learn C++, and to learn, I've started building a small Role-Playing-Game skeleton to help myself learn the language. I've tried to make sure that I've followed C++ styles/standards correctly.
This is the first "part" of my development of the skeleton, and it includes a usable class, Character
. The Character
class has the following attributes:
characterName
- The name of the character.healthPoints
- How much health the player has. (Stays within the range \$0\rightarrow\infty\$.)experiencePoints
- How much experience the player has. (Stays within the range \$0\rightarrow\infty\$.)
It also has the following methods:
applyRandomDamage
- Apply a random amount of damage to the player in a certain range.applyDamage
- Apply a set amount of damage to the player.addRandomExperience
- Add a random amount of experience to the player.addExperience
- Add a set amount of experience to the player.toString
- Display the player's statistics. (Name/Health/experience)
I'm wondering the following things:
- Is there an override so that I can just
std::cout
an instantiatedCharacter
object without having to create my own customtoString
method? - Is this designed appropriately? Is there anything that could be designed differently?
- Am I following the correct C++ styles/standards?
- Is this idiomatic C++?
character.h
#pragma once
#include <iostream>
/// <summary>
/// Represents a character, with certain attributes, like
/// health points, or the character's name.
/// </summary>
class Character
{
public:
std::string characterName;
int healthPoints;
int experiencePoints;
Character(std::string c_characterName, int c_healthPoints, int c_experiencePoints);
void applyRandomDamage(int minimumDamage, int maximumDamage);
void applyDamage(int damage);
void addRandomExperience(int minimumExperience, int maximumExperience);
void addExperience(int experience);
void toString();
};
character.cpp
#include <iostream>
#include <string>
#include <random>
#include "character.h"
/// <summary>
/// The constructor for our character.
/// </summary>
/// <param name="characterName">The name of our character.</param>
Character::Character(std::string c_characterName, int c_healthPoints, int c_experiencePoints)
{
characterName = c_characterName;
healthPoints = c_healthPoints;
experiencePoints = c_experiencePoints;
}
/// <summary>
/// Apply a random amount of damage to the player, based on
/// a low and a high value.
/// </summary>
/// <param name="minimumDamage">The minimum damage to apply.</param>
/// <param name="maximumDamage">The maximum damage to apply.</param>
void Character::applyRandomDamage(int minimumDamage, int maximumDamage)
{
std::random_device randomDevice;
std::mt19937 engine(randomDevice());
std::uniform_int_distribution<int> distribution(minimumDamage, maximumDamage);
int damage = distribution(engine);
healthPoints = healthPoints - damage >= 0 ? healthPoints - damage : 0;
}
/// <summary>
/// Apply a set damage to the character, rather than a
/// random amount in a certain range.
/// </summary>
void Character::applyDamage(int damage)
{
healthPoints = healthPoints - damage >= 0 ? healthPoints - damage : 0;
}
/// <summary>
/// Add a random amount of experience to the player.
/// </summary>
/// <param name="minimumExperience">The minimum experience to add.</param>
/// <param name="maximumExperience">The maximum experience to add.</param>
void Character::addRandomExperience(int minimumExperience, int maximumExperience)
{
std::random_device randomDevice;
std::mt19937 engine(randomDevice());
std::uniform_int_distribution<int> distribution(minimumExperience, maximumExperience);
int experience = distribution(engine);
experiencePoints = experiencePoints + experience >= 0 ? experience + experience : 0;
}
/// <summary>
/// Add a set amount of experience to the player.
/// </summary>
/// <param name="experience">The experience to add to the player.</param>
void Character::addExperience(int experience)
{
experiencePoints = experiencePoints + experience >= 0 ? experience + experience : 0;
}
/// <summary>
/// Returns a string value representing the character, e.g
/// health points, or the character's name.
/// </summary>
void Character::toString()
{
std::cout << "Name: " << characterName << "\n";
std::cout << "Health: " << healthPoints << "\n";
std::cout << "Experience: " << experiencePoints << "\n";
}
main.cpp (tests)
#include <iostream>
#include "character.h"
int main()
{
Character character = Character("Billy Bob Jenkins", 100, 50);
character.toString();
character.applyRandomDamage(5, 10);
character.addRandomExperience(5, 10);
character.toString();
character.applyDamage(50);
character.addExperience(50);
character.toString();
std::cin.get();
}
Character character = Character("Billy Bob Jenkins", 100, 50);
can be rewritten asCharacter character{"Billy Bob Jenkins", 100, 50};
\$\endgroup\$