-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.pas
More file actions
executable file
·447 lines (409 loc) · 10.8 KB
/
Copy pathcode.pas
File metadata and controls
executable file
·447 lines (409 loc) · 10.8 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//code by fabian gündel
//date:2011-08-13 22:17
//name: code.pas
//version: 1.0
unit code;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Cell, ComCtrls, Menus, figures;
type
TForm1 = class(TForm)
GenLabel: TLabel;
FieldEdit: TEdit;
FieldImage: TImage;
Timer1: TTimer;
RandomCheck: TCheckBox;
ButtonStart: TButton;
ClearButton: TButton;
FigureBox: TComboBox;
DrawGroup: TGroupBox;
SpeedBar: TTrackBar;
ButtonNext: TButton;
GenBox: TGroupBox;
SlowLabel: TLabel;
FastLabel: TLabel;
ButtonPause: TButton;
SizeBox: TGroupBox;
xEdit: TEdit;
yEdit: TEdit;
xLabel: TLabel;
SizeButton: TButton;
HeadLabel1: TLabel;
HeadLabel2: TLabel;
HeadLabel3: TLabel;
Label1: TLabel;
procedure FieldImageMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormCreate(Sender: TObject);
procedure FieldImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Timer1Timer(Sender: TObject);
procedure GenLabelClick(Sender: TObject);
procedure RandomCheckClick(Sender: TObject);
procedure ButtonStartClick(Sender: TObject);
procedure ClearButtonClick(Sender: TObject);
procedure FieldImageMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure SpeedBarChange(Sender: TObject);
procedure ButtonPauseClick(Sender: TObject);
procedure ButtonNextClick(Sender: TObject);
procedure SizeButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Cell: array of array of TCell;
xMouse,yMouse,xSize,ySize,gen : integer;
draw : boolean;
implementation
{$R *.dfm}
//
// Größe anpassen
//
procedure Size;
var i,j, Width, Height: integer;
begin
draw:= false;
xSize:= strtoint(Form1.xEdit.Text);
ySize:= strtoint(Form1.yEdit.Text);
setlength(Cell,xSize+1,ySize+1);
Width:=xSize*10;
Height:= ySize*10;
Form1.FieldImage.Width:= Width;
Form1.FieldImage.Height:=Height;
for i:=0 to xSize do
begin
for j:=0 to ySize do
begin
Cell[i,j]:=TCell.Create(i*10,j*10);
end;
end;
with Form1.Fieldimage.Canvas do
begin
Brush.Color:= clblack;
Rectangle(0,0,Width,Height);
end;
Form1.SizeBox.Top:= Form1.FieldImage.Top+Form1.FieldImage.Height+5;
Form1.DrawGroup.Top:= Form1.FieldImage.Top+Form1.FieldImage.Height+55;
Form1.GenBox.Top:= Form1.FieldImage.Top+Form1.FieldImage.Height+105;
Form1.ButtonStart.Top:= Form1.FieldImage.Top+Form1.FieldImage.Height+55;
Form1.Height:= Form1.FieldImage.Height+335;
Form1.FieldImage.Left:= (Form1.Width div 2) - (Form1.FieldImage.Width div 2);
end;
//
// Festlegen der Regeln
//
procedure rules;
var i,j: integer;
begin
for i:=0 to xSize do
begin
for j:=0 to ySize do
begin
//
// Eine tote Zelle mit genau 3 lebenden Nachbarn wird in der nächsten
// Generation neu geboren.
//
if (Cell[i,j].neighbor=3) and (Cell[i,j].alive=false) then
begin
Cell[i,j].alive:= true;
Cell[i,j].live;
end
//
// Lebende Zellen mit weniger als 2 lebenden Nachbarn sterben in der
// Folgegeneration an Einsamkeit.
//
else if (Cell[i,j].neighbor<2) and (Cell[i,j].alive=true) then
begin
Cell[i,j].alive:= false;
Cell[i,j].live;
end
//
// Eine lebende Zelle mit 2 oder 3 lebenden Nachbarn bleibt in der
// Folgegeneration lebend.
//
else if ((Cell[i,j].neighbor=2) or (Cell[i,j].neighbor=3))
and (Cell[i,j].alive=true) then
begin
Cell[i,j].alive:= true;
Cell[i,j].live;
end
//
// Lebende Zellen mit mehr als 3 Nachbarn sterben in der Folgegeneration
// an Überbevölkerung.
//
else if (Cell[i,j].neighbor>3) and (Cell[i,j].alive=true) then
begin
Cell[i,j].alive:= false;
Cell[i,j].live;
end;
end;
end;
end;
//
//Position der Zellen vereinheitlichen (Beispiel: Mausposition -> 453.4
// Zellenposition -> 450.0 )
//
procedure Cposition;
begin
xMouse:= xMouse div 10;
xMouse:= trunc(xMouse);
xMouse:= xMouse*10;
yMouse:= yMouse div 10;
yMouse:= trunc(yMouse);
yMouse:= yMouse*10;
end;
//
// Auslesen der Mausposition
//
procedure TForm1.FieldImageMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
xMouse:= X;
yMouse:= Y;
if draw= true then
begin
Cposition;
try
Cell[xMouse div 10,yMouse div 10].alive:= true;
Cell[xMouse div 10,yMouse div 10].live;
except
end;
end;
end;
//
// Formular erzeugen mit Zeichenfläche (FieldImage)
//
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Position:= poScreenCenter;
gen:=0;
Size;
end;
//
// Zellen selbst einfügen oder löschen
//
procedure TForm1.FieldImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
draw:= true;
if FigureBox.Text= 'Cell' then
begin
Cposition;
if Form1.FieldImage.Canvas.Pixels[X,Y]= clblack then
begin
Cell[xMouse div 10,yMouse div 10].alive:= true;
end
else
begin
Cell[xMouse div 10,yMouse div 10].alive:= false;
end;
Cell[xMouse div 10,yMouse div 10].live;
end
else if FigureBox.Text= 'f-Pentomino' then
fpentomino
else if FigureBox.Text= 'Glider' then
Glider
else if FigureBox.Text= 'Spaceship (lightweight)' then
LSpaceship
else if FigureBox.Text= 'Spaceship (middleweight)' then
MSpaceship
else if FigureBox.Text= 'Spaceship (heavyweight)' then
HSpaceship
else if FigureBox.Text= 'Gosper Gun' then
GosperGun;
end;
//
// Neue Generation erzeugen
//
procedure generation;
var i,j: integer;
begin
for i:=0 to xSize-1 do
begin
for j:=0 to ySize-1 do
begin
Cell[i,j].neighbor:=0;
try //Exception wenn Feld eines Arrays nicht existiert z.B. Cell[-1,-1]
if Cell[i-1,j-1].alive=true then
Inc (Cell[i,j].neighbor);
except
on EAccessViolation do
Cell[i,j].neighbor:=Cell[i,j].neighbor;
end;
try
if Cell[i,j-1].alive=true then
Inc (Cell[i,j].neighbor);
except
on EAccessViolation do
Cell[i,j].neighbor:=Cell[i,j].neighbor;
end;
try
if Cell[i+1,j-1].alive=true then
Inc (Cell[i,j].neighbor);
except
on EAccessViolation do
Cell[i,j].neighbor:=Cell[i,j].neighbor;
end;
try
if Cell[i-1,j].alive=true then
Inc (Cell[i,j].neighbor);
except
on EAccessViolation do
Cell[i,j].neighbor:=Cell[i,j].neighbor;
end;
try
if Cell[i+1,j].alive=true then
Inc (Cell[i,j].neighbor);
except
on EAccessViolation do
Cell[i,j].neighbor:=Cell[i,j].neighbor;
end;
try
if Cell[i-1,j+1].alive=true then
Inc (Cell[i,j].neighbor);
except
on EAccessViolation do
Cell[i,j].neighbor:=Cell[i,j].neighbor;
end;
try
if Cell[i,j+1].alive=true then
Inc (Cell[i,j].neighbor);
except
on EAccessViolation do
Cell[i,j].neighbor:=Cell[i,j].neighbor;
end;
try
if Cell[i+1,j+1].alive=true then
Inc (Cell[i,j].neighbor);
except
on EAccessViolation do
Cell[i,j].neighbor:=Cell[i,j].neighbor;
end;
end;
end;
rules;
Inc(gen);
Form1.FieldEdit.Text:= inttostr (gen);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
generation;
end;
procedure TForm1.GenLabelClick(Sender: TObject);
begin
end;
//
// zufällige Startzivilisation erzeugen
//
procedure TForm1.RandomCheckClick(Sender: TObject);
var i,j:integer;
begin
for i:=0 to xSize do
begin
for j:=0 to ySize do
begin
Cell[i,j]:=TCell.Create(i*10,j*10);
end;
end;
end;
//
// Starten bzw. Stoppen
//
procedure TForm1.ButtonStartClick(Sender: TObject);
begin
if ButtonStart.Caption = 'Start' then
begin
Timer1.Enabled:=true;
ButtonStart.Caption:= 'Stop';
FigureBox.Enabled:= false;
RandomCheck.Enabled:= false;
ClearButton.Enabled:= false;
SizeButton.Enabled:= false;
end
else
begin
Timer1.Enabled:=false;
ButtonStart.Caption:= 'Start';
FigureBox.Enabled:= true;
RandomCheck.Enabled:= true;
ClearButton.Enabled:= true;
SizeButton.Enabled:= true;
ClearButton.Click;
end;
end;
//
// Alle Zellen "töten"
//
procedure TForm1.ClearButtonClick(Sender: TObject);
var i,j:integer;
begin
for i:=0 to xSize do
begin
for j:=0 to ySize do
begin
Cell[i,j].alive:= false;
Cell[i,j].live;
end;
end;
RandomCheck.Checked:= false;
gen:=0;
FieldEdit.Text:= inttostr (gen);
end;
procedure TForm1.FieldImageMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
draw:= false;
end;
procedure TForm1.SpeedBarChange(Sender: TObject);
begin
if SpeedBar.Position=0 then Timer1.Interval:= 1000;
if SpeedBar.Position=1 then Timer1.Interval:= 900;
if SpeedBar.Position=2 then Timer1.Interval:= 800;
if SpeedBar.Position=3 then Timer1.Interval:= 700;
if SpeedBar.Position=4 then Timer1.Interval:= 600;
if SpeedBar.Position=5 then Timer1.Interval:= 500;
if SpeedBar.Position=6 then Timer1.Interval:= 400;
if SpeedBar.Position=7 then Timer1.Interval:= 300;
if SpeedBar.Position=8 then Timer1.Interval:= 200;
if SpeedBar.Position=9 then Timer1.Interval:= 100;
if SpeedBar.Position=10 then Timer1.Interval:= 50;
end;
procedure TForm1.ButtonPauseClick(Sender: TObject);
begin
if ButtonPause.Caption='Pause' then
begin
Timer1.Enabled:=false;
ButtonPause.Caption:='Run';
end
else
begin
Timer1.Enabled:=true;
ButtonPause.Caption:= 'Pause';
end;
end;
procedure TForm1.ButtonNextClick(Sender: TObject);
begin
Timer1.Enabled:= false;
ButtonPause.Caption:='Run';
generation;
end;
procedure TForm1.SizeButtonClick(Sender: TObject);
var x,y : integer;
begin
x:= strtoint (xEdit.Text);
y:= strtoint (yEdit.Text);
if (x<3) or (y<3) or (x>60) or (y>60)
then
begin
ShowMessage('Die Maße müssen zwischen 3x3 und 60x60 liegen!');
xEdit.Text:= inttostr (xSize);
yEdit.Text:= inttostr (ySize);
end
else Size;
end;
end.