1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
/*
Here is the framework to describe our data
*/
// Item descriptors
// First... the group that the item belongs to
enum ItemGroup
{
Wood,
Ore,
Fish,
Tool,
//etc
};
// Now, a structure to describe each individual item
struct Item
{
std::string name; // the name of this item as it is printed to the user
ItemGroup id; // what kind of item do we have? What group is it in?
int purchasePrice; // how much does it cost to buy this item?
int sellPrice; // how much to sell it?
int weight; // how much does it weigh to carry?
//etc
};
/*
Now... the actual data. Ideally this would be stored externally in a text file
or something, so that new items can be easily added later just by modifying that file.
To keep this simple, let's just have a big lookup table of all the items in the game
*/
const Item ItemList[] = {
{ "Wood Log", ItemGroup::Wood, 100, 50, 5 }; // Wood log, is of type wood, costs 100, sells for 50, weighs 5
{ "Oak Log", ItemGroup::Wood, 150, 60, 6 };
{ "Willow Log", ItemGroup::Wood, 175, 70, 4 };
//... etc
{ "Gold Ore", ItemGroup::Ore, 1000, 500, 10 };
//... etc
// This will actually be a huge table for how many items you have.
// But having the table huge here, means the rest of your code can be small.
};
/*
Now to keep track of inventory
*/
struct InventoryEntry
{
Item item; // the item in your inventory
int qty; // how many of that item you have
};
std::vector<InventoryEntry> inventory; // all the items the player has in their inventory
//
// ...
//
// Now when you want to print the user's inventory, you no longer need a large else/if chain.
// You can just use a loop and print each item in the inventory vector:
for(auto& i : inventory)
cout << "You have " << i.qty << " " << i.item.name << "s.\n"; // Ex: "You have 2 Gold Ores.\n"
|