program evoluts;
uses crt;
label 1,2,3,4,5;
var ia:word;
c,k:char;
a,b:array [1..30,1..20] of integer;
i,j,mk,z,t,x1,x2,x3,y1,y2,y3,xm,ym,p,m,vm,x,y,ey,ex:integer;
procedure ramka;
const lin='═══════════════════════════════';
begin
textcolor(14); for i:=2 to 20 do
begin
gotoxy(6,i); write('║');
gotoxy(36,i); write('║')
end;
gotoxy(6,1); write(lin); gotoxy(6,21); write(lin);
gotoxy(6,1); write('╔'); gotoxy(6,21); write('╚');
gotoxy(36,1); write('╗'); gotoxy(36,21); write('╝')
end;
procedure ogr1;
begin
if x>29 then x:=29;
if x<2 then x:=2;
if y>19 then y:=19;
if y<1 then y:=1
end;
procedure ogr2;
begin
if x>x3 then x3:=x;
if x<x2 then x2:=x;
if y>y3 then y3:=y;
if y<y2 then y2:=y
end;
procedure np;
label 1,2;
begin
for i:=1 to 30 do for j:=1 to 20 do
begin
a[i,j]:=0; b[i,j]:=0
end;
x:=15; y:=10; m:=0; clrscr; textbackground(0);
ramka; gotoxy(x+5,y+1); textcolor(9);
1: if not(keypressed) then goto 1;
textcolor(6); gotoxy(45,1); write('Установлено: ',m);
textcolor(9); gotoxy(x+5,y+1); k:=readkey;
if a[x,y]<>1 then write(' ');
if k=#77 then inc(x);
if k=#75 then dec(x);
if k=#80 then inc(y);
if k=#72 then dec(y);
ogr1; ogr2; gotoxy(x+5,y+1);
if k=#13 then
begin
a[x,y]:=1; inc(m); write('*'); gotoxy(x+5,y+1)
end;
if k=' ' then goto 2;
goto 1;
2: vm:=m
end;
begin
5: x2:=31; x3:=0; y2:=21; y3:=0; p:=0; np;
3: inc(p); m:=0; gotoxy(1,25); textcolor(7);
write('Поколение: ',p); x1:=x2-1; xm:=x3+1; y1:=y2-1; ym:=y3+1;
for ey:=y1 to ym do
begin
y:=ey+1; for ex:=x1 to xm do
begin
x:=ex+1; ogr1;
z:=b[x,y]; t:=0; mk:=0; gotoxy(x+5,y+1);
for x1:=-1 to 1 do for y1:=-1 to 1 do
begin
if (x1=0) and (y1=0) then goto 1;
if a[x+x1,y+y1]>0 then inc(mk);
1: end;
if z=0 then goto 2;
c:='*'; if z>5 then
begin
c:=' '; b[x+1,y+1]:=0
end;
textcolor(z); write(c);
if (mk=2) or (mk=3) then
begin
inc(m); t:=z+1; if t>13 then t:=0
end;
goto 4;
2: write(' '); b[x,y]:=0; if mk=3 then
begin
ogr2; inc(m); t:=1+z
end;
4: if t>13 then t:=0;
b[x,y]:=t
end
end;
gotoxy(56,25); textcolor(7); vm:=m; write(' ');
gotoxy(35,25); write('Численность колонии: ',vm);
for i:=1 to 30 do for j:=1 to 20 do if b[i,j]>=13 then b[i,i]:=0;
a:=b; if keypressed then if readkey=#27 then halt;
for ia:=0 to 65535 do;
if (vm>0) and (m>0) then goto 3;
textcolor(7); clrscr; write('Колония погибла!'); k:=readkey;
if k<>#27 then goto 5;
clrscr
end.