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
| #include "Head.h"
int min(int a, int b) {
if (a > b) return b; else return a;
}
Dinamic::~Dinamic() {
for (int i = 0; i < len; i++)
delete v[i];
delete v;
}
void Dinamic::insertW(Wolf* d)
{
resize(n1+1,n2);
v[n1 - 1] = d;
}
void Dinamic::insertH(Hare* d)
{
resize(n1, n2+1);
v[n2 - 1] = d;
}
Zver* & Dinamic::operator [] (int index)
{
return v[index];
}
Zver* Dinamic::operator [] (int index) const
{
return v[index];
}
int Dinamic::getlen() { return len; }
void Dinamic::eraseW(int n)
{
for (int i = n; i < len - 1; i++)
{
v[i] = v[i + 1];
}
resize(n1 - 1, n2);
}
void Dinamic::eraseH(int n)
{
for (int i = n; i < len - 1; i++)
{
v[i] = v[i + 1];
}
resize(n1 , n2-1);
}
/*void show()
{
for (int i = 0; i < len; i++)
cout << v[i] << ' ';
cout << endl << flush;
}
void full()
{
for (int i = 0; i < len; i++)
v[i] = rand() % 20;
}
void reverse()
{
for (int i = 0; i < len / 2; i++)
{
double m;
m = v[i];
v[i] = v[len - 1 - i];
v[len - 1 - i] = m;
}
} */
Zver::Zver(int a = rand() % 80, int b = rand() % 25) : x(a), y(b), life(0), satiety(0) {}
int Zver::getx() { return x; }
int Zver::gety() { return y; }
Zver::~Zver() { }
Wolf::Wolf(int a, int b) : Zver(a, b) {}
void Wolf::move() {
int r;
for (int i = 0; i < 2; i++) {
r = rand() % 4;
switch (r) {
case 0: x++; break;
case 1: x--; break;
case 2: y++; break;
case 3: y--; break;
}
}
if (x == 80) x = 0;
if (x == -1) x = 79;
if (y == 25) y = 0;
if (y == -1) y = 24;
}
void Wolf::eat(Dinamic ar) {
for (int i = 0; i < ar.getlen(); i++) {
if (1) {
satiety++;
ar.eraseH(i);
if (satiety == 5) {
Wolf w;
w.x = x; w.y = y;
ar.insertW(&w);
}
}
}
}
int Wolf::kto() { return 1; }
void Wolf::destroy(Dinamic& arr, int k) {
if (life == 20) arr.eraseW(k);
}
char Wolf::show() { return '#'; }
Wolf getWolf() { Wolf a; return a; }
Hare::Hare(int a, int b) : Zver(a, b) {}
void Hare::move() {
int r;
for (int i = 0; i < 1; i++) {
r = rand() % 4;
switch (r) {
case 0: x++; break;
case 1: x--; break;
case 2: y++; break;
case 3: y--; break;
}
}
if (x == 80) x = 0;
if (x == -1) x = 79;
if (y == 25) y = 0;
if (y == -1) y = 24;
}
void Hare::destroy(Dinamic& arr, int k) {
if (life == 10) arr.eraseH(k);
}
void Hare::born(Dinamic& arr, int k) {
if (life % 5 == 0 && life != 0) {
Hare r;
arr.insertH(&r);
}
}
char Hare::show() { return '*'; }
Hare getHare() { Hare a; return a; }
int Hare::kto() { return 0; }
void Hare::eat(Dinamic ar) {}
void Dinamic::show()
{
char screen[80][25];
for (int i = 0; i<80; i++)
for (int j = 0; j < 25; j++)
screen[i][j] = ' ';
for (int i = 0; i < len; i++)
screen[(v[i])->getx()][(v[i])->gety()] = (v[i])->show();
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 80; j++)
cout << screen[i][j];
cout << endl;
}
}
Dinamic::Dinamic(int N1 = 0, int N2 = 0) : v(0), len(0), n1(0), n2(0) //конструктор
{
v = new Zver*[N1 + N2];
for (int i = 0; i < N1; i++)
v[i] = new Wolf;
for (int i = 0; i < N2; i++)
v[i + N1] = new Hare;
if (v) len = N1 + N2;
n1 = N1;
n2 = N2;
}
int Dinamic::resize(int N1, int N2)
{
Zver **p = new Zver*[N1+N2];
for (int i = 0; i < N1; i++)
p[i] = new Wolf;
for (int i = 0; i < N2; i++)
p[i + N1] = new Hare;
if (!p) return len;
for (int i = 0; i < min(N1, n1); i++)
p[i] = v[i];
for (int i = 0; i < min(N2, n2); i++)
p[i + N1] = v[i + N1];
for (int i = 0; i < len; i++)
delete v[i];
delete v;
v = 0;
v = p;
len = N1 + N2;
n1 = N1;
n2 = N2;
return len;
} |