int main()
{
const int maxItems = 4;
string inventory[maxItems]
{"Sword", "Bronze Helmet", "Shield"};
// Print inventory as it is now.
for (int x = 0; x < maxItems; ++x)
{
std::cout << inventory[x] << std::endl;
}
// Give the user a chance to pick up a 4th item.
std::cout << "You see an axe, bat, and a keyboard.";
std::cout << "Which would you like to pick up?";
}
Here I could simply write that std::cin = inventory[3], but if I did that it would be a very top-down approach. What I want to do is present the user with options, like, if there are only 3/4 items in a list, then just append the new item to the list (as #4). So if there are only 2 items, the new item would be the 3rd, etc, instead of having to declare which element of the array I want it to be.
Also I'd like to implement features such as dropping an item (deleting it) and replacing an item. How would I go about a seemingly impossible task such as this? (This is me being sarcastic, obviously I am a noob. :) )
Using STL container seems like a good idea. You can also erase stuff from list. Its pretty dynamical.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <vector>
#include <string>
#include <iostream>
usingnamespace std;
vector<string>inventory;
vector<string>::iterator iter;
int main(){
//add some stuff to "end" of list
inventory.push_back("Axe");
inventory.push_back("Helm");
//see whats in the list
for(iter = inventory.begin(); iter != inventory.end(); iter++)
cout << *iter << " ";
}
And if you want to replace an item from the container without knowing what or where it is ahead of time you can use the find function:
1 2 3 4 5 6 7 8 9 10 11 12
string item_to_replace, item_to_keep;
//ask the user what items to replace and keep here and assign to those strings
iter = find( inventory.begin(), inventory.end(), item_to_replace );
//this checks to make sure the item was found before trying to replace it
if( iter == inventory.end() )
{ cout << "Item not found in inventory" << endl; }
else
{ *iter = item_to_keep; }
I think you should practice the basics a little more before attempting such an ambitious project. An item should be its own class, as well as the inventory being it's own class. This would allow code such as:
1 2 3 4 5 6 7
inventory.displayInventory(); //Inventory empty
inventory.add(new sword(swordAttack));
inventory.add(new shield(shieldDefense));
inventory.add(new armor(armorDefense));
inventory.displayInventory(); // Sword+3, Shield+1, Armor+4
inventory.drop(SWORD); // SWORD is a constant for item class sword
inventory.displayInventory(); //Shield+1, Armor+4
This is the entire point of OOP, and the entire point of using C++ instead of C.
Edit: As for the internal representation of the inventory system, I would have equiped items stored as a single variable, the items in your 'bags' in a vector, and have a different vector for each item type.
@intrexa - actually, this is my 'downgraded' project lol. What I really want to do is create an AI chat bot, but everyone said 'make something simpler to practice the basics', lol, so I decided to make a simple text adventure. But I understand what you're saying, I'll definitely keep studying then! Just gets SO boring reading chapter after chapter lol. But you're right.
But while I'm here I might as well learn how to make an inventory. Doesn't look too hard. I tried the vector thing, and I'm just getting a ton of errors. I don't like using namespace, Krofna could you write the code without the using namespace? I'm thinking it's:
Sorry for late reply, I forgot to check this topic for while.
Anyway, this is just a rough example. (added std::)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <vector>
#include <string>
#include <iostream>
std::vector<std::string>inventory;
std::vector<std::string>::iterator iter;
int main(){
//add some stuff to "end" of list
inventory.push_back("Axe");
inventory.push_back("Helm");
//see whats in the list
for(iter = inventory.begin(); iter != inventory.end(); iter++)
std::cout << *iter << " ";
}
However you can always modify it to something like this:
#include <vector>
#include <string>
#include <iostream>
struct item
{
std::string name;
int value;//Attack/def value? :)
};
std::vector<item>inventory;
std::vector<item>::iterator iter;
int main(){
//add some stuff to "end" of list
item axe;
axe.name = "Mighty Axe";
axe.value = 5;
item shield;
shield.name = "Huge shield";
shield.value = 10;
inventory.push_back(axe);
inventory.push_back(shield);
//see whats in the list
for(iter = inventory.begin(); iter != inventory.end(); iter++)
std::cout << iter->name << " ";
}
Also it doesnt have to be struct, could be class as well, but for sake of simplicity I made it a struct.
EDIT:
Code would be cleaner if you make them classes and pass arguments to a constructor, instead of that boring item axe; axe.name = "Mighty Axe"; axe.value = 5;