implementation
{$R *.dfm}
var Current, NextS: array [0..200,0..200] of integer;
N, NCell, CoordX, CoordY: integer;
CellColor: TColor;
Delay: Boolean;
procedure TForm1.DrawGrid1Click(Sender: TObject);
var ColumnA, RowA, i,j:integer;
begin
DrawGrid1.MouseToCell(CoordX, CoordY, ColumnA, RowA);
i:=Current[RowA,ColumnA];
Case i of
0:if (columnA<>0) and (RowA<>0) and (RowA<>N-1) and (ColumnA<>N-1) then
Current[RowA,ColumnA] :=1;
1:Current[RowA,ColumnA]:=0;
end;
for i:=1 to N-2
do for j:=1 to N-2
do if current[i,j]=1
then begin
Drawgrid1.Canvas.Brush.Color:=CellColor;
Drawgrid1.Canvas.FillRect(Drawgrid1.CellRect(j,i)) ;
end
else begin
Drawgrid1.Canvas.Brush.Color:=clWhite;
Drawgrid1.Canvas.FillRect(Drawgrid1.CellRect(j,i)) ;
end;
end;
procedure TForm1.DrawGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
CoordX:=x;
CoordY:=Y;
end;
procedure TForm1.FormCreate(Sender: TObject);
var i,j:integer;
begin
for i:=0 to N-1
do for j:=0 to N-1
do begin
current[i,j]:=0;
NextS[i,j]:=0;
end;
N:=15;
DrawGrid1.ColCount:=N;
DrawGrid1.RowCount:=N;
NCell:=20;
DrawGrid1.DefaultColWidth:=NCell;
DrawGrid1.DefaultRowHeight:=NCell;
CellColor:=clGreen;
ProgressBar1.Position:=0;
end;
procedure TForm1.Button2Click(Sender: TObject);
var Sum, i, j, k, kMax: integer;
begin
kMax := SpinEdit1.Value;
ProgressBar1.Position := 0;
for k:=1 to kMax
do begin
for i := 1 to N-2
do for j := 1 to N-2
do begin
Sum:= Current[i-1,j-1]+Current[i,j-1]+Current[i+1,j-1]+Current[i-1,j+1]+Current[i,j+1]+Current[i+1,j+1]+Current[i-1,j]+Current[i+1,j];
if Sum = 3
then NextS[i,j] := 1
else begin
if (Current[i,j] = 1) and (Sum = 2)
then NextS[i,j] := 1
else NextS[i,j] := 0;
end;
end;
for i := 1 to N-2
do for j := 1 to N-2
do Current[i,j] := NextS[i,j];
for i := 1 to N-2
do for j := 1 to N-2
do if Current[i,j] = 1
then begin
DrawGrid1.Canvas.Brush.Color := CellColor;
DrawGrid1.Canvas.FillRect(DrawGrid1.CellRect(j,i)) ;
end
else begin
DrawGrid1.Canvas.Brush.Color := clWhite;
DrawGrid1.Canvas.FillRect(DrawGrid1.CellRect(j,i)) ;
end;
ProgressBar1.Position := (ProgressBar1.Max*k) div kMax;
Application.ProcessMessages();
if Delay then Sleep(250);
end;
ProgressBar1.Position := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i, j: integer;
begin
for i := 1 to N-2
do for j := 1 to N-2
do begin
Current[i,j] := 0;
DrawGrid1.Canvas.Brush.Color := clWhite;
DrawGrid1.Canvas.FillRect(DrawGrid1.CellRect(j,i)) ;
end;
end;
end.