program LifeSimlator (input,output);
uses
crt;
const
xmax=24;
ymax=79;
n=2;
type
nice=array[1..xmax,1..ymax] of boolean;
var
a,b:nice;
x,y,xa,ya,k:integer;
function check(xa,ya:integer):boolean;
begin
    check:=false;
    if a[x+xa,y+ya] then inc(k);
    if k=n then
    begin
        b[x,y]:=True;
        check:=true;
    end;
end;
procedure putout(q:nice);
begin
    for x:=1 to xmax do
    begin
        for y:=1 to ymax do
        if q[x,y] then write('W')
        else write(' ');
        writeln;
    end;
end;
begin
    randomize;
    for x:=1 to xmax do
    for y:=1 to ymax do
    if random(2)=0 then
    a[x,y]:=false
    else
    a[x,y]:=true;
    repeat
    fillchar(b,sizeof(b),false);
    {
    putout(a);
    writeln('====');
    putout(b);
    readln;
    }
    for x:=1 to xmax do
    for y:=1 to ymax do
    begin
        k:=0;
        if x>1 then
        begin
            if check(-1,0) then continue;
            if y>1 then
            begin
                if check(-1,-1) then continue;
            end
            else
            if y<ymax then
            if check(-1,1) then continue;
        end
        else
        if x<xmax then
        begin
            if check(1,0) then continue;
            if y>1 then
            begin
                if check(1,-1) then continue;
            end
            else if y<ymax then
            if check(1,1) then continue;
            
        end
        else
        begin
            if y>1 then begin
                if check(0,-1) then continue;
            end
            else
            if y<ymax then
            begin
                if check(0,1) then continue;
            end;
        end;
    end;
    putout(b);
    a:=b;
    delay(100);
    //readln;
    clrscr;
    until keypressed;
    writeln;
    write('>');
    readln();
end.
