diff options
Diffstat (limited to 'Занимательное программирование/1/8_4_life')
| -rw-r--r-- | Занимательное программирование/1/8_4_life/project1.ico | bin | 0 -> 137040 bytes | |||
| -rw-r--r-- | Занимательное программирование/1/8_4_life/project1.lpi | 78 | ||||
| -rw-r--r-- | Занимательное программирование/1/8_4_life/project1.lpr | 22 | ||||
| -rw-r--r-- | Занимательное программирование/1/8_4_life/project1.lps | 158 | ||||
| -rw-r--r-- | Занимательное программирование/1/8_4_life/project1.res | bin | 0 -> 139052 bytes | |||
| -rw-r--r-- | Занимательное программирование/1/8_4_life/unit1.lfm | 27 | ||||
| -rw-r--r-- | Занимательное программирование/1/8_4_life/unit1.pas | 158 |
7 files changed, 443 insertions, 0 deletions
diff --git a/Занимательное программирование/1/8_4_life/project1.ico b/Занимательное программирование/1/8_4_life/project1.ico Binary files differnew file mode 100644 index 0000000..0341321 --- /dev/null +++ b/Занимательное программирование/1/8_4_life/project1.ico diff --git a/Занимательное программирование/1/8_4_life/project1.lpi b/Занимательное программирование/1/8_4_life/project1.lpi new file mode 100644 index 0000000..d95997c --- /dev/null +++ b/Занимательное программирование/1/8_4_life/project1.lpi @@ -0,0 +1,78 @@ +<?xml version="1.0" encoding="UTF-8"?> +<CONFIG> + <ProjectOptions> + <Version Value="11"/> + <General> + <SessionStorage Value="InProjectDir"/> + <MainUnit Value="0"/> + <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"/> + <Modes Count="0"/> + </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> diff --git a/Занимательное программирование/1/8_4_life/project1.lpr b/Занимательное программирование/1/8_4_life/project1.lpr new file mode 100644 index 0000000..c08a82d --- /dev/null +++ b/Занимательное программирование/1/8_4_life/project1.lpr @@ -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. + diff --git a/Занимательное программирование/1/8_4_life/project1.lps b/Занимательное программирование/1/8_4_life/project1.lps new file mode 100644 index 0000000..42d7088 --- /dev/null +++ b/Занимательное программирование/1/8_4_life/project1.lps @@ -0,0 +1,158 @@ +<?xml version="1.0" encoding="UTF-8"?> +<CONFIG> + <ProjectSession> + <Version Value="11"/> + <BuildModes Active="Default"/> + <Units Count="2"> + <Unit0> + <Filename Value="project1.lpr"/> + <IsPartOfProject Value="True"/> + <EditorIndex Value="-1"/> + <WindowIndex Value="-1"/> + <TopLine Value="-1"/> + <CursorPos X="-1" Y="-1"/> + <UsageCount Value="41"/> + </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="44"/> + <CursorPos X="30" Y="79"/> + <UsageCount Value="41"/> + <Loaded Value="True"/> + <LoadedDesigner Value="True"/> + </Unit1> + </Units> + <JumpHistory Count="30" HistoryIndex="29"> + <Position1> + <Filename Value="unit1.pas"/> + <Caret Line="8" Column="3"/> + </Position1> + <Position2> + <Filename Value="unit1.pas"/> + <Caret Line="20" Column="12" TopLine="11"/> + </Position2> + <Position3> + <Filename Value="unit1.pas"/> + <Caret Line="8" Column="12"/> + </Position3> + <Position4> + <Filename Value="unit1.pas"/> + <Caret Line="20" Column="21"/> + </Position4> + <Position5> + <Filename Value="unit1.pas"/> + <Caret Line="8" Column="20"/> + </Position5> + <Position6> + <Filename Value="unit1.pas"/> + <Caret Line="15" Column="13"/> + </Position6> + <Position7> + <Filename Value="unit1.pas"/> + <Caret Line="8" Column="9"/> + </Position7> + <Position8> + <Filename Value="unit1.pas"/> + <Caret Line="13" Column="17"/> + </Position8> + <Position9> + <Filename Value="unit1.pas"/> + <Caret Line="31" Column="3"/> + </Position9> + <Position10> + <Filename Value="unit1.pas"/> + <Caret Line="37" Column="47" TopLine="6"/> + </Position10> + <Position11> + <Filename Value="unit1.pas"/> + <Caret Line="97" Column="16" TopLine="38"/> + </Position11> + <Position12> + <Filename Value="unit1.pas"/> + <Caret Line="9" Column="7"/> + </Position12> + <Position13> + <Filename Value="unit1.pas"/> + <Caret Line="76" Column="21" TopLine="24"/> + </Position13> + <Position14> + <Filename Value="unit1.pas"/> + <Caret Line="25" Column="6"/> + </Position14> + <Position15> + <Filename Value="unit1.pas"/> + <Caret Line="96" Column="6" TopLine="41"/> + </Position15> + <Position16> + <Filename Value="unit1.pas"/> + <Caret Line="26" Column="21"/> + </Position16> + <Position17> + <Filename Value="unit1.pas"/> + <Caret Line="25" Column="16"/> + </Position17> + <Position18> + <Filename Value="unit1.pas"/> + <Caret Line="41" Column="6" TopLine="21"/> + </Position18> + <Position19> + <Filename Value="unit1.pas"/> + <Caret Line="46" Column="13" TopLine="23"/> + </Position19> + <Position20> + <Filename Value="unit1.pas"/> + <Caret Line="40" Column="60" TopLine="6"/> + </Position20> + <Position21> + <Filename Value="unit1.pas"/> + <Caret Line="41" Column="21" TopLine="9"/> + </Position21> + <Position22> + <Filename Value="unit1.pas"/> + <Caret Line="40" Column="50" TopLine="12"/> + </Position22> + <Position23> + <Filename Value="unit1.pas"/> + <Caret Line="42" Column="63" TopLine="14"/> + </Position23> + <Position24> + <Filename Value="unit1.pas"/> + <Caret Line="87" Column="43" TopLine="34"/> + </Position24> + <Position25> + <Filename Value="unit1.pas"/> + <Caret Line="13" Column="4"/> + </Position25> + <Position26> + <Filename Value="unit1.pas"/> + <Caret Line="12" Column="3"/> + </Position26> + <Position27> + <Filename Value="unit1.pas"/> + <Caret Line="21" Column="3"/> + </Position27> + <Position28> + <Filename Value="unit1.pas"/> + <Caret Line="35" Column="23"/> + </Position28> + <Position29> + <Filename Value="unit1.pas"/> + <Caret Line="89" Column="15" TopLine="53"/> + </Position29> + <Position30> + <Filename Value="unit1.pas"/> + <Caret Line="93" Column="60" TopLine="58"/> + </Position30> + </JumpHistory> + <RunParams> + <FormatVersion Value="2"/> + <Modes Count="0" ActiveMode=""/> + </RunParams> + </ProjectSession> +</CONFIG> diff --git a/Занимательное программирование/1/8_4_life/project1.res b/Занимательное программирование/1/8_4_life/project1.res Binary files differnew file mode 100644 index 0000000..1adb040 --- /dev/null +++ b/Занимательное программирование/1/8_4_life/project1.res diff --git a/Занимательное программирование/1/8_4_life/unit1.lfm b/Занимательное программирование/1/8_4_life/unit1.lfm new file mode 100644 index 0000000..d47db80 --- /dev/null +++ b/Занимательное программирование/1/8_4_life/unit1.lfm @@ -0,0 +1,27 @@ +object Form1: TForm1 + Left = -340 + Height = 636 + Top = 427 + Width = 904 + Caption = 'Form1' + ClientHeight = 636 + ClientWidth = 904 + DesignTimePPI = 93 + LCLVersion = '2.0.12.0' + object StartStopBtn: TButton + Left = 416 + Height = 25 + Top = 600 + Width = 76 + Caption = 'Пуск' + OnClick = StartStopBtnClick + TabOrder = 0 + end + object Screen: TPaintBox + Left = 10 + Height = 571 + Top = 11 + Width = 882 + OnPaint = ScreenPaint + end +end diff --git a/Занимательное программирование/1/8_4_life/unit1.pas b/Занимательное программирование/1/8_4_life/unit1.pas new file mode 100644 index 0000000..b5d4702 --- /dev/null +++ b/Занимательное программирование/1/8_4_life/unit1.pas @@ -0,0 +1,158 @@ +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; + + TPoint = record + x: Integer; + y: Integer; + end; + + { TForm1 } + + TForm1 = class(TForm) + Screen: TPaintBox; + StartStopBtn: TButton; + procedure ScreenPaint(Sender: TObject); + procedure StartStopBtnClick(Sender: TObject); + private + procedure UpdateSurface(field: TField; point: TPoint; neighbours: Integer); + var Rx, Ry: Integer; { Ширина, высота клекти } + BlueField, GreenField: TField; + s, v: Integer; + bluePoint, greenPoint: TPoint; + public + + end; + +var + Form1: TForm1; + IsRunning: Boolean; + +implementation + +{$R *.lfm} + +function CountNeighbours(Field: TField; x: Integer; y: Integer): Integer; + var i, j: Integer; +begin + Result := 0; + + for i := -1 to 1 do + for j := -1 to 1 do + Result := Result + Ord(Field[x + i][y + j]); + Result := Result - Ord(Field[x][y]); +end; + +{ Выбираем случайную клекту } +function ChoosePoint(): TPoint; +begin + Result.x := RandomRange(1, FieldWidth); + Result.y := RandomRange(1, FieldHeight); +end; + +{ 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 + begin + BlueField[i, j] := false; + GreenField[i, j] := false; + end; + + while IsRunning do { Основной цикл } + begin + bluePoint := ChoosePoint(); + greenPoint := ChoosePoint(); + + s := CountNeighbours(BlueField, bluePoint.x, bluePoint.y); + v := CountNeighbours(GreenField, greenPoint.x, greenPoint.y); + + Screen.Invalidate; + + Sleep(250); + Application.ProcessMessages; + if Application.Terminated then Exit; + end +end; + +procedure TForm1.UpdateSurface(field: TField; point: TPoint; neighbours: Integer); +begin + if (field[point.x, point.y] = false) and (neighbours > 2) then { Создаем новую инфузорию } + begin + Screen.Canvas.Pen.Color := clBlue; + Screen.Canvas.Ellipse((2 * point.x - 1) * Rx - Rx, (2 * point.y - 1) * Ry - Ry, + (2 * point.x - 1) * Rx + Rx, (2 * point.y -1) * Ry + Ry); + field[point.x, point.y] := true; + end + else if (field[point.x, point.y] = true) and ((neighbours < 3) or (neighbours > 4)) then { Удаляем } + begin + Screen.Canvas.Pen.Color := clBtnFace; + Screen.Canvas.Ellipse((2 * point.x - 1) * Rx - Rx, (2 * bluePoint.y - 1) * Ry - Ry, + (2 * point.x - 1) * Rx + Rx, (2 * point.y -1) * Ry + Ry); + field[point.x, point.y] := false; + end; +end; + +procedure TForm1.ScreenPaint(Sender: TObject); + var i, j: Integer; +begin + Screen.Canvas.Pen.Color := clBlue; + + for i := 1 to FieldWidth do { Создаем начальную конфигурацию } + for j := 1 to FieldHeight do + if Random(4) = 0 then { В среднем будет одна инфузория } + begin { на четыре клетки } + BlueField[i, j] := true; + Screen.Canvas.Ellipse((2 * i - 1) * Rx - Rx, (2 * j - 1) * Ry - Ry, + (2 * i - 1) * Rx + Rx, (2 * j - 1) * Ry + Ry); + end; + + UpdateSurface(blueField, bluePoint, s); + + Screen.Canvas.Pen.Color := clGreen; + + for i := 1 to FieldWidth do { Создаем начальную конфигурацию } + for j := 1 to FieldHeight do + if Random(4) = 0 then { В среднем будет одна инфузория } + begin { на четыре клетки } + GreenField[i, j] := true; + Screen.Canvas.Ellipse((2 * i - 1) * Rx - Rx, (2 * j - 1) * Ry - Ry, + (2 * i - 1) * Rx + Rx, (2 * j - 1) * Ry + Ry); + end; + + UpdateSurface(greenField, greenPoint, v); +end; + +end. + |
