vendredi 6 mars 2020

General constructor value

I just got into c++ since last week and as I'm trying to create a little rpg game, I got stuck in a point where I'd rather abandon the idea of OOP and make many integers and write values for each class.

I tried some ideas that I thought were great, but only errors were to be expected.

The main point is that I made a constructor of a class with the int attack, defense and so on, but to call it, i have to specify the class (eg. wizard.getAttack()).

I have 5 classes when the time comes to attack the enemy, i don't have a solution to input a general attack value of the class that has been chosen (such as battle = attackPlayer - defenseEnemy; or something like that).

It looks like this:

class Player
{
private:
    int attack;
    int defense;
    int evasion;
    string name;
public:
    Player(int atk = 0, int def = 0, int ev = 0, string nameClass = "Wiz")
    {
        attack = atk;
        defense = def;
        evasion = ev;
        name = nameClass;
    }

    int getAttack()
    {
        return attack;
    }

    int getDefense()
    {
        return defense;
    }

    int getEvasion()
    {
        return evasion;
    }
    string getName()
    {
        return name;
    }
};

int choice;
//int battle;

int main()
{
    cout << "Hi what class do you choose?" << endl;
    cin >> choice;


    Player wizard(60, 40, 60, "wizard");
    Player warrior(50, 60, 40, "warrior");
    Player paladin(40, 70, 50, "paladin");
    Player druid(60, 40, 60, "druid");
    Player assassin(70, 30, 70, "assassin");


        switch (choice)
    {
        case 1:
            wizard.getAttack(), wizard.getDefense(), wizard.getEvasion(), wizard.getName();
            break;
        case 2:
            warrior.getAttack(), warrior.getDefense(), warrior.getEvasion(), warrior.getName();
            break;
    }

//    battle = attackPlayer - attackEnemy;

    Player treant (60, 40, 60, "treant");

    cout << "Hello " << choice << endl;


    return 0;
};

I think it's easier with a many integers that could work for each class and each enemy, but if i would have to review the values it would take a lot of time. And as c++ have oop tools, I thought I could use them.

Aucun commentaire:

Enregistrer un commentaire