bonsoir ,
j'espère vous embéter pour la dernière fois avec ce projet mais la g tout recommencer proprement (enfin plus proprement )
je m'explique g une classe faune qui utilise une classe animal et vice-verca ,
pour éviter les problèmes d'inclusion cyclique g utilisé les forward déclaration sur vos conseil avisés , mais g toujours des problèmes car le compilateur me sort des références non définis sur toutes les méthodes d'animal.
voici les source , si quelq'un à le courage de s'y plonger pour voir ou se situe le problème il me rendrait un grand service ( pour ma part je pense que ça merde au niveau des inclusion
Animal.h :
animal.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 #ifndef _ANIMAL #define _ANIMAL #include "./Para.h" #include "./Monde.h" #include <time.h> #include <math.h> #include <iostream> using namespace std; int refGenerale=0; class Faune; class Animal{ int wAge; int wSex; // 0 male, 1 femelle bool wIfDead; int wEnergy; int wRef; bool wIfReprod; char wType; Herbe* wPos; public: Animal(); virtual ~Animal(); int getAge(); int getSex(); bool getIfDead(); int getEnergy(); int getRef(); bool getIfReprod(); char getType(); int getX(); int getY(); int getNbRabbit(); int getNbFox(); int getHauteurHerbe(); Herbe* getPos(); void vieillir(); void setSex(int); void setIfDead(bool); void setEnergy(int); void addEnergy(int); void subEnergy(int); void setIfReprod(bool); char setType(char ); void setPos(Herbe* ); virtual void trySeDeplacer(Monde &) = 0; virtual void trySeReproduire(Faune &) =0; virtual void tryManger(Faune &) =0; }; class Lapin:public Animal{ public: Lapin(); Lapin(int s); ~Lapin(); void avoirBebeLapin(Faune &, int); bool cherchePartenaireLapin(Faune &,int); void trySeDeplacer(Monde &); void trySeReproduire(Faune &); void tryManger(Faune &); }; class Renard:public Animal{ public: Renard(); Renard(int s); ~Renard(); void avoirBebeRenard(Faune &, int); bool cherchePartenaireRenard(Faune &,int); void trySeDeplacer(Monde &); void trySeReproduire(Faune &); void tryManger(Faune &); }; #endif // _ANIMAL
Faune.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 #include "./Animal.h" #include "./Faune.h" Animal::Animal(){ cout<<"Constructeur d'Animal\n"; wAge = 0; wIfDead = false; wRef = refGenerale++; wIfReprod = false; wPos = NULL; } Animal::~Animal(){ cout<<"Destructeur d'animal\n"; if (wPos != NULL) delete wPos; } int Animal::getAge(){return wAge;} int Animal::getSex(){return wSex;} bool Animal::getIfDead(){return wIfDead;} int Animal::getEnergy(){return wEnergy;} int Animal::getRef(){return wRef;} bool Animal::getIfReprod(){return wIfReprod;} char Animal::getType(){return wType;} int Animal::getX(){return wPos->X;} int Animal::getY(){return wPos->Y;} int Animal::getNbRabbit(){return wPos->nbRabbitMale + wPos->nbRabbitFemelle;} int Animal::getNbFox(){return wPos->nbFoxMale + wPos->nbFoxFemelle;} int Animal::getHauteurHerbe(){return wPos->hauteurHerbe;} Herbe* Animal::getPos(){return wPos;} void Animal::vieillir(){wAge = wAge+1;} void Animal::setSex(int s){wSex = s;} void Animal::setIfDead(bool d){ wIfDead = d;} void Animal::setEnergy(int e){ wEnergy = e;} void Animal::addEnergy(int e){wEnergy = wEnergy + e;} void Animal::subEnergy(int e){wEnergy = wEnergy - e;} void Animal::setIfReprod(bool r){wIfReprod = r;} char Animal::setType(char c){wType = c;} void Animal::setPos(Herbe* h){wPos = h;} Lapin::Lapin():Animal(){ cout<<"Constructeur de Lapin \n"; Animal::setType('r'); Animal::setEnergy(tabInfo[9]); } Lapin::Lapin(int s):Animal(){ Animal::setType('r'); Animal::setEnergy(tabInfo[9]); Animal::setSex(s); } Lapin::~Lapin(){cout<<"Destructeur de Lapin\n";} void Lapin::trySeDeplacer(Monde &m){ if(getEnergy()>tabInfo[11] && getHauteurHerbe()>tabInfo[5])//nrj ok et hauteurHeubre trop petite pour se nourrir { int best_X=0,best_Y=0,max=0,i_bis=0,j_bis=0; for(int i=(this->getX()-1);i<=(this->getX()+1);i++) { for(int j=(this->getY()-1);j<=(this->getY()+1);j++) { //mise à jour des coord pour monde torique i_bis=i; j_bis=j; if(i<0)i_bis=tabInfo[0]-1; if(i>=tabInfo[0])i_bis=0; if(j<0)j_bis=tabInfo[0]-1; if(j>tabInfo[0])j_bis=0; if(m.getCase(i_bis,j_bis).hauteurHerbe>max) { max=m.getCase(i_bis,j_bis).hauteurHerbe; best_X=i_bis; best_Y=j_bis; } } } if(best_X != this->getX() || best_Y != this->getY()) { if(getSex()==0) getPos()->nbRabbitMale--; else this->getPos()->nbRabbitFemelle--; setPos(m.getPtrCase(best_X,best_Y)); setEnergy(getEnergy()-tabInfo[20]); if(getSex()==0)this->getPos()->nbRabbitMale++; else this->getPos()->nbRabbitFemelle++; } } } //à définir void Lapin::tryManger(Faune &f){cout<<"Miam une carotte\n";}//à définir Renard::Renard():Animal(){ cout<<"Constructeur de renard \n"; Animal::setType('f'); Animal::setEnergy(tabInfo[10]); } Renard::Renard(int s):Animal(){ Animal::setType('f'); Animal::setEnergy(tabInfo[10]); Animal::setSex(s); } Renard::~Renard(){cout<<"Destructeur de Renard\n";} void Renard::trySeDeplacer(Monde &m){if(getEnergy()>tabInfo[12] && getNbRabbit()==0)//nrj ok et plus de Lapin sur la case { int best_X=0,best_Y=0,max=0,i_bis=0,j_bis=0; for(int i=(this->getX()-1);i<=(this->getX()+1);i++) { for(int j=(this->getY()-1);j<=(this->getY()+1);j++) { i_bis=i; j_bis=j; if(i<0)i_bis=tabInfo[0]-1; if(i>=tabInfo[0])i_bis=0; if(j<0)j_bis=tabInfo[0]-1; if(j>tabInfo[0])j_bis=0; if(m.getCase(i_bis,j_bis).nbRabbitMale+m.getCase(i_bis,j_bis).nbRabbitFemelle>max) { max=m.getCase(i_bis,j_bis).nbRabbitMale+m.getCase(i_bis,j_bis).nbRabbitFemelle; best_X=i_bis; best_Y=j_bis; } } } if(best_X != this->getX() || best_Y != this->getY()) { if(getSex()==0) getPos()->nbFoxMale--; else this->getPos()->nbFoxFemelle--; setPos(m.getPtrCase(best_X,best_Y)); setEnergy(getEnergy()-tabInfo[19]); if(getSex()==0)this->getPos()->nbFoxMale++; else this->getPos()->nbFoxFemelle++; } } } void Renard::trySeReproduire(Faune &f){cout<<"Oh oui fox prend moi\n";} //à définir void Renard::tryManger(Faune &f){cout<<"miam un lapin\n";}//à définir void Renard::avoirBebeRenard(Faune &f, int n){ int i = 0; while( i != n){ Renard* r = new Renard(rand()%2); r->setPos(this->getPos()); f.addAnimal(r); tabRapport[8]++; i++; } } void Lapin::avoirBebeLapin(Faune &f, int n){ int i = 0; while( i != n){ Lapin* l = new Lapin(rand()%2); l->setPos(this->getPos()); f.addAnimal(l); tabRapport[7]++; i++; } } bool Lapin::cherchePartenaireLapin(Faune &f, int n){ if(f[n]->getX() == this->getX && f[n]->getY() == this->getY()) if(f[n]->getType() == 'r') if(this->getSex() == 0){ // si l'instance est un male if(f[n]->getSex() == 1)//on cherche une femelle if(f[n]->getIfReprod() == false) if(f[n]->getAge()<= tabInfo[15]) return true; } else{ // si l'instance est une femelle if(f[n]->getSex() == 0)//on cherche un male if(f[n]->getAge()<= tabInfo[15]) return true; } return false; } bool Renard::cherchePartenaireRenard(Faune &f, int n){ if(f[n]->getX() == this->getX && f[n]->getY() == this->getY()) if(f[n]->getType() == 'f') if(this->getSex() == 0){ // si l'instance est un male if(f[n]->getSex() == 1) // on cherche une femelle if(f[n]->getIfReprod() == false) if(f[n]->getAge()<= tabInfo[16]) return true; } else{ else{ // si l'instance est une femelle if(f[n]->getSex() == 0)//on cherche un male if(f[n]->getAge()<= tabInfo[16]) return true; } return false; } void Lapin::trySeReproduire(Faune &f){ // vérification de l'age mini de reproduction if(this->getAge()<= tabInfo[15]){ //si en age de reprod if(this->getSex() == 0){ // si male if(this->getPos()->nbRabbitFemelle != 0){ // s'il y a des lapin femelle sur la case int nb_max = this->getEnergy() / tabInfo[13]; int i = 0; while(i != f.getSize() && i != nb_max){ if(this->cherchePartenaireLapin(f, i)){ // Vrai si l'instance courante dans f est une lapine qui peut se reprod f[i]->setIfReprod(true); // la femelle va se reproduire this->avoirBebeLapin(f,rand()%4); // elle a au max 4 bébé f[i]->subEnergy(tabInfo[13]); // on baisse son energie if(f[i]->getEnergy() <= 0)f[i]->setIfDead(true);//si son energie tombe a zero elle meurt } i++; } } } else{ //c'est une femelle if(this->getPos()->nbRabbitMale != 0){ // s'il y a des lapin males sur la case int nb_max = this->getEnergy() / tabInfo[13]; int i = 0; while(i != f.getSize() && i != nb_max){ if(this->cherchePartenaireLapin(f, i)){ // Vrai si l'instance courante dans f est un lapin qui peut se reprod this->setIfReprod(true); // la femelle(instance courante) va se reproduire this->avoirBebeLapin(f,rand()%4); // elle a au max 4 bébé this->subEnergy(tabInfo[13]); // on baisse son energie if(this->getEnergy() <= 0)this->setIfDead(true);//si son energie tombe a zero elle meurt } i++; } } } }
Dans le main() j'inclus Faune.h qui inclut lui-même Animal.h donc je pense que c'est bon.
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 #ifndef _FAUNE #define _FAUNE #include "./Animal.h" #include "./Para.h" #include <vector> #include <iostream> using namespace std; typedef vector<Animal*> VecAnim; class Faune:private VecAnim{ public: Faune():VecAnim(){cout<<"constructeur de faune\n";} ~Faune(){ cout<<"Destructeur de faune\n"; VecAnim::iterator i; for(int i=0;i<this->getSize();i++)delete this->operator[](i); this->clear(); } int getSize(){return this->size();} void addAnimal(Animal* a) { this->push_back(a); //si lapin if( a->getType() == 'r'){ //si male if(a->getSex()== 0) a->getPos()->nbRabbitMale++; //si femelle lapin else a->getPos()->nbRabbitFemelle++; } else{//si renard //si male if(a->getSex()== 0) a->getPos()->nbFoxMale++; //si femelle renard else a->getPos()->nbFoxFemelle++; } } void delAnimal(int ref) { int i=0; VecAnim::iterator ite; ite=this->begin(); while (i != this->getSize() && this->operator[](i)->getRef() != ref) { i++;ite++;} if( i!=this->getSize()) { //si lapin if( this->operator[](i)->getType() == 'r'){ //si male if(this->operator[](i)->getSex()== 0) this->operator[](i)->getPos()->nbRabbitMale--; //si femelle lapin else this->operator[](i)->getPos()->nbRabbitFemelle--; } else{//si renard //si male if(this->operator[](i)->getSex()== 0) this->operator[](i)->getPos()->nbFoxMale--; //si femelle renard else this->operator[](i)->getPos()->nbFoxFemelle--; } delete this->operator[](i); this->operator[](i)=NULL; this->erase(ite,ite); } } Animal* seekAnimal(int ref) { int i=0; while (i != this->getSize() && this->operator[](i)->getRef() != ref) i++; if( i!=this->getSize()) return this->operator[](i); } Animal* & operator[](int i) { return *(begin() + i); } // verif si la bestiole à fait son temps bool ifDeadAge(int i){ //si Lapin if(this->operator[](i)->getType() == 'r'){ if( this->operator[](i)->getAge()> tabInfo[1]) return true; } else{// si Renard if( this->operator[](i)->getAge()> tabInfo[2]) return true; } return false; } // fin de tour : mettre reprod a false , vieillir, mourir les vieux, void finDeTour(){ int i; for( i=0;i != this->getSize();i++){ this->operator[](i)->vieillir(); if( this->ifDeadAge(i)) this->delAnimal(this->operator[](i)->getRef()); if(this->operator[](i)->getSex() == 1 && this->operator[](i)->getIfReprod() == true) this->operator[](i)->setIfReprod(false); if(this->operator[](i)->getIfDead() ==true) this->delAnimal(this->operator[](i)->getRef()); } } }; #endif //_FAUNE
voila si quelqu'un a une solution ou désire un complément d'information ou de code ya pas de soucis !
merci à vous , bonne soirée !
Partager