I have an RPG-like program that uses a Character
class to specify and return information about an individual player/character. I was curious about a way to best set and return an inventory of string
s in terms of memory and performance for this purpose. I gave some thought to creating a struct
or using something similar to the following:
char *inventory[] = { "Item", "Item I", "Item II", "Item III" };
for (int i = 0; i < 4; ++i) {
char *pos = inventory[i];
while (*pos != '\0') {
printf("%s ", *(pos++));
}
And setting *inventory[]
in the above from a single array
containing all strings
in the program. My goal would be to return all the inventory from a player in the most efficient way possible, while still retaining the ability to choose individual items (through the same or another function); though this does seem problematic for scaling above 4 items, or when the size is changed.
How else can this be improved?
#include <iostream>
#include <string>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::string;
class Character {
private:
string name, classType;
int experience;
string inventory[4];
public:
// Should be separate?
struct Position {
int x; int y;
};
Position location;
public:
void Character::setName(string x) { name = x; }
string Character::getName() { return name; }
void Character::setClassType(string x) { classType = x; }
string Character::getClassType() { return classType; }
void Character::setExperience(int x) { experience = x; }
int Character::getExperience() { return experience; }
void Character::setPosition(int x, int y) {
location.x = x; location.y = y;
}
Position& Character::getPosition() { return location; }
void Character::setInventory(string(&x)[4]) { for (int i = 0; i < 4; ++i) { inventory[i] = x[i]; } }
string& Character::getInventory(int slot) { return inventory[slot]; }
};
void showUser(Character player);
int main() {
bool running(true);
while (running) {
try {
string itemsWizard[4] = { "Scroll of Invisibility", "Mana Potion", "Enchanted Staff", "Mage Robe" };
string itemsRanger[4] = { "Longbow", "Quiver", "Dagger", "Jerkin" };
string itemsWarrior[4] = { "Broadsword", "Shield", "Steel Breastplate", "Throwing Axe" };
Character characterI, characterII, characterIII;
characterI.setName("Jax");
characterI.setClassType("Ranger");
characterI.setExperience(4750);
characterI.setInventory(itemsRanger);
characterI.setPosition(50, 20);
characterII.setName("Brand");
characterII.setClassType("Warrior");
characterII.setExperience(9999);
characterII.setInventory(itemsWarrior);
characterII.setPosition(1, 45);
characterIII.setName("Paige");
characterIII.setClassType("Sorcerer");
characterIII.setExperience(5900);
characterIII.setInventory(itemsWizard);
characterIII.setPosition(65, 120);
cout << "\n" << "Retrieving Character Info..." << "\n" << endl;
showUser(characterI);
showUser(characterII);
showUser(characterIII);
} // Close try
// Generalized Catch
catch (std::exception & e) {
cerr << "\nERROR : " << e.what() << '\n';
}
char choice('c');
bool invalidChoice(true);
// Option to run again
while (cout << "\nRun Again? (y/n) \n\n" && (!(cin >> choice)) || (!(choice == 'n')) && (!(choice == 'y'))) {
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cerr << "\nERROR: The only valid answers are y/n. \n" << endl;
}
if (choice == 'y') {
invalidChoice = false;
cout << "\nSelected: \"" << choice << "\" *** RUNNING AGAIN. ***\n" << endl;
}
else if (choice == 'n') {
invalidChoice = false;
running = false;
cout << "\nSelected: \"" << choice << "\" *** EXITING. ***\n" << endl;
}
}
// Pause before returning
do {
cin.ignore();
cout << '\n' << "Press enter to continue...";
} while (cin.get() != '\n');
return 0;
}// End Main
void showUser(Character player) {
cout << "----------------------------------------" << endl;
cout << "Name : " << player.getName() << endl;
cout << "Class : " << player.getClassType() << endl;
cout << "Experience : " << player.getExperience() << endl;
Character::Position p = player.getPosition();
cout << "Location : (" << p.x << "," << p.y << ")" << endl;
for (int i = 0; i < 4; ++i) {
cout << "Inventory " << i + 1 << " : " << player.getInventory(i) << endl;
}
}