-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfLifeUtils.pas
126 lines (110 loc) · 3.2 KB
/
GameOfLifeUtils.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
unit GameOfLifeUtils;
interface
uses
Vcl.Grids, Vcl.Forms, Vcl.Dialogs, SysUtils, Vcl.Graphics, Math;
type
TAutomataCell = Record
IsAlive: Boolean;
End;
TAutomataCellArray = Array of Array of TAutomataCell;
TGameOfLifeUtils = class
class procedure ResizeGrid(MainForm: TForm; MainGrid: TDrawGrid);
class procedure PopulateCellArray(MainGrid: TDrawGrid; var Cells: TAutomataCellArray);
class function IsAliveNextStep(MainGrid: TDrawGrid; Cells: TAutomataCellArray; Y, X: Integer): Boolean;
class procedure GenocideCells(MainGrid: TDrawGrid; var Cells: TAutomataCellArray);
class procedure RandomiseCells(MainGrid: TDrawGrid; var Cells: TAutomataCellArray);
end;
implementation
class procedure TGameOfLifeUtils.ResizeGrid(MainForm: TForm; MainGrid: TDrawGrid);
begin
MainGrid.ColCount := Round(MainForm.Width / MainGrid.DefaultColWidth);
MainGrid.RowCount := Round(MainForm.Height / MainGrid.DefaultRowHeight);
end;
class procedure TGameOfLifeUtils.PopulateCellArray(MainGrid: TDrawGrid; var Cells: TAutomataCellArray);
var
Col, Row, Rand: Integer;
WillBeAlive: Boolean;
begin
SetLength(Cells, MainGrid.RowCount);
for Row := 0 to MainGrid.RowCount - 1 do
SetLength(Cells[Row], MainGrid.ColCount);
end;
class function TGameOfLifeUtils.IsAliveNextStep(MainGrid: TDrawGrid; Cells: TAutomataCellArray; Y, X: Integer): Boolean;
var
Col, Row, XMin, XMax, YMin, YMax, LiveNeighbourCount: Integer;
CurrentlyAlive: Boolean;
begin
CurrentlyAlive := Cells[Y, X].IsAlive;
LiveNeighbourCount := 0;
XMin := Max(0, X - 1);
XMax := Min(X + 1, MainGrid.ColCount - 1);
YMin := Max(0, Y - 1);
YMax := Min(Y + 1, MainGrid.RowCount - 1);
for Row := YMin to YMax do
begin
for Col := XMin to XMax do
begin
if (Row <> Y) or (Col <> X) then
begin
if Cells[Row, Col].IsAlive then
LiveNeighbourCount := LiveNeighbourCount + 1;
if LiveNeighbourCount > 3 then
begin
Result := False;
Exit;
end;
end;
end;
end;
case CurrentlyAlive of
True:
begin
if LiveNeighbourCount < 2 then
Result := False
else if LiveNeighbourCount > 3 then
Result := False
else
Result := True
end;
False:
begin
if LiveNeighbourCount = 3 then
Result := True
else
Result := False
end;
end;
end;
class procedure TGameOfLifeUtils.GenocideCells(MainGrid: TDrawGrid; var Cells: TAutomataCellArray);
var
Col, Row: Integer;
begin
for Row := 0 to MainGrid.RowCount - 1 do
begin
for Col := 0 to MainGrid.ColCount - 1 do
begin
Cells[Row, Col].IsAlive := False;
MainGrid.Canvas.Brush.Color := clWhite;
MainGrid.Canvas.FillRect(MainGrid.CellRect(Col, Row));
end;
end;
end;
class procedure TGameOfLifeUtils.RandomiseCells(MainGrid: TDrawGrid; var Cells: TAutomataCellArray);
var
Col, Row, Rand: Integer;
begin
for Row := 0 to MainGrid.RowCount - 1 do
begin
for Col := 0 to MainGrid.ColCount - 1 do
begin
Rand := Random(2);
case Rand of
0:
Cells[Row, Col].IsAlive := False;
1:
Cells[Row, Col].IsAlive := True;
end;
end;
end;
end;
end.