Add book "Занимательное программирование"

This commit is contained in:
2025-12-13 16:35:46 +01:00
parent 98329e0a3d
commit c1147629f7
78 changed files with 4530 additions and 0 deletions

View File

@@ -0,0 +1,119 @@
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
Math;
const FieldWidth = 30; { Ширина и }
FieldHeight = 25; { высота поля }
type
TField = array [0..FieldWidth + 1, 0..FieldHeight + 1] of Boolean;
{ TForm1 }
TForm1 = class(TForm)
Screen: TPaintBox;
StartStopBtn: TButton;
procedure ScreenPaint(Sender: TObject);
procedure StartStopBtnClick(Sender: TObject);
private
var Rx, Ry: Integer; { Ширина, высота клекти }
Field: TField;
x, y, s: Integer;
public
end;
var
Form1: TForm1;
IsRunning: Boolean;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.StartStopBtnClick(Sender: TObject);
var i, j: 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;
Randomize;
for i := 0 to FieldWidth + 1 do { Очистка поля }
for j := 0 to FieldHeight + 1 do
Field[i, j] := false;
for i := 1 to FieldWidth do { Создаем начальную конфигурацию }
for j := 1 to FieldHeight do
if Random(4) = 0 then { В среднем будет одна инфузория }
begin { на четыре клетки }
Field[i, j] := true;
end;
while IsRunning do { Основной цикл }
begin
x := RandomRange(1, FieldWidth); { Выбираем случайную клекту }
y := RandomRange(1, FieldHeight);
s := 0; { Подсчитываем соседей }
for i := -1 to 1 do
for j := -1 to 1 do
s := s + Ord(Field[x + i][y + j]);
s := s - Ord(Field[x][y]);
if (Field[x, y] = false) and (s > 2) then { Создаем новую инфузорию }
begin
Field[x, y] := true;
end
else if (Field[x, y] = true) and ((s < 3) or (s > 4)) then { Удаляем }
begin
Field[x, y] := false;
end;
Screen.Invalidate;
Sleep(5);
Application.ProcessMessages;
if Application.Terminated then Exit;
end
end;
procedure TForm1.ScreenPaint(Sender: TObject);
var i, j: Integer;
begin
Screen.Canvas.Pen.Color := clBlue;
for i := 1 to FieldWidth do
begin
for j := 1 to FieldHeight do
begin
if Field[i][j] then
begin
Screen.Canvas.Pen.Color := clBlue;
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;
end;
end.