CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Oct 2013
    Posts
    5

    C++ help with random shuffle and leveling

    Hello everyone!

    I was wondering if anyone would be willing to help me with a program I am working on. I am trying to add different levels in the below program. When the player reaches level 3, I would like to make him face a Dragon to save the princess. I am using random shuffle to pick the enemy for each fight but is there a way to bypass random shuffle at level 3 and force the battle to be against a dragon.
    Can anyone give me a suggestion on how to do this?
    Also any advice for improvements would be greatly appreciated.
    Thank you in advance.
    Code:
    #include<iostream>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<ctime>
    #include<cstdlib>
    using namespace std;
    int main()
    {vector<string> inventory;       //inventory of weapons
    	inventory.push_back("sword");
    	inventory.push_back("bow");
    	inventory.push_back("wand");
    	inventory.push_back("dagger");
    	vector<string> player;          //player options
    	player.push_back(" ");          //empty place holder to move character names into the #1-4 spot to match enum values
    	player.push_back("Knight");
    	player.push_back("Archer");
    	player.push_back("Sorcerer");
    	player.push_back("Elf");
    	vector<string> foe;             //foe options
    	foe.push_back("Dragon");
    	foe.push_back("Troll");
    	foe.push_back("Worlock");
    	foe.push_back("Fairy");
    	vector<string> outcome;
    	outcome.push_back("You are wounded and you lose 10 health points and 5 power points");
    	outcome.push_back("You have killed your enemy and you gain 10 health points and 5 power points");
    	outcome.push_back("You escaped unharmed and you gain 10 health points and 5 power points");
    	outcome.push_back("You are missing a limb you lose 20 health points and 10 power points");
    	enum Player{Knight=1, Archer, Sorcerer, Elf};   //enum to give the words integer assignments
    	enum Action{Rest=1, Move, Fight, Run, viewStats=4, Quit=9};
    	Player myPlayer;     //assign specific variable to my enums        
    	Action myAction;
    	int health = 50;
    	int power = 25;
    	int Level = 0;
    	int XP = 0;
    	int choice;
    	string name;  //assigned for user name input
    	cout<<"\tYour princess has been taken by an evil witch.\n";
    	cout<<"You must travel across the land through the forest,";
    	cout<<"into the caves, and over the marshlands to save her.\n\n";
    	cout<<"What name do you go by?  ";
    	cin>>name;  //user input character name
    	cout<<"\n\nVery well "<<name<<" , what type of adventurer are you?\n\n";
    	cout<<"Please select your character type: 1 - Knight\n";
    	cout<<"                                   2 - Archer\n";
    	cout<<"                                   3 - Sorcerer\n";
    	cout<<"                                   4 - Elf\n";
    	cin>>choice;    //user input character persona
    	bool success;   //test for condition in switch/case
    	myPlayer=Player(choice);  //assign user choice to enum variable
    	do       //do loop to execute user choice of character persona
    	{switch (myPlayer)
    		{case Knight:	
    	cout<<"You are a brave Knight, "<<name<<".";
    	myPlayer==Knight;
    	success=true;
    	break;
    	case Archer:
    	cout<<"You are a deadly Archer, "<<name<<".";
    	success=true;
    	break;
    	case Sorcerer:
    	cout<<"Your magic is very powerful, Sorcerer "<<name<<".";
    	success=true;
    	break;
    	case Elf:
    	cout<<"You are a sneaky Elf, "<<name<<".";
    	success=true;
    	break;
    default:
    success=false;
    cout<<"Please choose another adventurer.";
    cin>>choice;
    }
    }while (success==false);	
    srand(static_cast<unsigned int>(time(0)));                //randomly selects weapon from inventory
    random_shuffle(inventory.begin(), inventory.end());
    const string WEAPON = inventory[0];
    cout<<"\n\nYour weapon for the journey is a "<<WEAPON<<".";
    cout<<"\n\n\t\tStart of Game:\n\n";
    cout<<"Name:"<<name;
    cout<<"\nAdventurer: "<<player[myPlayer];
    cout<<"\nWeapon: "<<WEAPON;
    cout<<"\nHealth: "<<health;
    cout<<"\nPower: "<<power;
    cout<<"\nLevel: "<<Level;
    cout<<"\nHealth: "<<XP<<"\n\n";
    int move;
    cout<<"What would you like to do first?\n\n";       //user chooses first action
    cout<<"                               1 - Rest\n";
    cout<<"                               2 - Move\n";
    cout<<"                               3 - Fight\n";
    cout<<"                               4 - View Stats\n";
    cout<<"                               9 - Quit\n";
    cin>>move;                               
    myAction = Action(move);  //assigns specific action to enum variable
    int select; //declaring select as int variable for switch loop
    string STRANGER;
    string FIGHT;
    string RESULT;
    bool terminate = false;
    while (!terminate){
    random_shuffle(foe.begin(), foe.end());    //randomly selects foe to interact with
    string STRANGER = foe[0];
    random_shuffle(foe.begin(), foe.end());
    string FIGHT = foe[0];
    random_shuffle(outcome.begin(), outcome.end());    //randomly selects foe to interact with
    string RESULT = outcome[0];
    switch(myAction)    //output of user choice of action
    {
    case Rest:
    cout<<"\nTime to rest and build your strength.";
    health +=10;
    power +=15;
    success=true;
    cout<<"\n\nWhat would you like to do next?\n";
    cout<<"\n                                1-Fight";
    cout<<"\n                                2-Run";
    cout<<"\n                                3-Quit\n";
    cout<<"                                  4-View Stats\n";
    cin>>select;
    if (select==1)  //displays consequence of user secondary choice of action
    {
    cout<<"\n"<<RESULT<<" . \n";
    cout<<"You lose 10 Health points and 5 Power points.";*/
    power-=5;*/
    if (RESULT=="You are wounded and you lose 10 health points and 5 power points")
    {
    health-=10;
    power-=5;
    if (health==80)
    {
    cout<<"\nYou have reached Level 1\n";
    Level+=1;
    }
    else if (health==120) 
    {
    cout<<"\nYou have reached Level 2\n";
    Level+=1;
    }
    else if (health==130) 
    {
    cout<<"\nYou have saved the princess 2\n";
    Level+=1;
    }
    }
    else if (RESULT=="You escaped unharmed and you gain 10 health points and 5 power points")
    {
    health+=10;
    power+=5;
    if (health==80)
    {
    cout<<"\nYou have reached Level 1\n";
    Level+=1;
    }
    else if (health==120) 
    {
    cout<<"\nYou have reached Level 2\n";
    Level+=1;
    }
    else if (health==130) 
    {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    							else if (RESULT=="You have killed your enemy and you gain 10 health points and 5 power points")
    							{
    							health+=10;
    							power+=5;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    							else if (RESULT=="You are missing a limb you lose 20 health points and 10 power points")
    							{
    							health-=20;
    							power-=10;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    					}
    					else if(select==2)
    						{
    							cout<<"You have escaped to travel on, but you have used a lot of energy.\n\n";
    							cout<<"Your Power points are decreased by 10.";
    							power-=10;
    						}
    					else if(select==3)
    						{
    							cout<<"Your journey has ended.";
    							terminate = true;
    						}
    					else if(select==4)
    						{
    							
    				cout<<"Name:"<<name;
    				cout<<"\nAdventurer: "<<player[myPlayer];
    				cout<<"\nWeapon: "<<WEAPON;
    				cout<<"\nHealth: "<<health;
    				cout<<"\nPower: "<<power;
    				cout<<"\nLevel: "<<Level;
    				cout<<"\nHealth: "<<XP<<"\n\n";
    				cout<<"\n\nWhat would you like to do next?\n";
    				cout<<"\n                                1-Fight";
    				cout<<"\n                                2-Run";
    				cout<<"\n                                3-Quit\n";
    				cout<<"                                  4-View Stats\n";
    				cin>>select;
    						}
    					break;
    
    			case Move://output of user choice of action
    			
    			cout<<"\nYou see a "<<STRANGER<<" . ";
    					if(STRANGER=="Troll")
    						{
    							cout<<"It blocks your path.\n";
    							cout<<"\n\nWhat would you like to do next?\n\n";
    							cout<<"                                1-Fight\n";
    							cout<<"                                2-Run\n";
    							cout<<"                                3-Quit\n";
    							cout<<"                                4-View Stats\n";
    						}
    					else if(STRANGER=="Dragon")
    						{
    							cout<<"It is coming towards you.\n\n";
    							cout<<"\n\nWhat would you like to do next?\n\n";
    							cout<<"                                1-Fight\n";
    							cout<<"                                2-Run\n";
    							cout<<"                                3-Quit\n";
    							cout<<"                                4-View Stats\n";
    						}
    					else if(STRANGER=="Worlock")
    						{
    							cout<<"He is picking herbs.\n\n";
    							cout<<"\n\nWhat would you like to do next?\n\n";
    							cout<<"                                1-Fight\n";
    							cout<<"                                2-Run\n";
    							cout<<"                                3-Quit\n";
    							cout<<"                                4-View Stats\n";
    						}
    					else if(STRANGER=="Fairy")
    						{
    							cout<<"She is singing a song.\n\n";
    							cout<<"\n\nWhat would you like to do next?\n\n";
    							cout<<"                                1-Fight\n";
    							cout<<"                                2-Run\n";
    							cout<<"                                3-Quit\n";
    							cout<<"                                4-View Stats\n";
    						}
    			cin>>select;
    					if (select==1)//displays consequence of user secondary choice of action
    						{
    							cout<<"\n"<<RESULT<<" . ";
    														/*cout<<"You have been wounded.\n\n";
    							cout<<"You lose 10 Health points and 5 Power points.";
    							health-=10;
    							power-=5;*/
    						if (RESULT=="You are wounded and you lose 10 health points and 5 power points")
    							{
    							health-=10;
    							power-=5;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    							}
    							else if (RESULT=="You escaped unharmed and you gain 10 health points and 5 power points")
    							{
    							health+=10;
    							power+=5;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    							else if (RESULT=="You have killed your enemy and you gain 10 health points and 5 power points")
    							{
    							health+=10;
    							power+=5;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    							else if (RESULT=="You are missing a limb you lose 20 health points and 10 power points")
    							{
    							health-=20;
    							power-=10;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    					}
    					else if(select==2)
    						{
    							cout<<"You have escaped to travel on, but you have used a lot of energy.\n\n";
    							cout<<"Your Power points are decreased by 10.";
    							power-=10;
    						}
    					else if(select==3)
    						{
    							cout<<"Your journey has ended.";
    							terminate = true;
    						}
    					else if(select==4)
    						{
    						
    				cout<<"Name:"<<name;
    				cout<<"\nAdventurer: "<<player[myPlayer];
    				cout<<"\nWeapon: "<<WEAPON;
    				cout<<"\nHealth: "<<health;
    				cout<<"\nPower: "<<power;
    				cout<<"\nLevel: "<<Level;
    				cout<<"\nHealth: "<<XP<<"\n\n";
    				cout<<"\n\nWhat would you like to do next?\n";
    				cout<<"\n                                1-Fight";
    				cout<<"\n                                2-Run";
    				cout<<"\n                                3-Quit\n";
    				cout<<"                                  4-View Stats\n";
    				cin>>select;
    					}
    					success=false;
    			
    					break;
    					
    
    			case Fight://output of user choice of action
    			
    			cout<<"You fought a "<<FIGHT<<" . ";
    					if(FIGHT=="Troll")
    						{
    							cout<<"\n\nYou have been wounded.\n\n";
    							cout<<"You lose 10 Health points and 5 Power points.";
    							health-=10;
    							power-=5;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    					else if(FIGHT=="Dragon")
    						{
    							cout<<"\n\nYou have slain the Dragon!  You gain 20 Power points!";
    							power+=20;
    							Level+=1;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    					else if(FIGHT=="Worlock")
    						{
    							cout<<"\n\nYou have acquired a healing potion. You gain 20 Health points.";
    							health+=20;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    					else if(FIGHT=="Fairy")
    						{
    							cout<<"\n\nShe put you in a deep sleep. You lose 25 Health points.";
    							health-=25;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    					success=true;
    
    			cout<<"\n\nWhat would you like to do next?\n\n";
    			cout<<"                                1-Fight\n";
    			cout<<"                                2-Run\n";
    			cout<<"                                3-Quit\n";
    			cout<<"                                4-View Stats\n";
    			cin>>select;
    					if (select==1)//displays consequence of user secondary choice of action
    						{
    							cout<<"\n"<<RESULT<<" . ";
    							
    							/*cout<<"You have been wounded.\n\n";
    							cout<<"You lose 10 Health points and 5 Power points.";
    							health-=10;
    							power-=5;*/
    						if (RESULT=="You are wounded and you lose 10 health points and 5 power points")
    							{
    							health-=10;
    							power-=5;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    							}
    							else if (RESULT=="You escaped unharmed and you gain 10 health points and 5 power points")
    							{
    							health+=10;
    							power+=5;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    							else if (RESULT=="You have killed your enemy and you gain 10 health points and 5 power points")
    							{
    							health+=10;
    							power+=5;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    							else if (RESULT=="You are missing a limb you lose 20 health points and 10 power points")
    							{
    							health-=20;
    							power-=10;
    							if (health==80)
    							{
    							cout<<"\nYou have reached Level 1\n";
    							Level+=1;
    							}
    							else if (health==120) 
                                {
    							cout<<"\nYou have reached Level 2\n";
    							Level+=1;
    							}
    							else if (health==130) 
                                {
    							cout<<"\nYou have saved the princess 2\n";
    							Level+=1;
    							}
    						}
    					}
    					else if(select==2)
    						{
    							cout<<"You have escaped to travel on, but you have used a lot of energy.\n\n";
    							cout<<"Your Power points are decreased by 10.";
    							power-=10;
    						}
    					else if(select==3)
    						{
    							cout<<"Your journey has ended.";
    							terminate = true;
    						}
    					else if(select==4)
    						{
    							
    				cout<<"Name:"<<name;
    				cout<<"\nAdventurer: "<<player[myPlayer];
    				cout<<"\nWeapon: "<<WEAPON;
    				cout<<"\nHealth: "<<health;
    				cout<<"\nPower: "<<power;
    				cout<<"\nLevel: "<<Level;
    				cout<<"\nHealth: "<<XP<<"\n\n";
    				cout<<"\n\nWhat would you like to do next?\n";
    				cout<<"\n                                1-Fight";
    				cout<<"\n                                2-Run";
    				cout<<"\n                                3-Quit\n";
    				cin>>select;
    					}
    					break;
    
    					default:
    							success=false;
    					break;
    		}
    		}
    		
    		cout<<"\n\nThanks for playing.";   //displays final data to user
    		cout<<"\n\nYour final score:\n";
    		cout<<"Name: "<<name;
    		cout<<"\nAdventurer: "<<player[myPlayer];
    		cout<<"\nWeapon: "<<WEAPON;
    		cout<<"\nHealth: "<<health;
    	    cout<<"\nPower: "<<power;
    	    cout<<"\nLevel: "<<Level;
    	    cout<<"\nXP: "<<XP<<"\n\n";
    
    	int pause;
    	cin>>pause;
    
    	return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: C++ help with random shuffle and leveling

    As posted, your code is just about unreadable as it is not indented properly. Please make sure it is formatted before posting.

    You are randomly shuffling the vector foe and selecting the first element of the shuffled vector each time for your STRANGER, FIGHT and RESULT variables. You have a variable Level which is the current level of the player. All you need is a simple test of Level before you shuffle/assign. Something like

    Code:
    string FIGHT;
    if (Level == 3)
         FIGHT = "Dragon";
    else {
         random_shuffle(foe.begin(), foe.end());
         FIGHT = foe[0];
    }
    The same concept can be applied to STRANGER and RESULT and the tests can be extended to cover other specific circumstances.

    Later in the program, you then test for "Dragon" as well as the other foes. As Dragon isn't in the default foe list, it won't get selected by chance on the shuffle.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured