Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //C++ Vector Iterations and Dynamic Memory: By Ben Bollinger
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <ctime>
- #include <vector>
- using namespace std;
- char askYesNo(string myQuestion);
- char attackDefend(string question);
- string changeWeapon(vector<string> weapon);
- int main()
- {
- //Create weapon choice:
- vector<string> inv;
- inv.push_back("sword");
- inv.push_back("knife");
- inv.push_back("gun");
- //Declare vector iteratiors:
- vector<string>::iterator myIter;
- vector<string>::const_iterator iter;
- cout << "Your Items:\n";
- cout << "-----------\n";
- //List out items:
- for(iter = inv.begin(); iter != inv.end(); iter++)
- {
- cout << *iter << endl;
- }
- cout << "-----------\n";
- //Let user select weapon:
- string userWeapon;
- string change = changeWeapon(inv);
- {
- if(change == "sword")
- {
- cout << "You chose sword\n";
- userWeapon = "sword";
- }else if(change == "knife")
- {
- cout << "You chose knife\n";
- userWeapon = "knife";
- }else if(change == "gun")
- {
- cout << "You chose gun\n";
- userWeapon = "gun";
- }else
- {
- //Set random weapon if user types invalid input:
- srand(static_cast<unsigned int>(time(0))); //Seed random number with time.
- int rndWeapon = rand() % 3 + 1; //Generate number.
- int realNum = rndWeapon;
- //Decision structure:
- if(realNum == 1)
- {
- userWeapon = "sword";
- }else if(realNum == 2)
- {
- userWeapon = "knife";
- }else if(realNum == 3)
- {
- userWeapon = "gun";
- }
- cout << "Invaid Option, your weapon is now: " << userWeapon << "\n";
- }
- }
- //Setup first enemy attack
- char enemy1 = attackDefend("Enemy attacks, do you 'attack' or 'defend'?");
- {
- if(enemy1 == 'a'){
- if(userWeapon == "sword")
- {
- cout << "You perished\n"; //If sword, then death.
- }else if(userWeapon == "knife")
- {
- cout << "You never stood a chance\n"; //If knife, then death.
- }else if(userWeapon == "gun")
- {
- cout << "You killed the enemy\n"; //If gun, then success.
- }
- }else if(enemy1 == 'd'){
- cout << "Your defenses were breached\n"; //If defend, then death.
- }
- }
- //Ask user to create a new weapon:
- char createWeapon = askYesNo("Create a weapon?");
- {
- if (createWeapon == 'y')
- {
- int replaceWeap;
- cout << "What weapon do you want to replace? (1: sword, 2: knife, 3: gun): ";
- cin >> replaceWeap; //Set weapon to replace.
- string newWeap;
- cout << "New weapon name: ";
- cin >> newWeap; //Set name of new weapon.
- //Weapon replace logic:
- myIter = inv.begin() + replaceWeap - 1;
- string oldWeap = *myIter;
- *myIter = newWeap;
- cout << "\n++++++++++++++++++++\n";
- cout << "Successfuly replaced '" << oldWeap << "' with '" << newWeap << "'\n";
- cout << "++++++++++++++++++++\n";
- //Display new item set:
- cout << "\nNew Items: \n";
- cout << "------------\n";
- for(iter = inv.begin(); iter != inv.end(); iter++)
- {
- cout << *iter << endl;
- }
- cout << endl;
- }else
- {
- cout << "Game Over" << endl;
- }
- }
- }
- //Enemy attack function:
- char attackDefend(string question)
- {
- char response;
- cout << question << " (a/d): ";
- cin >> response;
- return response;
- }
- //Change user weapon function:
- string changeWeapon(vector<string> inventory)
- {
- string weapon;
- string choice;
- cout << "Enter what weapon you would like to use: ";
- cin >> choice;
- for(int i = 0; i < inventory.size(); i++)
- {
- if(choice == inventory[i])
- {
- weapon = choice;
- }
- }
- return weapon;
- }
- //Ask a question function:
- char askYesNo(string myQuestion)
- {
- char answer;
- cout << myQuestion << " (y/n): ";
- cin >> answer;
- return answer;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement