*Its only a portion of the code due to the character limit*
I need to add these following variables but have no idea how to:
input the theme of their “favorites” (NO hard-coded variables!).
display a list of all items,
add new item(s),
replace an item with something different,
remove any item from the list (not just the last one!),
assign a value to the favorites to determine total worth (ie. food – calories, books – value, etc.)
Display “favorites” worth at end when User exits game.
// Hero's Inventory
// Demonstrates Arrays
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int score = 100;
vector<string> inventory;
inventory.push_back("Sword");
inventory.push_back("Dagger");
inventory.push_back("Poison Dart");
inventory.push_back("Bow and Arrow");
inventory.push_back("Pistol");
inventory.push_back("Grenade");
inventory.push_back("Poisonous Gas");
inventory.push_back("Choker");
inventory.push_back("Crossbow");
cout << "\t\tWelcome to Assassin's Creed Story Mode\n" << endl;
cout << "\n You are a young time-traveling assassin that has traveled back to 3100 B.C. \n You have orders to kill the Pharoh Narmar before he releases the genocide that wipes out Africa\n The ransom money is half of the Pharoh's wealth(alot)" << endl;
cout << "\nRules: \n You begin with a score of 100 and certain tasks can either add or take away from your score and once you reach 600 you win and kill the Pharoh\n" << endl;
cout << "\nPress Enter To Continue... " << endl;
int reset =1;
do {
cin.ignore();
cout << " Your weapons are: \n";
for (int i = 0; i < numItems; ++i)
{
cout << "\t" << inventory[i] << endl;
}
int SackReset = 1;
do {
cout << "\nYou are walking along the city of Egypt, scoping out the land";
cout << "\nAs You're walking you find a burlap sack, do you open it? [Y/N] ";
std:: getline (std:: cin, OpenSack);
if ( OpenSack == "Y" || OpenSack == "y")
{
SackReset = 0;
cout << "\nAs you open it, it seems that a battle axe in it.\n";
cout << "\nWould You like to Trade Your battle axe for a poisonous gas? [Y/N] ";
cin >> userResponse;
if (userResponse == 'Y')
{
inventory[6] = "Battle Axe";
cout << "\n\nYour Weapons:\n";
score = score +50;
for (int i = 0; i < numItems; ++i)
{
cout << inventory[i] << endl;
}
}
if (userResponse == 'y')
{
inventory[6] = "Battle Axe";
cout << "\n\nYour Weapons:\n";
score = score +50;
for (int i = 0; i < numItems; ++i)
{
cout << inventory[i] << endl;
}
}
else if (userResponse == 'N')
{
cout << "\nYou have choosen wrongly, you have lost 50 points.";
cout << "\nYou have passed on a very important tool." << endl;
cout << "\n Press Enter to continue... " << endl;
score = score -50;
}
}
else if (OpenSack == "n")
{
SackReset = 0;
cout << "\nYou have choosen wrongly, you have lost 50 points.\n";
cout << "You have passed on a very important tool." << endl;
score = score -50;
}
else
{
cout << "\n\n You did not choose a proper choice, try again." << endl;
SackReset = 1;
}
cout << "\n\n";
} while (SackReset !=0);
cin.ignore();
cout << "\n\nAs you are walking an armed servant of the Pharoh runs towards you" << endl;
cout << "You pull out your " << inventory[5] << " and do which of the following?" << endl;
cout << "Do you Run or Fight the dangerous servant?[R/F]" << endl;
std:: getline (std:: cin, fightOrFlight);
if ( fightOrFlight == "f" || fightOrFlight == "F")
{
cout << "\nAs you fight, you seem to be winning! \n";
cout << "\nDespite all odds You have won this fight.\n";
cout << "\nWould you like to change your weapon, so you could fight with something else? [Y/N]" << endl;
std:: getline (std:: cin, OpenSack);
if (OpenSack == "Y" || OpenSack == "y")
{
cout << "Which would you like to replace? ";
std:: getline (std:: cin, inventoryReplace);
if (inventoryReplace == "Sword" || inventoryReplace == "sword")
{
cout << "What would you like to replace it with? ";
std:: getline (std:: cin, inventoryReplace2);
inventory[0] = inventoryReplace2;
cout << "\nSword has been replace with, " << inventoryReplace2 << endl;
score = score +200;
}
else if (inventoryReplace == "Dagger" || inventoryReplace == "dagger")
{
cout << "What would you like to replace it with? ";
std:: getline (std:: cin, inventoryReplace2);
inventory[1] = inventoryReplace2;
cout << "\nDagger has been replace with, " << inventoryReplace2 << endl;
score = score +250;
}
else if (inventoryReplace == "Poison Dart" || inventoryReplace == "poison dart")
{
cout << "What would you like to replace it with? ";
std:: getline (std:: cin, inventoryReplace2);
inventory[2] = inventoryReplace2;
cout << "\nPoison Dart has been replace with, " << inventoryReplace2 << endl;
score = score +520;
}
else if (inventoryReplace == "Bow and Arrow" || inventoryReplace == "bow and arrow")
{
cout << "What would you like to replace it with? ";
std:: getline (std:: cin, inventoryReplace2);
inventory[3] = inventoryReplace2;
cout << "\nBow and Arrow has been replace with, " << inventoryReplace2 << endl;
score = score +500;
}
else if (inventoryReplace == "Pistol" || inventoryReplace == "pistol")
{
cout << "What would you like to replace it with? ";
std:: getline (std:: cin, inventoryReplace2);
inventory[4] = inventoryReplace2;
cout << "\nPistol has been replace with, " << inventoryReplace2 << endl;
score = score +5000;
}
else if (inventoryReplace == "Grenade" || inventoryReplace == "grenade")
{
cout << "What would you like to replace it with? ";
std:: getline (std:: cin, inventoryReplace2);
inventory[5] = inventoryReplace2;
cout << "\nGrenade has been replace with, " << inventoryReplace2 << endl;
score = score +510;
}
else if (inventoryReplace == "Poisonous Gas" || inventoryReplace == "Battle Axe")
{
cout << "What would you like to replace it with? ";
std:: getline (std:: cin, inventoryReplace2);
inventory[6] = inventoryReplace2;
cout << "\nYour choice has been replace with, " << inventoryReplace2 << endl;
score = score +500;
You need to generalize the replacement code a little. It should work like this:
- ask the user which item they want to replace.
- search the inventory array for the item they mentioned If you find it, then remember the index in the array (I'll call it x). If you don't find it then print a message to the user.
- assuming you found it, now ask what they want to replace it with.
- change the inventory[x] to be the new item.
Note that you should probably verify that the item they want to replace it with is valid. For example, what if they say they want to replace the battle axe with "large pizza extra cheese"? To fix this, you should keep a collection of all the valid items. Note that this is different from the inventory, which is just the items that the user currently has.
You also need to adjust the score. That immediately suggests that your collection of valid items should contain both the name and the value of each item. That way you could look up the value of the old item and new items. This would let you adjust the user's total score.
But what variables do I have to use? Do I have to add more strings or inventory.push_backs? And how do I add the "favorites" section? Sorry, I'm very new at coding.