aboutsummaryrefslogtreecommitdiff
path: root/Занимательное программирование/1/8_4_life/unit1.pas
blob: b5d4702e1665edb66cfc88b57a795fa46ab707c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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.