-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuMain.pas
More file actions
276 lines (241 loc) · 6.51 KB
/
uMain.pas
File metadata and controls
276 lines (241 loc) · 6.51 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
unit uMain;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, System.Generics.Collections;
type
TCoordinates = record
X: UInt8;
Y: UInt8;
end;
TMineButton = class(TBitBtn)
procedure userClick(Sender: TObject);
procedure mouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure mouseIndiDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
FisBomb: Boolean;
FbombIndicator: UInt8;
Fcoordinates: TCoordinates;
Indicator: TLabel;
Neighbours: TList<TMineButton>;
function isInBounds(Coordinate: Integer): Boolean;
public
constructor Create(AOwner: TComponent; X: UInt8; Y: UInt8; bombChance: Float32); overload;
procedure calcBombIndicator;
procedure explode;
procedure Free; overload;
procedure addNeighbour(Neighbour: TMineButton);
procedure remNeighbour(Neighbour: TMineButton);
function isBomb: Boolean;
end;
TMineField = array of array of TMineButton;
TGameForm = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
MineField: TMineField;
public
procedure explodeAll;
end;
var
GameForm: TGameForm;
implementation
{$R *.dfm}
procedure TGameForm.FormClose(Sender: TObject; var Action: TCloseAction);
var
X,Y: Integer;
begin
for Y := 0 to Length(MineField)-1 do begin
for X := 0 to Length(MineField[Y])-1 do begin
MineField[Y][X].Free;
end;
end;
SetLength(MineField,0,0);
end;
procedure TGameForm.FormCreate(Sender: TObject);
var
Y: Integer;
X: Integer;
begin
SetLength(MineField,20,20);
for Y := 0 to Length(MineField)-1 do begin
for X := 0 to Length(MineField[Y])-1 do begin
MineField[Y][X] := TMineButton.Create(Self,X,Y,0.15);
end;
end;
for Y := 0 to Length(MineField)-1 do begin
for X := 0 to Length(MineField[Y])-1 do begin
MineField[Y][X].calcBombIndicator;
end;
end;
Self.Width := MineField[Length(MineField)-1][Length(MineField[Length(MineField)-1])-1].Left+135;
Self.Height := MineField[Length(MineField)-1][Length(MineField[Length(MineField)-1])-1].Top+175;
end;
procedure TGameForm.explodeAll;
var
X,Y: Integer;
begin
for Y := 0 to Length(MineField)-1 do begin
for X := 0 to Length(MineField[Y])-1 do begin
if MineField[Y][X].isBomb then MineField[Y][X].explode;
MineField[Y][X].OnClick := nil;
end;
end;
SetLength(MineField,0,0);
end;
{ TMineButton }
constructor TMineButton.Create(AOwner: TComponent; X: UInt8; Y: UInt8; bombChance: Float32);
begin
inherited Create(AOwner);
Fcoordinates.X := X;
Fcoordinates.Y := Y;
FisBomb := Random < bombChance;
Top := Y * 50;
Left := X * 50;
Width := 50;
Height := 50;
Parent := AOwner as TWinControl;
Self.OnMouseDown := mouseDown;
Neighbours := TList<TMineButton>.Create;
//Add Neighbour references
//Links
if X > 0 then begin
GameForm.MineField[Y][X-1].addNeighbour(Self);
addNeighbour(GameForm.MineField[Y][X-1]);
end;
//Oben Links
if (X > 0) and (Y > 0) then begin
GameForm.MineField[Y-1][X-1].addNeighbour(Self);
addNeighbour(GameForm.MineField[Y-1][X-1]);
end;
//Oben
if Y > 0 then begin
GameForm.MineField[Y-1][X].addNeighbour(Self);
addNeighbour(GameForm.MineField[Y-1][X]);
end;
//Oben Rechts
if (X < Length(GameForm.MineField[0])-1) and (Y > 0) then begin
GameForm.MineField[Y-1][X+1].addNeighbour(Self);
addNeighbour(GameForm.MineField[Y-1][X+1]);
end;
end;
procedure TMineButton.addNeighbour(Neighbour: TMineButton);
begin
Neighbours.Add(Neighbour);
end;
procedure TMineButton.remNeighbour(Neighbour: TMineButton);
begin
Neighbours.Remove(Neighbour);
end;
procedure TMineButton.calcBombIndicator;
var
Neighbour: TMineButton;
begin
for Neighbour in Neighbours do
if Neighbour.isBomb then Inc(FbombIndicator);
end;
function TMineButton.isInBounds(Coordinate: Integer): Boolean;
var
MinCoord, MaxCoord: Integer;
begin
MinCoord := 0;
MaxCoord := Length(GameForm.MineField)-1;
Result := (Coordinate >= MinCoord) and (Coordinate <= MaxCoord);
end;
procedure TMineButton.userClick(Sender: TObject);
var
Neighbour: TMineButton;
function _getColor: TColor;
begin
case FbombIndicator of
1: Result := clBlue;
2: Result := clGreen;
3: Result := clRed;
4: Result := clPurple;
5: Result := clBlack;
6: Result := clGray;
7: Result := clMaroon;
8: Result := clWebTurquoise;
end;
end;
begin
if FisBomb then begin
GameForm.explodeAll;
Exit;
end;
if Enabled and (Indicator = nil) then begin
if FbombIndicator < 1 then begin
Enabled := False;
for Neighbour in Neighbours do begin
Neighbour.remNeighbour(Self);
Neighbour.userClick(Self);
end;
end else begin
Enabled := False;
Indicator := TLabel.Create(Self);
with Indicator do begin
Parent := Self;
Align := alClient;
Alignment := taCenter;
Layout := tlCenter;
Caption := IntToStr(FbombIndicator);
Font.Color := _getColor;
end;
end;
end;
end;
procedure TMineButton.explode;
var
Neighbour: TMineButton;
begin
Enabled := False;
if Indicator <> nil then FreeAndNil(Indicator);
Indicator := TLabel.Create(Self);
with Indicator do begin
Parent := Self;
Align := alClient;
Alignment := taCenter;
Layout := tlCenter;
Caption := '%';
Font.Color := clWebOrange;
end;
for Neighbour in Neighbours do Neighbour.remNeighbour(Self);
end;
procedure TMineButton.mouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbRight then begin
Indicator := TLabel.Create(Self);
with Indicator do begin
Parent := Self;
Align := alClient;
Alignment := taCenter;
Layout := tlCenter;
Caption := '#';
Font.Color := clBlack;
OnMouseDown := mouseIndiDown;
end;
end else if Button = mbLeft then begin
if Indicator = nil then userClick(Sender)
else Indicator.BringToFront;
end;
end;
procedure TMineButton.mouseIndiDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbRight then
FreeAndNil(Indicator);
end;
function TMineButton.isBomb: Boolean;
begin
Result := FisBomb;
end;
procedure TMineButton.Free;
begin
Neighbours.Clear;
Neighbours.Free;
inherited Free;
end;
end.