-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Experiment.cpp
128 lines (109 loc) · 2.39 KB
/
Experiment.cpp
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
/** @file Experiment.cpp
@brief Código de la clase Experiment
*/
#include "Experiment.hpp"
// Constructoras
Experiment::Experiment()
{
MAXORGANISMES = 0;
mida = 0;
vius = 0;
}
Experiment::Experiment(int m)
{
MAXORGANISMES = m;
EXP = vector<Organisme>(m);
emparellats = vector<vector<bool> >(m,vector<bool>(m,false));
}
// Modificadoras
int Experiment::reproduccion(Ranking &R)
{
int fills = 0;
int final = mida;
int i = 0;
int j = 1;
vector<bool> aparicions(final,false);
while (j < final and mida != MAXORGANISMES) {
while (i < final - 1 and (aparicions[i] or not EXP[i].esta_vivo())) ++i;
j = i + 1;
while (j < final and (aparicions[j] or emparellats[i][j] or not EXP[j].esta_vivo())) ++j;
if (j < final and i < final - 1) {
Organisme h;
h.reproduccion(EXP[i],EXP[j]);
aparicions[i] = true;
aparicions[j] = true;
emparellats[i][j] = true;
emparellats[j][i] = true;
if (h.esta_vivo()) {
EXP[mida] = h;
++mida;
R.actualizar_ranking(i,j,mida);
++fills;
++vius;
}
}
else if (i < final - 1) {
++i;
j = i + 1;
}
}
return fills;
}
void Experiment::estiron(int x)
{
if (EXP[x-1].consultar_potcreixer()) {
EXP[x-1].estiron();
}
}
void Experiment::recorte(int x)
{
if (EXP[x-1].esta_vivo()) {
EXP[x-1].recorte();
if (not EXP[x-1].esta_vivo()) --vius;
}
}
// Consultoras
int Experiment::tamano() const
{
return mida;
}
int Experiment::tamano_maximo() const
{
return MAXORGANISMES;
}
int Experiment::consultar_vius() const
{
return vius;
}
bool Experiment::muerto() const
{
return (vius == 0);
}
// Lectura
void Experiment::leer_experiment(int marca)
{
int num = 0;
for (int i = 0; i < marca; ++i) {
++num;
EXP[i].leer_organisme(num);
}
vius = marca;
mida = marca;
}
// Escritura
void Experiment::escribir_organisme(int x)
{
if (x <= mida) {
cout << x << " : ";
EXP[x-1].escribir_organisme();
cout << endl;
}
}
void Experiment::escribir_ultims(int x)
{
for (int i = mida - x; i < mida; ++i) {
cout << i + 1 << " : ";
EXP[i].escribir_organisme();
cout << endl;
}
}