Bonjour à tous

J'essaie de faire un mini rpg et j'ai un probleme au niveau de l'initialisation des personnage :

- quand je le fait manuellement // Guerrier Achille; // c'est nickel

- mais quand j'utilise ma fonction creationPersonnage je me retrouve avec des valeurs confondues ( nombrePotionDeVie = Vie ), et ça plante que je veux afficher l'arme, enfin c assez bizarre ....

Si quelqu'un veut bien m'aider il est le bienvenue

main
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include "Personnage.h"
 
using namespace std;
 
int main()
{
    Personnage &x = creationPersonnage();
    Guerrier Achille;
    //Achille.attaquer(x);
    //x.attaquer(Achille);
    x.afficherEtat();
    //x.afficherEtat();
    //Achille.AffVie();
    //x.AffVie();
 
    return 0;
}
Personnage.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef PERSONNAGE_H_INCLUDED
#define PERSONNAGE_H_INCLUDED
 
#include <iostream>
#include <string>
#include "Arme.h"
#include "PouvoirMagique.h"
 
class Personnage
{
    public:
 
    Personnage(int vie, int nombrePotionDeVie, std::string nomArme, int degatsArme);
    void recevoirDegats(int nbDegats);
    void attaquer(Personnage &cible);
    void afficherEtat() const;
    void boirePotionDeVie();
    //void jouerAutomatiquement();
    void AffVie() const;
 
    protected:
 
    int m_vie, m_vieMax;
    int m_nombrePotionDeVie, m_nombrePotionDeVieMax;
    std::string m_nom;
    Arme m_arme;
};
 
 
class Guerrier : public Personnage
{
    public:
 
    Guerrier();
 
    protected:
};
 
 
class Mage : public Personnage
{
    public:
 
    Mage();
    void lancerSort(Personnage &cible);
    void boirePotionDeMana();
    void afficherEtat() const;
 
    protected:
 
    int m_mana, m_manaMax;
    int m_nombrePotionDeMana, m_nombrePotionDeManaMax;
    int m_regMana;
    PouvoirMagique m_pouvoirMagique;
};
 
 
class Tank : public Personnage
{
    public:
 
    Tank();
 
    protected:
};
 
Personnage& creationPersonnage();
 
#endif // PERSONNAGE_H_INCLUDED
Personnage.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "Personnage.h"
#include "Arme.h"
#include <iostream>
#include <string>
 
using namespace std;
 
 
Personnage::Personnage(int vie, int nombrePotionDeVie, string nomArme, int degatsArme): m_vieMax(vie), m_nombrePotionDeVieMax(nombrePotionDeVie), m_arme(nomArme, degatsArme)
{
    m_vie = vie;
    m_nombrePotionDeVie = nombrePotionDeVie;
}
 
Guerrier::Guerrier(): Personnage(150, 1, "Epee en fer", 40)
{
 
}
 
Mage::Mage(): Personnage(120, 1, "Epee en bois", 15), m_manaMax(150), m_nombrePotionDeManaMax(3), m_regMana(30), m_pouvoirMagique("Boule de feu", 45, 90)
{
    m_mana = m_manaMax;
    m_nombrePotionDeMana = m_nombrePotionDeManaMax;
}
 
Tank::Tank(): Personnage(220, 3, "Epee en bois", 15)
{
 
}
 
void Personnage::recevoirDegats(int nombreDegats)
{
    m_vie -= nombreDegats;
    if(m_vie < 0)
    {
        m_vie = 0;
    }
}
 
void Personnage::attaquer(Personnage &cible)
{
    cible.recevoirDegats(m_arme.getDegats());
}
 
void Personnage::afficherEtat() const
{
 
    cout << "Vie : " << m_vie << " (Vie max : " << m_vieMax << ")" << endl;
    cout << "Nombre de potion de vie : " << m_nombrePotionDeVie << " (Nombre de potion de vie max : " << m_nombrePotionDeVieMax << ")" << endl;
    m_arme.afficher();
    cout << endl;
}
 
void Personnage::boirePotionDeVie()
{
    if(m_nombrePotionDeVie > 0 && m_vieMax > m_vie + 30)
    {
        m_vie += 30;
        m_nombrePotionDeVie--;
    }
    else if(m_vieMax < m_vie + 30)
    {
        m_vie = m_vieMax;
    }
    else if(m_nombrePotionDeVie <= 0)
    {
        cout << "Vous n'avez plus de potion de vie ;(" << endl;
    }
}
 
void Mage::boirePotionDeMana()
{
    if(m_nombrePotionDeMana > 0 && m_manaMax > m_mana + 30)
    {
        m_mana += 30;
        m_nombrePotionDeMana--;
    }
    else if(m_manaMax < m_mana + 30)
    {
        m_mana = m_manaMax;
    }
    else if(m_nombrePotionDeMana < 0)
    {
        cout << "Vous n'avez plus de potion de mana ;(" << endl;
    }
}
 
