Skip to content

Instantly share code, notes, and snippets.

@jherr
Created February 2, 2021 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jherr/1ced89de7e5a7bcd477152bc49a1892f to your computer and use it in GitHub Desktop.
Save jherr/1ced89de7e5a7bcd477152bc49a1892f to your computer and use it in GitHub Desktop.
#include <random>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
int rollDice(int top) {
return (rand() % top) + 1;
}
enum Statistics {
Strength = 0,
Intelligence,
Wisdom,
Dexterity,
Constitution,
Charisma
};
class Character {
protected:
int statistics[6];
string name;
public:
Character(string inName) {
name = inName;
for(int i = 0; i < 6; i++) {
statistics[i] = rollDice(6) + rollDice(6) + rollDice(6);
}
}
int getStrength() {
return statistics[Strength];
}
int getIntelligence() {
return statistics[Intelligence];
}
int getWisdom() {
return statistics[Wisdom];
}
int getDexterity() {
return statistics[Dexterity];
}
int getConstitution() {
return statistics[Constitution];
}
int getCharisma() {
return statistics[Charisma];
}
virtual string getClass() {
return "";
}
string getFormattedDisplay() {
stringstream fmtString;
fmtString << "-----------------" << endl;
fmtString << "Name: " << name << endl;
fmtString << "Class: " << getClass() << endl;
fmtString << "Strength : " << getStrength() << endl;
fmtString << "Intelligence: " << getIntelligence() << endl;
fmtString << "Wisdom : " << getWisdom() << endl;
fmtString << "Dexterity : " << getDexterity() << endl;
fmtString << "Constitution: " << getConstitution() << endl;
fmtString << "Charisma : " << getCharisma() << endl;
fmtString << "-----------------" << endl;
return fmtString.str();
}
protected:
void setStrength(int value) {
statistics[Strength] = value;
}
void setIntelligence(int value) {
statistics[Intelligence] = value;
}
void setWisdom(int value) {
statistics[Wisdom] = value;
}
void setDexterity(int value) {
statistics[Dexterity] = value;
}
void setConstitution(int value) {
statistics[Constitution] = value;
}
void setCharisma(int value) {
statistics[Charisma] = value;
}
};
class Fighter : public Character {
public:
Fighter(string inName): Character(inName) {
prioritizeStatistics();
}
string getClass() {
return "Fighter";
}
private:
void prioritizeStatistics() {
vector<int> myvector (statistics, statistics+6);
sort(myvector.begin(), myvector.end());
reverse(myvector.begin(), myvector.end());
setStrength(myvector[0]);
setConstitution(myvector[1]);
setDexterity(myvector[2]);
setCharisma(myvector[3]);
setWisdom(myvector[4]);
setIntelligence(myvector[5]);
}
};
class Wizard : public Character {
public:
Wizard(string inName): Character(inName) {
prioritizeStatistics();
}
string getClass() {
return "Wizard";
}
private:
void prioritizeStatistics() {
vector<int> myvector (statistics, statistics+6);
sort(myvector.begin(), myvector.end());
reverse(myvector.begin(), myvector.end());
setStrength(myvector[5]);
setConstitution(myvector[2]);
setDexterity(myvector[4]);
setCharisma(myvector[3]);
setWisdom(myvector[1]);
setIntelligence(myvector[0]);
}
};
int main() {
srand(time(NULL));
Fighter elricTheFighter("Elric");
cout << elricTheFighter.getFormattedDisplay();
Wizard harryTheWizard("Harry");
cout << harryTheWizard.getFormattedDisplay();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment