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

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 KiB

View File

@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="12"/>
<General>
<Flags>
<CompatibilityMode Value="True"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<Title Value="project1"/>
<Scaled Value="True"/>
<ResourceType Value="res"/>
<UseXPManifest Value="True"/>
<XPManifest>
<DpiAware Value="True"/>
</XPManifest>
<Icon Value="0"/>
</General>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<UseFileFilters Value="True"/>
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
</RunParams>
<RequiredPackages Count="1">
<Item1>
<PackageName Value="LCL"/>
</Item1>
</RequiredPackages>
<Units Count="2">
<Unit0>
<Filename Value="project1.lpr"/>
<IsPartOfProject Value="True"/>
</Unit0>
<Unit1>
<Filename Value="unit1.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="Form1"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="Unit1"/>
</Unit1>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="project1"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Options>
<Win32>
<GraphicApplication Value="True"/>
</Win32>
</Options>
</Linking>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

View File

@@ -0,0 +1,22 @@
program project1;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, Unit1
{ you can add units after this };
{$R *.res}
begin
RequireDerivedFormResource:=True;
Application.Scaled:=True;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

View File

@@ -0,0 +1,165 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectSession>
<Version Value="12"/>
<BuildModes Active="Default"/>
<Units Count="3">
<Unit0>
<Filename Value="project1.lpr"/>
<IsPartOfProject Value="True"/>
<EditorIndex Value="-1"/>
<WindowIndex Value="-1"/>
<TopLine Value="-1"/>
<CursorPos X="-1" Y="-1"/>
<UsageCount Value="25"/>
</Unit0>
<Unit1>
<Filename Value="unit1.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="Form1"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="Unit1"/>
<IsVisibleTab Value="True"/>
<TopLine Value="131"/>
<CursorPos X="31" Y="118"/>
<UsageCount Value="25"/>
<Loaded Value="True"/>
<LoadedDesigner Value="True"/>
</Unit1>
<Unit2>
<Filename Value="../9_conway/unit1.pas"/>
<UnitName Value="Unit1"/>
<EditorIndex Value="-1"/>
<TopLine Value="13"/>
<UsageCount Value="10"/>
</Unit2>
</Units>
<JumpHistory Count="30" HistoryIndex="29">
<Position1>
<Filename Value="unit1.pas"/>
<Caret Line="103" Column="52" TopLine="35"/>
</Position1>
<Position2>
<Filename Value="unit1.pas"/>
<Caret Line="105" Column="52" TopLine="39"/>
</Position2>
<Position3>
<Filename Value="unit1.pas"/>
<Caret Line="21" Column="5" TopLine="4"/>
</Position3>
<Position4>
<Filename Value="unit1.pas"/>
<Caret Line="8" Column="3"/>
</Position4>
<Position5>
<Filename Value="unit1.pas"/>
<Caret Line="20" Column="12" TopLine="11"/>
</Position5>
<Position6>
<Filename Value="unit1.pas"/>
<Caret Line="8" Column="12"/>
</Position6>
<Position7>
<Filename Value="unit1.pas"/>
<Caret Line="20" Column="21"/>
</Position7>
<Position8>
<Filename Value="unit1.pas"/>
<Caret Line="8" Column="20"/>
</Position8>
<Position9>
<Filename Value="unit1.pas"/>
<Caret Line="15" Column="13"/>
</Position9>
<Position10>
<Filename Value="unit1.pas"/>
<Caret Line="8" Column="9"/>
</Position10>
<Position11>
<Filename Value="unit1.pas"/>
<Caret Line="13" Column="17"/>
</Position11>
<Position12>
<Filename Value="unit1.pas"/>
<Caret Line="31" Column="3"/>
</Position12>
<Position13>
<Filename Value="unit1.pas"/>
<Caret Line="37" Column="47" TopLine="6"/>
</Position13>
<Position14>
<Filename Value="unit1.pas"/>
<Caret Line="97" Column="16" TopLine="38"/>
</Position14>
<Position15>
<Filename Value="unit1.pas"/>
<Caret Line="9" Column="7"/>
</Position15>
<Position16>
<Filename Value="unit1.pas"/>
<Caret Line="76" Column="21" TopLine="24"/>
</Position16>
<Position17>
<Filename Value="unit1.pas"/>
<Caret Line="25" Column="6"/>
</Position17>
<Position18>
<Filename Value="unit1.pas"/>
<Caret Line="96" Column="6" TopLine="41"/>
</Position18>
<Position19>
<Filename Value="unit1.pas"/>
<Caret Line="26" Column="21"/>
</Position19>
<Position20>
<Filename Value="unit1.pas"/>
<Caret Line="15" Column="68"/>
</Position20>
<Position21>
<Filename Value="unit1.pas"/>
<Caret Line="89" Column="23" TopLine="30"/>
</Position21>
<Position22>
<Filename Value="unit1.pas"/>
<Caret Line="15" Column="68" TopLine="14"/>
</Position22>
<Position23>
<Filename Value="unit1.pas"/>
<Caret Line="88" Column="23" TopLine="27"/>
</Position23>
<Position24>
<Filename Value="unit1.pas"/>
<Caret Line="120" Column="43" TopLine="55"/>
</Position24>
<Position25>
<Filename Value="unit1.pas"/>
<Caret Line="88" Column="45" TopLine="33"/>
</Position25>
<Position26>
<Filename Value="unit1.pas"/>
<Caret Line="106" Column="15" TopLine="46"/>
</Position26>
<Position27>
<Filename Value="unit1.pas"/>
<Caret Line="78" Column="14" TopLine="42"/>
</Position27>
<Position28>
<Filename Value="unit1.pas"/>
<Caret Line="108" Column="28" TopLine="54"/>
</Position28>
<Position29>
<Filename Value="unit1.pas"/>
<Caret Line="103" Column="52" TopLine="69"/>
</Position29>
<Position30>
<Filename Value="unit1.pas"/>
<Caret Line="157" Column="44" TopLine="97"/>
</Position30>
</JumpHistory>
<RunParams>
<FormatVersion Value="2"/>
<Modes ActiveMode=""/>
</RunParams>
</ProjectSession>
</CONFIG>

View File

@@ -0,0 +1,26 @@
object Form1: TForm1
Left = 1048
Height = 675
Top = 310
Width = 933
Caption = 'Form1'
ClientHeight = 675
ClientWidth = 933
LCLVersion = '2.2.2.0'
object StartStopBtn: TButton
Left = 429
Height = 26
Top = 619
Width = 78
Caption = 'Пуск'
OnClick = StartStopBtnClick
TabOrder = 0
end
object Screen: TPaintBox
Left = 10
Height = 589
Top = 11
Width = 910
OnPaint = ScreenPaint
end
end

View File

@@ -0,0 +1,214 @@
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 Integer;
{ 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: Integer;
public
end;
var
Form1: TForm1;
IsRunning: Boolean;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.StartStopBtnClick(Sender: TObject);
var i, j: Integer;
protoplasmCount, infusoria: Integer;
direction, positionX, positionY, energy: 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] := 0;
while IsRunning do { Основной цикл }
begin
x := RandomRange(1, FieldWidth); { Выбираем случайную клекту }
y := RandomRange(1, FieldHeight);
{ Подсчитываем соседей }
infusoria := 0;
protoplasmCount := 0;
for i := -1 to 1 do
for j := -1 to 1 do
begin
if Field[x + i][y + j] = -2 then
begin
Inc(protoplasmCount);
end
else if Field[x + i][y + j] = -1 then
begin
Inc(infusoria);
end;
end;
if Field[x][y] = -1 then
Dec(infusoria);
if (Field[x, y] = 0) and (infusoria > 2) then { Создаем новую инфузорию }
begin
Field[x, y] := -1;
end
{ Удаляем инфузорию }
else if (Field[x, y] = -1) and ((infusoria < 3) or (infusoria > 4)) then
begin
Field[x, y] := 0;
end
{ Создаем инфузория, если достаточно протоплазмы }
else if ((Field[x, y] = 0) or (Field[x, y] = -2)) and
(protoplasmCount > 6) then
begin
for i := -1 to 1 do
for j := -1 to 1 do
if Field[x + i, y + j] = -2 then
Field[x + i, y + j] := 0;
Field[x, y] := -1;
end
{ Создаем протоплазму, если клетка пустая }
else if Field[x, y] = 0 then
begin
Field[x, y] := -2;
end
{ Животное двигается }
else if Field[x, y] > 0 then
begin
direction := Random(4);
energy := Field[x, y];
case direction of
0:
begin
positionX := x - 1;
positionY := y;
end;
1:
begin
positionX := x + 1;
positionY := y;
end;
2:
begin
positionX := x;
positionY := y - 1;
end;
3:
begin
positionX := x;
positionY := y + 1;
end;
end;
if Field[positionX, positionY] = 0 then
Dec(energy)
else if Field[positionX, positionY] = -1 then
Inc(energy)
else if Field[positionX, positionY] > 0 then
energy := energy + Field[positionX, positionY];
Field[x, y] := 0;
if energy > 0 then
Field[positionX, positionY] := energy
end;
{ Создаем животных }
for i := 1 to FieldWidth do
begin
for j := 1 to FieldHeight do
begin
if (Field[i - 1, j] = -1) and
(Field[i, j] = -1) and (Field[i + 1, j] = -1) then
begin
Field[i - 1, j] := 0;
Field[i, j] := 5;
Field[i + 1, j] := 0;
end
else if (Field[i, j - 1] = -1) and
(Field[i, j] = -1) and (Field[i, j + 1] = -1) then
begin
Field[i, j - 1] := 0;
Field[i, j] := 5;
Field[i, j + 1] := 0;
end;
end;
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] = -1 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
else if Field[i][j] = -2 then
begin
Screen.Canvas.Pen.Color := clGreen;
Screen.Canvas.Ellipse((2 * i - 1) * Rx - Rx, (2 * j - 1) * Ry - Ry,
(2 * i - 1) * Rx + Rx, (2 * j -1) * Ry + Ry);
end
else if Field[i][j] > 0 then
begin
Screen.Canvas.Pen.Color := clYellow;
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.