void Mage::afficherEtat() const
{
    cout << "Vie : " << m_vie << " (Vie max : " << m_vieMax << ")" << endl;
    cout << "Mana : " << m_mana << " (Mana max : " << m_manaMax << ")" << endl;
   // m_arme.afficher();
    cout << endl;
}
 
void Mage::lancerSort(Personnage &cible)
{
    if(m_mana > m_pouvoirMagique.getCoutMana())
    {
        cible.recevoirDegats(m_pouvoirMagique.getDegats());
        m_mana -= m_pouvoirMagique.getCoutMana();
    }
    else
    {
        cout << "Vous n'avez plus assez de mana ! (Mana : " << m_mana << ")" << endl;
    }
}
 
/*void Personnage::jouerAutomatiquement()
{
    int x;
    string nomClasse;
    string *nomPersonnage;
    int PersonnageInt;
    do
    {
        cout << "Voulez-vous jouer seul (1) ou a plusieurs (2) ?"<< endl;
        cin >> x;
    }while(x != 1 || 2);
    if(x == 1)
    {
        do
        {
        cout << "Quelle classe desirez-vous prendre parmi Guerrier, Mage, et Tank ?" << endl;
        cin >> nomClasse;
        } while(nomClasse != "Guerrier" || "Mage" || "Tank");
        cout << "Quelle est le nom de votre " << nomClasse << " ?" << endl;
        cin >> *nomPersonnage;
        if(nomClasse == "Guerrier")
        {PersonnageInt = 0;}
        else if(nomClasse == "Mage")
        {PersonnageInt = 1;}
        else
        {PersonnageInt = 2;}
        switch(PersonnageInt)
        {
            case 0:
                Guerrier *nomPersonnage;
                break;
            case 1:
 
                break;
            case 2:
 
                break;
        }
 
    }
    else
    {
 
    }
}*/
 
Personnage& creationPersonnage()
{
    string nomClasse;
    string nomPersonnage;
    string *pointeurNomPersonnage = &nomPersonnage;
    do
    {
    cout << "Quelle classe desirez-vous prendre parmi Guerrier, Mage ou Tank ?" << endl;
    cin >> nomClasse;
    }while(nomClasse != "Guerrier" && nomClasse != "Mage" && nomClasse != "Tank");
    cout << "Quel est le nom de votre " << nomClasse << " ?" << endl;
    cin >> nomPersonnage;
    if(nomClasse == "Guerrier")
    {
        Guerrier nomPersonnage;
        cout << "Vous venez de creer le " << nomClasse << " " << *pointeurNomPersonnage << " !" << endl;
        return nomPersonnage;
    }
    else if(nomClasse == "Mage")
    {
        Mage nomPersonnage;
        cout << "Vous venez de creer le " << nomClasse << " " << *pointeurNomPersonnage << " !" << endl;
        return nomPersonnage;
    }
    else
    {
        Tank nomPersonnage;
        cout << "Vous venez de creer le " << nomClasse << " " << *pointeurNomPersonnage << " !" << endl;
        return nomPersonnage;
    }
}
 
void Personnage::AffVie() const
{
    cout << m_vie << endl;
    cout << m_vieMax << endl;
    m_arme.afficher();
}
Arme.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef ARME_H_INCLUDED
#define ARME_H_INCLUDED
 
#include <iostream>
#include <string>
 
class Arme
{
    public:
 
    Arme();
    Arme(std::string nom, int degats);
    void changer(std::string nom, int degats);
    void afficher() const;
    int getDegats() const;
 
    protected:
 
    std::string m_nom;
    int m_degats;
    std::string m_classe;
};
 
 
#endif // ARME_H_INCLUDED
Arme.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "Arme.h"
 
using namespace std;
 
 
Arme::Arme(string nom, int degats): m_nom(nom), m_degats(degats)
{
 
}
 
void Arme::changer(string nom, int degats)
{
    m_nom = nom;
    m_degats = degats;
}
 
void Arme::afficher() const
{
    cout << "Arme : " << m_nom << " (degats : " << m_degats << ")" << endl;
}
 
int Arme::getDegats() const
{
    return m_degats;
}
PouvoirMagique.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef POUVOIRMAGIQUE_H_INCLUDED
#define POUVOIRMAGIQUE_H_INCLUDED
 
#include <iostream>
#include <string>
 
class PouvoirMagique
{
    public:
 
    PouvoirMagique(std::string nom, int degats, int coutMana);
    int getDegats() const;
    int getCoutMana() const;
 
 
    protected:
 
    std::string m_nom;
    int m_degats;
    int m_coutMana;
};
 
#endif // POUVOIRMAGIQUE_H_INCLUDED
PouvoirMagique.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "PouvoirMagique.h"
 
using namespace std;
 
 
PouvoirMagique::PouvoirMagique(std::string nom, int degats, int coutMana): m_nom(nom), m_degats(degats), m_coutMana(coutMana)
{
 
}
 
int PouvoirMagique::getDegats() const
{
    return m_degats;
}
 
int PouvoirMagique::getCoutMana() const
{
    return m_coutMana;
}