149 lines
3.6 KiB
ObjectPascal
149 lines
3.6 KiB
ObjectPascal
unit Unit1;
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
interface
|
|
|
|
uses
|
|
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
|
|
|
|
type
|
|
|
|
{ TForm1 }
|
|
|
|
TForm1 = class(TForm)
|
|
StartStopBtn: TButton;
|
|
Screen: TPaintBox;
|
|
procedure ScreenPaint(Sender: TObject);
|
|
procedure StartStopBtnClick(Sender: TObject);
|
|
private
|
|
const FieldWidth = 30;
|
|
FieldHeight = 25;
|
|
|
|
var Field: array [0 .. FieldWidth + 1, 0 .. FieldHeight + 1] of Boolean;
|
|
Changes: array [0 .. FieldWidth + 1, 0 .. FieldHeight + 1] of Boolean;
|
|
Rx, Ry: Integer;
|
|
public
|
|
|
|
end;
|
|
|
|
var
|
|
Form1: TForm1;
|
|
IsRunning: Boolean;
|
|
|
|
implementation
|
|
|
|
{$R *.lfm}
|
|
|
|
{ TForm1 }
|
|
|
|
procedure TForm1.StartStopBtnClick(Sender: TObject);
|
|
var x, y, offset: Integer;
|
|
s, i, j: Integer;
|
|
u, v: Integer;
|
|
begin
|
|
if IsRunning then
|
|
begin
|
|
IsRunning := false;
|
|
StartStopBtn.Caption := 'Пуск';
|
|
Exit;
|
|
end;
|
|
|
|
StartStopBtn.Caption := 'Стоп';
|
|
IsRunning := true;
|
|
|
|
Rx := (Screen.Width div FieldWidth) div 2;
|
|
Ry := (Screen.Height div FieldHeight) div 2;
|
|
|
|
{ Очистка поля }
|
|
for i := 0 to FieldWidth + 1 do
|
|
for j := 0 to FieldHeight + 1 do
|
|
begin
|
|
Field[i, j] := false;
|
|
Changes[i, j] := false;
|
|
end;
|
|
|
|
{ Здесь внести строки, задающие конфигурацию инфузорий }
|
|
offset := 1;
|
|
Field[offset + 0, 10] := true;
|
|
Field[offset + 1, 10] := true;
|
|
Field[offset + 2, 9] := true;
|
|
Field[offset + 2, 11] := true;
|
|
Field[offset + 3, 10] := true;
|
|
Field[offset + 4, 10] := true;
|
|
Field[offset + 5, 10] := true;
|
|
Field[offset + 6, 10] := true;
|
|
Field[offset + 7, 9] := true;
|
|
Field[offset + 7, 11] := true;
|
|
Field[offset + 8, 10] := true;
|
|
Field[offset + 9, 10] := true;
|
|
|
|
while IsRunning do
|
|
begin
|
|
Screen.Invalidate;
|
|
|
|
for x := 1 to FieldWidth do
|
|
for y := 1 to FieldHeight do
|
|
begin
|
|
s := 0;
|
|
for i := -1 to 1 do
|
|
for j := -1 to 1 do
|
|
begin
|
|
if (x = 1) and (i = -1) then
|
|
u := FieldWidth - 1
|
|
else if (x = FieldWidth - 1) and (i = 1) then
|
|
u := 1
|
|
else
|
|
u := x;
|
|
if (y = 1) and (j = -1) then
|
|
v := FieldHeight - 1
|
|
else if (y = FieldHeight - 1) and (y = 1) then
|
|
v := 1
|
|
else
|
|
v := y;
|
|
s := s + Ord(Field[u + i][v + j]);
|
|
end;
|
|
s := s - Ord(Field[x][y]);
|
|
|
|
{ Если произошло рождение или смерть }
|
|
if ((Field[x, y] = false) and (s = 3)) or
|
|
((Field[x, y] = true) and ((s < 2) or (s > 3))) then
|
|
Changes[x, y] := true;
|
|
end;
|
|
|
|
{ Внесение изменений }
|
|
for x := 1 to FieldWidth do
|
|
for y := 1 to FieldHeight do
|
|
begin
|
|
if Changes[x, y] then
|
|
begin
|
|
Field[x, y] := not Field[x, y]; { Меняем состояние на }
|
|
Changes[x, y] := false; { противоположное }
|
|
end;
|
|
end;
|
|
|
|
Sleep(100);
|
|
Application.ProcessMessages;
|
|
if Application.Terminated then
|
|
IsRunning := false;
|
|
end;
|
|
end;
|
|
|
|
procedure TForm1.ScreenPaint(Sender: TObject);
|
|
var i, j: Integer;
|
|
begin
|
|
for i := 1 to FieldWidth do
|
|
for j := 1 to FieldHeight do
|
|
begin
|
|
if Field[i, j] then
|
|
Screen.Canvas.Pen.Color := clBlue
|
|
else
|
|
Screen.Canvas.Pen.Color := clBtnFace;
|
|
Screen.Canvas.Ellipse((2 * i - 1) * Rx - Rx, (2 * j - 1) * Ry - Ry,
|
|
(2 * i - 1) * Rx + Rx, (2 * j - 1) * Ry + Ry);
|
|
end;
|
|
end;
|
|
|
|
end.
|
|
|