-
Notifications
You must be signed in to change notification settings - Fork 0
/
ugol.pas
280 lines (247 loc) · 6.51 KB
/
ugol.pas
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
(******************************************************************************)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* This file is part of Greenfoot for Lazarus *)
(* *)
(* See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(******************************************************************************)
(*
* This Unit is the Original Java to FreePascal translation from
*
* http://www.greenfoot.org/scenarios/1336
*
*
* Changelog : ver. 0.01 = 1:1 Translation
* ver. 0.02 = Minor Changes
*
*)
Unit ugol;
{$MODE objfpc}{$H+}
Interface
Uses
SysUtils,
ugreenfoot, OpenGLContext;
Type
{ TCell }
TCell = Class(TActor) // Müsste fertig sein.
private
mystate, mylaststate: Boolean;
myFirstTime: Boolean;
mynumNeighbors: integer;
mybodysize: integer;
myNeighbors: TActorList;
public
Constructor create(); override;
Constructor create(cellsize: integer); virtual;
// Destructor destroy(); override;
Procedure saveLastState();
Function getState(): Boolean;
Procedure flipstate();
Procedure SetState(state: Boolean);
Function countNeighbors(): integer;
Procedure act(); override;
Procedure ShowState;
Procedure ApplyRule(); virtual;
End;
{ TGameOfLifeCell }
TGameOfLifeCell = Class(TCell)
private
public
Constructor create(cellsize: integer); override;
Procedure ApplyRule(); override;
End;
{ TCellularAutomata }
TCellularAutomata = Class(TWorld) // Müsste fertig sein.
private
mycells: Array Of Array Of TCell;
mywidth, myheight, mycellsize: integer;
Procedure CreateCells();
Procedure setInitCellState(Const Cell: TCell);
public
Constructor create(Parent: TOpenGLControl);
Procedure Act(); override;
End;
Implementation
{ TGameOfLifeCell }
Constructor TGameOfLifeCell.create(cellsize: integer);
Begin
Inherited create(cellsize);
End;
Procedure TGameOfLifeCell.ApplyRule;
Var
numNeighborsOn: integer;
state: Boolean;
Begin
numNeighborsOn := countNeighbors();
state := mystate;
If (numNeighborsOn < 2) Then Begin
state := false;
End
Else Begin
If (numNeighborsOn > 3) Then Begin
state := false;
End
Else Begin
If (numNeighborsOn = 3) Then Begin
state := True;
End
End;
End;
mystate := state;
End;
{ TCell }
Constructor TCell.create;
Begin
create(10);
End;
Constructor TCell.create(cellsize: integer);
Var
img: TGreenfootImage;
Begin
Inherited create;
myFirstTime := true;
mystate := false;
mylaststate := false;
img := GreenFootGraphicEngine.FindImage('gol_alive');
If Not assigned(img) Then Begin
img := TGreenfootImage.Create(cellsize, cellsize);
img.BeginUpdate();
img.clear();
img.SetColor(WHITE);
img.fillRect(1, 1, cellsize - 2, cellsize - 2);
img.EndUpdate();
GreenFootGraphicEngine.AddImage(img, 'gol_alive');
End;
img := GreenFootGraphicEngine.FindImage('gol_dead');
If Not assigned(img) Then Begin
img := TGreenfootImage.Create(cellsize, cellsize);
img.BeginUpdate();
img.clear();
img.SetColor(black);
img.fillRect(1, 1, cellsize - 2, cellsize - 2);
img.EndUpdate();
GreenFootGraphicEngine.AddImage(img, 'gol_dead');
End;
setImage(img);
myBodySize := cellSize - 2;
showState();
End;
Procedure TCell.saveLastState;
Begin
mylaststate := mystate;
End;
Function TCell.getState: Boolean;
Begin
result := mystate;
End;
Procedure TCell.flipstate;
Begin
SetState(Not mystate);
End;
Procedure TCell.SetState(state: Boolean);
Begin
mystate := state;
ShowState;
End;
Function TCell.countNeighbors: integer;
Var
sum: integer;
i: Integer;
Begin
sum := 0;
For i := 0 To myNumNeighbors - 1 Do Begin
If (TCell(myNeighbors[i]).myLastState) Then
inc(sum);
End;
result := sum;
End;
Procedure TCell.act;
Var
neighbors: TActorList;
i: Integer;
Begin
If (myFirstTime) Then Begin
neighbors := getNeighbours(1, true, self.ClassType);
myNumNeighbors := length(neighbors);
// myNeighbors = new Cell[myNumNeighbors];
setlength(myNeighbors, myNumNeighbors);
For i := 0 To myNumNeighbors - 1 Do Begin
myNeighbors[i] := TCell(neighbors[i]);
End;
myFirstTime := false;
End;
applyRule();
showState();
End;
Procedure TCell.ShowState;
Begin
If mystate Then Begin
setImage(GreenFootGraphicEngine.FindImage('gol_alive'));
End
Else Begin
setImage(GreenFootGraphicEngine.FindImage('gol_dead'));
End;
End;
Procedure TCell.ApplyRule;
Begin
End;
{ TCellularAutomata }
Procedure TCellularAutomata.CreateCells();
Var
i: Integer;
c: TCell;
j: Integer;
Begin
SetLength(mycells, myHeight, mywidth);
For i := 0 To myheight - 1 Do
For j := 0 To mywidth - 1 Do Begin
c := TGameOfLifeCell.create(mycellsize); // Hier wird festgelegt was für eine Art von Zelle das Level befölkern soll
mycells[i, j] := c;
setInitCellState(c);
addObject(c, i, j);
End;
End;
Procedure TCellularAutomata.setInitCellState(Const Cell: TCell);
Begin
If getRandomNumber(4) = 0 Then Begin
cell.setstate(true);
End;
End;
Constructor TCellularAutomata.create(Parent: TOpenGLControl);
Var
img: TGreenfootImage;
Begin
Inherited create(parent, 50, 50, 10);
myheight := getHeight();
mywidth := getWidth();
mycellsize := getCellSize();
img := TGreenfootImage.Create(mycellsize, mycellsize);
img.BeginUpdate();
img.clear();
img.SetColor(RED);
img.drawLine(0, 0, mywidth - 1, 0);
img.drawLine(0, 0, 0, myheight - 1);
img.EndUpdate();
setBackground(img);
CreateCells();
SetSpeed(10);
End;
Procedure TCellularAutomata.Act();
Var
i: Integer;
j: Integer;
Begin
For i := 0 To myHeight - 1 Do Begin
For j := 0 To myWidth - 1 Do Begin
myCells[i][j].saveLastState();
End;
End;
End;
End.