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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
| unit pr1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, RXClock, ToolWin, ComCtrls, Menus, RXSpin, RXCtrls,
Spin;
// Описание констант
const kol=50; // количство клеток по координатам X и Y
shag=11; // размер клетки (в пикселях), используется при прорисовке клеток
sax=-8;say=-8;//отступ от края формы, используется при прорисовке клеток
// определение новых типов данных
type masInt=array [1..kol,1..kol] of integer;
masWord=array [1..kol,1..kol] of word;
mas_boolean=array [1..kol,1..kol] of boolean;
masString=array [1..kol,1..kol] of string [1];
procedure showAutomats;
procedure refr_1;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
RxLabel6: TRxLabel;
RxLabel4: TRxLabel;
RxLabel7: TRxLabel;
RxLabel5: TRxLabel;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
RadioButton4: TRadioButton;
Button3: TButton;
Button4: TButton;
Panel1: TPanel;
procedure showAutomats;
procedure refr_1;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
RxLabel6: TRxLabel;
RxLabel4: TRxLabel;
RxLabel7: TRxLabel;
RxLabel5: TRxLabel;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
RadioButton4: TRadioButton;
Button3: TButton;
Button4: TButton;
Panel1: TPanel;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure RxSpinEdit1Change(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormPaint(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure RadioButton1Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure RxSpinEdit3Change(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
TimeRef:integer=3; //начальная установка времени рефрактерности клетки
W:masInt; //время возбуждения клетки
Wtmp:masInt; //время возбуждения клетки (временное значение)
automats:masword; //параметр клеточного автомата. 0 – состояние покоя, 1-
под.акт.
// 2-источник возб. или сост. возбуждения, 3-рефрактерность
automatstmp:masword; //параметр клеточного автомата (временное значение)
automats_graph:masword; //параметр клеточного автомата, выведенного на
экран
name_automat:masString; //имя клеточного автомата в состоянии возбужде-
ния
ref:masword; //время рефрактерности клеточного автомата
mas_istochnick_boolean:mas_boolean; //"0" соответствует состоянию возбуж-
дения
//"1" соответствует состоянию источника
xx,yy:integer; //координаты клетки
ver:byte; //вероятность перехода возбуждения к соседним клеткам
implementation
{$R *.DFM}
uses unit2, Unit3;
//Прорисовка клеток с измененным состоянием (графическое отображение)
procedure showAutomats;
var sai,saj:byte;
begin
form1.Canvas.Font.Color:=clwhite;
form1.Canvas.Font.Size:=5;
form1.Canvas.Font.Height:=5;
for SAi:=1 to kol do for SAj:=1 to kol do
if automats_graph[SAi,SAj]<>automats[SAi,SAj] then // если параметры кле-
точного автомата,
// выведенного на экран, не совпадают с параметрами
// клеточного автомата, то отображается измененная клетка.
begin // в зависимости от типа автомата отображается клетка определенного
цвета
if automats[SAi,SAj]=0 then form1.canvas.Brush.Color:=$0080FFFF;
if automats[SAi,SAj]=1 then form1.canvas.Brush.Color:=clwhite;
if automats[SAi,SAj]=2 then form1.canvas.Brush.Color:=clRed;
if automats[SAi,SAj]=3 then form1.canvas.Brush.Color:=clGray;
if automats[SAi,SAj]=4 then form1.canvas.Brush.Color:=clMaroon;
form1.Canvas.Rectangle( SAx+SAj*shag, SAy+SAi*shag,
SAx+(shag+1)+SAj*shag, SAy+(shag+1)+SAi*shag);
form1.Canvas.TextOut(SAx+SAj*shag+2,SAy+SAi*shag-
0,name_automat[SAi,SAj]);
end;
automats_graph:=automats;
end;
// Процедура уменьшения времени рефрактерности клетки на единицу
procedure refr_1;
var ri,rj:integer;
begin
for ri:=1 to kol do
for rj:=1 to kol do
If (automats[ri,rj]=3) and (ref[ri,rj]>0) then
Begin
if wtmp[ri,rj]=0 then
begin
ref[ri,rj]:=ref[ri,rj]-1;
if ref[ri,rj]=0 then automats[ri,rj]:=0;
end
else If ref[ri,rj]>0 then wtmp[ri,rj]:=wtmp[ri,rj]-1;
End;
end;
// В процедуре проверяется, находится ли клетка в состоянии возбуждения,
// если да, то соседние клетки с определенной вероятностью переходят в со-
стояние возбуждения,
// а сама клетка, после прохождения цикла возбуждения, переходит в состоя-
ние рефрактерности
procedure Vozb;
var Vi,Vj:integer;
begin
automatstmp:=automats;
for vi:=1 to kol do
for vj:=1 to kol do
begin
if (wtmp[Vi,Vj]=0)and(mas_istochnick_boolean[Vi,Vj]=false) then
Begin
if (automats[Vi,Vj]=2) then
begin
//параметру клеточного автомата (временное значение) присваивается значе-
ние рефрактерности
automatstmp[Vi,Vj]:=3;
//присвоение клетке времени рефрактерности
ref[Vi,Vj]:=TimeRef;
//проверка состояния соседних клеток и
//присвоение им с определенной вероятностью состояния возбуждения,
//если они находятся в состоянии покоя
if (vi<>1)and(automats[Vi-1,Vj]=0)and(random(100)<=ver-1)then
begin
automatstmp[Vi-1,Vj]:=2;name_automat[Vi-1,Vj]:=name_automat[Vi,Vj];
wtmp[Vi-1,Vj]:=w[Vi,Vj];
w[Vi-1,Vj]:=w[Vi,Vj];
end;
if (vi<>kol) and (automats[Vi+1,Vj]=0)and(random(100)<=ver-1) then
begin
automatstmp[Vi+1,Vj]:=2;name_automat[Vi+1,Vj]:=name_automat[Vi,Vj];
wtmp[Vi+1,Vj]:=w[Vi,Vj];
w[Vi+1,Vj]:=w[Vi,Vj];
end;
if (vj<>1) and (automats[Vi,Vj-1]=0)and(random(100)<=ver-1) then
begin
automatstmp[Vi,Vj-1]:=2;name_automat[Vi,Vj-1]:=name_automat[Vi,Vj];
wtmp[Vi,Vj-1]:=w[Vi,Vj];
w[Vi,Vj-1]:=w[Vi,Vj];
end;
if (vj<>kol) and (automats[Vi,Vj+1]=0)and(random(100)<=ver-1) then
begin
automatstmp[Vi,Vj+1]:=2;name_automat[Vi,Vj+1]:=name_automat[Vi,Vj];
wtmp[Vi,Vj+1]:=w[Vi,Vj];
w[Vi,Vj+1]:=w[Vi,Vj];
end;
wtmp[Vi,Vj]:=w[Vi,Vj];
name_automat[Vi,Vj]:='';
end;
End
Else if (automats[Vi,Vj]=2)and(mas_istochnick_boolean[Vi,Vj]=false) then
wtmp[Vi,Vj]:=wtmp[Vi,Vj]-1;
end;
end;
// В процедуре проверяется, находится ли клетка в состоянии источника,
// если да, то соседние клетки с определенной вероятностью переходят в со-
стояние возбуждения,
// а сама клетка-источник не изменит своего состояния
procedure Istochnic;
var Vi,Vj:integer;
begin
for vi:=1 to kol do
for vj:=1 to kol do
if (wtmp[Vi,Vj]<=0)and(mas_istochnick_boolean[Vi,Vj]=true) then
Begin
if (automats[Vi,Vj]=2) then
//проверка состояния соседних клеток и
//присвоение им с определенной вероятностью состояния возбуждения,
//если они находятся в состоянии покоя
begin
if (vi<>1) and (automats[Vi-1,Vj]=0) then
begin
automatstmp[Vi-1,Vj]:=2;mas_istochnick_boolean[Vi-1,Vj]:=false;
wtmp[Vi-1,Vj]:=StrToInt(form1.RxSpinEdit4.Text)-1;
w[Vi-1,Vj]:=StrToInt(form1.RxSpinEdit4.Text)-1;
name_automat[Vi-1,Vj]:=name_automat[Vi,Vj];
end;
if (vi<>kol) and (automats[Vi+1,Vj]=0) then
begin
automatstmp[Vi+1,Vj]:=2;
wtmp[Vi+1,Vj]:=StrToInt(form1.RxSpinEdit4.Text)-1;
w[Vi+1,Vj]:=StrToInt(form1.RxSpinEdit4.Text)-1;
name_automat[Vi+1,Vj]:=name_automat[Vi,Vj];
end;
if (vj<>1) and (automats[Vi,Vj-1]=0) then
begin
automatstmp[Vi,Vj-1]:=2;
wtmp[Vi,Vj-1]:=StrToInt(form1.RxSpinEdit4.Text)-1;
w[Vi,Vj-1]:=StrToInt(form1.RxSpinEdit4.Text)-1;
name_automat[Vi,Vj-1]:=name_automat[Vi,Vj];
end;
if (vj<>kol) and (automats[Vi,Vj+1]=0) then
begin
automatstmp[Vi,Vj+1]:=2;
wtmp[Vi,Vj+1]:=StrToInt(form1.RxSpinEdit4.Text)-1;
w[Vi,Vj+1]:=StrToInt(form1.RxSpinEdit4.Text)-1;
name_automat[Vi,Vj+1]:=name_automat[Vi,Vj];
end;
wtmp[Vi,Vj]:=w[Vi,Vj];
end;
End
Else if (mas_istochnick_boolean[Vi,Vj]=true) then wtmp[Vi,Vj]:=wtmp[Vi,Vj]-1;
end;
//Нажата кнопка [GO]
procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
wtmp:=w;
automats_graph:=automats;
for i:=1 to strtoint(form1.RxSpinEdit2.Text) do
begin
vozb;
automats:=automatstmp;
Istochnic;
automats:=automatstmp;
refr_1; //процедура уменьшения времени рефрактерности на единицу
showautomats; // процедура прорисовки клеток
end;
end;
//Вызов формы "О програмее"
procedure TForm1.Button1Click(Sender: TObject);
begin
form3.showmodal;
end;
// процедура, выполняемая при получении формой фокуса ввода
procedure TForm1.FormActivate(Sender: TObject);
var fax,fay:word;
begin
randomize;
for fax:=1 to kol do
for fay:=1 to kol do
begin
name_automat[fax,fay]:='';
mas_istochnick_boolean[fax,fay]:=false;
end;
wtmp:=w;
ver:=strtoint(form1.RxSpinEdit3.Text);
end;
// Изменение времени рефрактерности
procedure TForm1.RxSpinEdit1Change(Sender: TObject);
begin
TimeRef:=strtoint(form1.RxSpinEdit1.Text)+1;
end;
// Событие, возникающее при нажатии кнопки мышки (на Форме)
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if form1.GroupBox1.Visible=true then exit; //если панель выбора параметров
клетки
//уже активна, то выход из процедуры
xx:=(x+shag) div shag; yy:=(y+shag) div shag; //определяет координаты клетки
if (xx<=50) and (yy<=50)then //при попадании координаты клетки в заданную
область
// выполняется следующий цикл:
begin
form1.RxLabel6.Caption:=IntToStr(xx); //отображение координаты клетки X
form1.RxLabel7.Caption:=IntToStr(yy); //отображение координаты клетки Y
form1.GroupBox1.Visible:=true; //активация панели выбора параметров клет-
ки
end;
end;
// Процедура запускается, когда необходимо перерисовать форму
procedure TForm1.FormPaint(Sender: TObject);
var fax,fay:word;
begin
for fax:=1 to kol do for fay:=1 to kol do automats_graph[fax,fay]:=1;
showautomats;
end;
//Отмена установки параметров выбранной клетки
procedure TForm1.Button4Click(Sender: TObject);
begin
form1.GroupBox1.Visible:=false;
end;
// Выбор клетки, задание ее параметров
// Процедура, выполняемая при нажатии кнопки [OK]
procedure TForm1.Button3Click(Sender: TObject);
begin
form1.GroupBox1.Visible:=false; //убирается панель выбора параметров клет-
ки
//выбранной клетке присваивается заданное состояние
if form1.RadioButton1.Checked=true then automats[yy,xx]:=0;
if form1.RadioButton2.Checked=true then begin
ref[yy,xx]:=form1.SpinEdit1.Value;automats[yy,xx]:=3;end;
if form1.RadioButton3.Checked=true then begin
w[yy,xx]:=form1.SpinEdit2.Value-
1;mas_istochnick_boolean[yy,xx]:=false;automats[yy,xx]:=2;name_automat[yy,xx
]:=form1.edit2.text;end;
if form1.RadioButton4.Checked=true then automats[yy,xx]:=1;
if form1.RadioButton5.Checked=true then begin
w[yy,xx]:=form1.SpinEdit2.Value-2; automats[
yy,xx]:=2;mas_istochnick_boolean[yy,xx]:=true;name_automat[yy,xx]:=for
m1.edit2.text;end;
showautomats; // прорисовываем поле клеточных автоматов
end;
//Задание визуального набора параметров при выборе состояния клетки
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
if form1.RadioButton1.Checked=true then
begin
form1.Panel1.Visible:=false; form1.Panel2.Visible:=false;
end;
if form1.RadioButton2.Checked=true then
begin
form1.Panel1.Visible:=false; form1.Panel2.Visible:=true;
end;
if form1.RadioButton3.Checked=true then
begin
form1.Panel1.Visible:=true; form1.Panel2.Visible:=false;
end;
if form1.RadioButton4.Checked=true then
begin
form1.Panel1.Visible:=false; form1.Panel2.Visible:=false;
end;
if form1.RadioButton5.Checked=true then
begin
form1.Panel1.Visible:=true; form1.Panel2.Visible:=false;
end;
end;
// Выход из программы
procedure TForm1.Button5Click(Sender: TObject);
begin
Close;
end;
// Установка вероятности перехода возбуждения
procedure TForm1.RxSpinEdit3Change(Sender: TObject);
begin
ver:=strtoint(form1.RxSpinEdit3.Text);
end;
//Процедура выполняется при нажатии на клавишу "Очистить поле"
//сброс значений параметров клеток, все клетки переходят в состояние покоя.
//прорисовка формы
procedure TForm1.Button6Click(Sender: TObject);
var fax,fay:word;
begin
randomize;
for fax:=1 to kol do
for fay:=1 to kol do
begin
name_automat[fax,fay]:='';
mas_istochnick_boolean[fax,fay]:=false;
automats[fax,fay]:=0;
ref[fax,fay]:=0;
automats[fax,fay]:=0;
form1.canvas.Brush.Color:=$0080FFFF;
form1.Canvas.Rectangle(sAx+fAx*shag,sAy+fAy*shag,sAx+(shag+1)+fAx*shag,
sAy+(shag+1)+fAy*shag);
end;
wtmp:=w;
ver:=strtoint(form1.RxSpinEdit3.Text);
end;
// Процедура запускается при создании формы
procedure TForm1.FormCreate(Sender: TObject);
begin
TimeRef:=strtoint(form1.RxSpinEdit1.Text)+1;
end;
end. |