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
/*
 * Personnage.h
 *
 *  Created on: 11 déc. 2020
 *      Author: vincent
 */
 
#ifndef PERSONNAGE_H_
#define PERSONNAGE_H_
#include <iostream>
#include <string>
#include "Arme.h"
 
class Personnage
{
 
public:
	Personnage();
	Personnage(std::string monArme, int degatsArme);
	~Personnage();
	void recevoirDegats(int nbDegats);
	void attaquer(Personnage &cible);
	void boirePotionDeVie (int quantitePotion);
	void changerArme(std::string nomNouvelleArme, int degatsNouvelleArme);
	void coupDePoing (Personnage &cilble) const;
	bool estVivant() const;
	void afficherEtat() const;
private:
	int m_vie;
	int m_mana;
	Arme *m_arme; //pointeur
	std::string m_nom;
};
 
#endif /* 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Personnage.cpp
 *
 *  Created on: 11 déc. 2020
 *      Author: vincent
 */
 
#include "Personnage.h"
 
using namespace std;
void Personnage::recevoirDegats(int nbDegats)
{
	m_vie -= nbDegats;
if (m_vie < 0)
	{
		m_vie = 0;
	}
}
int Arme::getDegats() const
{
	return m_degats;
}
 
void Personnage::attaquer(Personnage &cible)
{
	cible.recevoirDegats(m_arme->getDegats());
}
void Personnage::boirePotionDeVie (int quantitePotion)
{
	m_vie += quantitePotion;
	if (m_vie > 100)
	{
	m_vie = 100;
	}
}
void Personnage::changerArme(string nomNouvelleArme, int degatsNouvelleArme)
{
	m_arme->changer(nomNouvelleArme, degatsNouvelleArme);
	//pas dans le bouquin demander par le compilateur
	//m_nomArme = nomNouvelleArme;
	//m_degatsArme = degatsNouvelleArme;
}
bool Personnage::estVivant() const
{
	if (m_vie > 0)
		{
			return true;
		}
	else
		{
			return false;
		}
}
Personnage::Personnage() :m_vie(100),m_mana(100),m_arme(0)
{
	 m_arme = new Arme();
 
}
Personnage::Personnage(string nomArme, int degatsArme): m_vie(100), m_mana(100),m_arme(0)
{	//nomArme, degatsArme
	m_arme = new Arme(nomArme,degatsArme );
}
Personnage::Personnage () : m_vie (100), m_nom("jack"),m_mana(100),m_arme(0)
{
}
void Personnage::recevoirDegats(int degats)
{
	m_vie -= degats;
}
void Personnage::coupDePoing(Personnage &cible)const
{
	cible.recevoirDegats(10);
}
void Personnage::afficherEtat() const
{
   cout << "Vie : " << m_vie << endl;
    cout << "Mana : " << m_mana << endl;
    m_arme->afficher();
}
 
Personnage::~Personnage()
{
delete m_arme;
}
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
/*
 * Arme.cpp
 *
 *  Created on: 11 déc. 2020
 *      Author: vincent
 */
 
#include "Arme.h"
using namespace std;
 
Arme::Arme() : m_nom ("Epée rouillée"),m_degats (10)
{
}
 
 
 
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;
}
Arme::~Arme()
{
 
}
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
/*
 * Arme.h
 *
 *  Created on: 11 déc. 2020
 *      Author: vincent
 */
 
#ifndef ARME_H_
#define ARME_H_
#include <iostream>
#include <string>
class Arme {
 
public:
	Arme();
 
 
		~Arme();
		Arme(std::string nom, int degats);
		void changer(std::string nom, int degats);
		void afficher() const;
		int getDegats() const;
 
 
private :
	std::string m_nom;
	int m_degats;
 
};
 
#endif /* 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
26
27
28
#include <iostream>
#include <string>
#include "Personnage.h"
 
using namespace std;
 
 
int main()
{
    // Création des personnages
    Personnage david, goliath("Epée aiguisée", 20);
 
    // Au combat !
    goliath.attaquer(david);
    david.boirePotionDeVie(20);
    goliath.attaquer(david);
    david.attaquer(goliath);
    goliath.changerArme("Double hache tranchante vénéneuse de la mort", 40);
    goliath.attaquer(david);
 
    // Temps mort ! Voyons voir la vie de chacun...
    cout << "David" << endl;
    david.afficherEtat();
    cout << endl << "Goliath" << endl;
    goliath.afficherEtat();
 
    return 0;
}
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
/*
 * Guerrier.h
 *
 *  Created on: 4 févr. 2021
 *      Author: vincent
 */
 
#ifndef GUERRIER_H_
#define GUERRIER_H_
#include <iostream>
#include <string>
#include "Personnage.h"
class Guerrier : public Personnage
{
public:
	void frapper () const ;
};
 
#endif /* GUERRIER_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
 
/*
 * Guerrier.cpp
 *
 *  Created on: 4 févr. 2021
 *      Author: vincent
 */
 
#include "Guerrier.h"
 
 
Guerrier::Guerrier() public Personnage : m_vie (100), m_nom("jack"),m_mana(100),m_arme(0)
{
	m_arme = new Arme();
 
}
Guerrier::~Guerrier() {
	// TODO Auto-generated destructor stub
}
Comment implémenter le fichier Guerrier.cpp ? J' ai essayer plusieurs choses sans y arriver