-
Notifications
You must be signed in to change notification settings - Fork 0
/
uminesweeper.pas
402 lines (355 loc) · 9.69 KB
/
uminesweeper.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
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
(******************************************************************************)
(* *)
(* 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/7992
*
*
* Changelog : ver. 0.01 = 1:1 Translation
* ver. 0.02 = Diverse Bugfixes (z.B. 1. Klick erzeugt die welt, so dass es keine Bombe unter der Maus gibt).
*
*)
Unit uminesweeper;
{$MODE objfpc}{$H+}
Interface
Uses
SysUtils,
ugreenfoot, OpenGLContext;
Type
{ TBomb }
TBomb = Class(TActor)
private
exploded: boolean;
public
Constructor create(); override;
Procedure Boom();
End;
{ TCover }
TCover = Class(TActor)
private
Procedure Open();
public
Constructor create(); override;
Procedure Act(); override;
End;
{ TFlag }
TFlag = Class(TActor)
private
public
Constructor create(); override;
Procedure Act(); override;
End;
{ TCount }
TCount = Class(TActor)
private
count: integer;
public
Constructor create(); override;
Procedure AddedtoWorld(Const world: Tworld); override;
Function getCount(): integer;
End;
{ TQuestion }
TQuestion = Class(TActor)
private
public
Constructor create(); override;
Procedure Act(); override;
End;
{ TMineField }
TMineField = Class(TWorld)
private
fBombCount: integer;
Initialized: Boolean;
Procedure checkGameOver();
Procedure addBombs(count, keepoutx, keepouty: integer);
Procedure addCounts();
Procedure addCovers();
public
Constructor create(Parent: TOpenGLControl; FieldWidth, FieldHeight, BombCount: integer);
Procedure Act(); override;
End;
Implementation
{ TQuestion }
Constructor TQuestion.create;
Var
image: TGreenfootImage;
Begin
Inherited create;
image := GreenFootGraphicEngine.FindImage('mquestion.png');
If Not Assigned(image) Then Begin
image := TGreenfootImage.Create('images' + PathDelim + 'mquestion.png');
GreenFootGraphicEngine.AddImage(image, 'mquestion.png');
End;
setImage(image);
End;
Procedure TQuestion.Act;
Var
mouse: TMouseInfo;
Begin
If (mouseClicked(self)) Then Begin
mouse := MouseInfo;
If (mouse.getButton() = MB_Right) Then Begin
getWorld().removeObject(self);
End;
End;
End;
{ TCount }
Constructor TCount.create;
Begin
Inherited create;
count := -1;
End;
Procedure TCount.AddedtoWorld(Const world: Tworld);
Var
bombs: TActorList;
image: TGreenfootImage;
Begin
Inherited AddedtoWorld(world);
bombs := getNeighbours(1, true, TBomb);
count := high(bombs) + 1;
If count = 0 Then Begin
world.removeObject(self);
End
Else Begin
image := GreenFootGraphicEngine.FindImage('mcount' + inttostr(count) + '.png');
If Not assigned(image) Then Begin
image := TGreenfootImage.Create('images' + PathDelim + 'mcount' + inttostr(count) + '.png');
GreenFootGraphicEngine.AddImage(image, 'mcount' + inttostr(count) + '.png');
End;
setimage(image);
End;
End;
Function TCount.getCount: integer;
Begin
result := count;
End;
{ TFlag }
Constructor TFlag.create;
Var
image: TGreenfootImage;
Begin
Inherited create;
image := GreenFootGraphicEngine.FindImage('mflag.png');
If Not Assigned(image) Then Begin
image := TGreenfootImage.Create('images' + PathDelim + 'mflag.png');
GreenFootGraphicEngine.AddImage(image, 'mflag.png');
End;
setImage(image);
End;
Procedure TFlag.Act;
Var
mouse: TMouseInfo;
Begin
If (mouseClicked(self)) Then Begin
mouse := MouseInfo;
If (mouse.getButton() = MB_Right) Then Begin
getWorld().addObject(tQuestion.create(), getX(), getY());
getWorld().removeObject(self);
End;
End;
End;
{ TCover }
Procedure TCover.Open;
Var
flag: TFlag;
bomb: TBomb;
count: TActor;
covers: TActorList;
Question: TQuestion;
i: integer;
Begin
If getWorld() = Nil Then exit;
flag := TFlag(getOneIntersectingObject(TFlag));
question := TQuestion(getOneIntersectingObject(tQuestion));
If (assigned(flag) Or assigned(question)) Then Begin
exit;
End;
bomb := TBomb(getOneIntersectingObject(TBomb));
If assigned(bomb) Then Begin
getWorld().removeObject(self);
bomb.boom();
exit;
End;
count := getOneIntersectingObject(TCount);
If assigned(count) Then Begin
getWorld().removeObject(self);
exit;
End;
covers := getNeighbours(1, true, TCover);
getWorld().removeObject(self);
For i := 0 To high(covers) Do Begin
If Not (Covers[i] Is TCover) Then Begin
Raise exception.Create('asd');
End;
TCover(Covers[i]).Open();
End;
End;
Constructor TCover.create;
Var
cover: TGreenfootImage;
Begin
Inherited create;
cover := GreenFootGraphicEngine.FindImage('mcover.png');
If Not assigned(cover) Then Begin
cover := TGreenfootImage.Create('images' + PathDelim + 'mcover.png');
GreenFootGraphicEngine.AddImage(cover, 'mcover.png');
End;
setimage(cover);
End;
Procedure TCover.Act;
Var
mouse: TMouseInfo;
Begin
If (mouseClicked(self)) Then Begin
mouse := MouseInfo;
If (mouse.getButton() = MB_Left) Then Begin
open();
End
Else Begin
If (mouse.getButton() = MB_Right) Then Begin
getWorld().addObject(tFlag.create(), getX(), getY());
End;
End;
End;
End;
{ TBomb }
Constructor TBomb.create;
Var
image: TGreenfootImage;
Begin
Inherited create;
Exploded := false;
image := GreenFootGraphicEngine.FindImage('mbomb.png');
If Not Assigned(image) Then Begin
image := TGreenfootImage.Create('images' + PathDelim + 'mbomb.png');
GreenFootGraphicEngine.AddImage(image, 'mbomb.png');
End;
setImage(image);
End;
Procedure TBomb.Boom;
Var
questions, covers, flags, bombs: TActorList;
i: Integer;
image: TGreenfootImage;
Begin
exploded := true;
bombs := getWorld().getObjects(TBomb);
// Alle Bomben Anzeigen.
For i := 0 To high(bombs) Do Begin
flags := getWorld().getObjectsAt(bombs[i].getx, bombs[i].gety, TFlag);
getWorld().removeObjects(flags);
covers := getworld().getObjectsAt(bombs[i].getx, bombs[i].gety, TCover);
getWorld().removeObjects(covers);
questions := getworld().getObjectsAt(bombs[i].getx, bombs[i].gety, TQuestion);
getWorld().removeObjects(questions);
End;
image := GreenFootGraphicEngine.FindImage('mboom.png');
If Not assigned(image) Then Begin
image := TGreenfootImage.Create('images' + PathDelim + 'mboom.png');
GreenFootGraphicEngine.AddImage(image, 'mboom.png');
End;
setimage(image);
stop;
End;
{ TMineField }
Procedure TMineField.checkGameOver;
Var
bombs: TActorList;
numBombs, numCovers: integer;
b: Boolean;
i: Integer;
Begin
(*
* Das Spiel ist beendet, wenn entweder nur noch Bomben verdeckt sind, oder eine Bombe gezündet wurde ( der 2. Fall verhindert das weiterspielen nach einer Explosion )
*)
bombs := getObjects(TBomb);
numBombs := length(bombs);
numCovers := length(getObjects(TCover));
b := false;
For i := 0 To high(bombs) Do Begin
If TBomb(bombs[i]).exploded Then Begin
b := true;
break;
End;
End;
If (numBombs = numCovers) Or b Then Begin
stop();
End;
End;
Procedure TMineField.addBombs(count, keepoutx, keepouty: integer);
Var
x, y, i: integer;
Begin
For i := 0 To count - 1 Do Begin
x := -1;
y := -1;
// Die Bomben so erstellen, dass sie überall da sind, wo der User nicht hingeklickt hat.
Repeat
x := getRandomNumber(getWidth());
y := getRandomNumber(getHeight());
Until (high(getObjectsAt(x, y, TBomb)) = -1) And (Not ((x = keepoutx) And (y = keepouty)));
addObject(TBomb.create(), x, y);
End;
End;
Procedure TMineField.addCounts;
Var
x, y: integer;
Begin
For x := 0 To getWidth() - 1 Do Begin
For y := 0 To getHeight() - 1 Do Begin
If (high(getObjectsAt(x, y, TBomb)) = -1) Then Begin
addObject(TCount.create(), x, y);
End;
End;
End;
End;
Procedure TMineField.addCovers;
Var
x, y: integer;
Begin
For x := 0 To getWidth() - 1 Do Begin
For y := 0 To getHeight() - 1 Do Begin
addObject(tCover.create(), x, y);
End;
End;
End;
Constructor TMineField.create(Parent: TOpenGLControl; FieldWidth, FieldHeight, BombCount: integer);
Begin
If FieldWidth * FieldHeight <= BombCount Then Begin
Raise exception.create('Error to much bombs for this field.');
End;
Inherited create(parent, FieldWidth, FieldHeight, 16);
fBombCount := BombCount;
setBackground('images' + PathDelim + 'mgrid.png');
setPaintOrder([TQuestion, TFlag, TCover, TBomb]);
addCovers();
End;
Procedure TMineField.Act;
Var
mouse: TMouseInfo;
x, y: integer;
Begin
If Not Initialized And mouseClicked(Nil) Then Begin
Initialized := true;
mouse := MouseInfo;
x := mouse.getX();
y := mouse.getY();
addBombs(fBombCount, x, y);
addCounts();
End;
checkGameOver();
End;
End.