aboutsummaryrefslogtreecommitdiff
path: root/Занимательное программирование/1/9_conway/unit1.pas
blob: c96515da5b7ea4780b35af42c6c1b6d9e7b15d53 (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
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.