mercredi 29 septembre 2021

How to generate a random string from a vector? [duplicate]

I am trying to come up with a way to take one random string out of my vector element and failed. It keeps printing out Mutant string over and over.

What am I doing wrong?

#include <iostream>
#include <stdlib.h>
#include <vector>

class Game {
public:
    Game(int attack, int health, int potions, int enemyAttack, int enemyHealth) {
        Attack = attack;
        Health = health;
        Potions = potions;
        EnemyAttack = enemyAttack;
        EnemyHealth = enemyHealth;
    }

    std::string getEnemy() {
        return Enemy[rand() % Enemy.size()];
    }

    void setPlayerName() {
        std::cout << "Type in your name: ";
        std::cin >> Name;
    }

    std::string getPlayerName() {
        return Name;
    }

     int attackEnemy() {
        srand(time(0));

        return EnemyHealth = EnemyHealth - rand() % Attack; }

     int getEnemyHealth() {
         return EnemyHealth;
     }

private:
    std::vector <std::string> Enemy = { "Orc","Mutant","Mutant elephant","Bear" };

    std::string Name;

    int Attack;
    int Health;
    int Potions;

    int EnemyAttack;
    int EnemyHealth;

};

int main() {
    Game begin = Game(10,100,2,5,40);

//  begin.setPlayerName();

    std::cout << begin.getEnemy();
    

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire