-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnit1.pas
More file actions
1411 lines (1230 loc) · 47.7 KB
/
Unit1.pas
File metadata and controls
1411 lines (1230 loc) · 47.7 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit Unit1;
{HANDMODIFIED COMMENT ADDED
March 30 2025
Entry for Minesweeper contest.
Code produced by Google Gemini 2.5 Pro by successively giving it these prompts:
"Make a complete minesweeper game in Delphi.
It must contain animated graphics and keep track of score,
contain a nice intro and a nice failure and success end
animation/screen."
Providing additional hint by followup prompt:
"Produce the dfm file too"
Providing additional hint by followup prompt:
"Change the code to not use predefined images,
but instead draw them using drawing primitives.
That includes all graphics and animations."
Providing additional hint by followup prompt:
"DFM files can't contain comments.
And TMainMenu does not have a height since it does not count as part
of the client area."
Which produced the current code, including DFM file.
The code could not compile without simple changes, which
I could have told it to make Im sure, but I instead decided to
mark the changes with {HANDMODIFIED....
Two types of mistakes was made:
1) thinking that InflateRect is a function. It is a procedure.
so requires to be on a separate line. Eg.
DrawFlag(ACanvas, InflateRect(DestRect, -BEVEL_WIDTH-1, -BEVEL_WIDTH-1));
2) not specifying correct paranthesis for a statement:
else if FGameState in [gsWon, gsLost] and (FEndTime > FStartTime) then
Which fails because of missing paranthesis around the FGameState in [...] part.
In addition it forgot to link the imgBoardMouseDown event handler, that it did produce
to the actual eventhandler OnMouseDown of the imgBoard control.
With those fixes, the code runs and works pretty well.
It seems to me that, with an aggregated prompt, and a little fine tuning,
it might very well have produced a fully functioning Delphi Minesweeper game
without further human interaction needed, except for copy/paste/compile/run.
Kim Bo Madsen
Components4Developers
www.components4developers.com
House of components for serious applications
}
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls,
Vcl.Menus, System.Math, Vcl.ComCtrls, System.Types, // Added System.Types for TPoint
System.UIConsts; // Added for standard colors like clBtnFace etc.
type
TGameState = (gsIntro, gsReady, gsPlaying, gsWon, gsLost, gsAnimating);
// NEW: State for the smiley face
TSmileyState = (ssNormal, ssOoh, ssWin, ssLost);
TCell = record
IsMine: Boolean;
IsRevealed: Boolean;
IsFlagged: Boolean;
AdjacentMines: Integer;
end;
TDifficulty = (dEasy, dMedium, dHard);
TMainForm = class(TForm)
imgBoard: TImage;
StatusBar1: TStatusBar;
GameTimer: TTimer;
AnimationTimer: TTimer;
MainMenu1: TMainMenu;
FileMenu: TMenuItem;
NGame: TMenuItem;
DifficultyMenu: TMenuItem;
EasyItem: TMenuItem;
MediumItem: TMenuItem;
HardItem: TMenuItem;
ExitItem: TMenuItem;
imgSmiley: TImage; // Will draw directly onto this
imgOverlay: TImage; // For intro/outro screens - drawn directly
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject); // Keep for freeing lists etc.
procedure imgBoardMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure GameTimerTimer(Sender: TObject);
procedure AnimationTimerTimer(Sender: TObject);
procedure NGameClick(Sender: TObject);
procedure EasyItemClick(Sender: TObject);
procedure MediumItemClick(Sender: TObject);
procedure HardItemClick(Sender: TObject);
procedure ExitItemClick(Sender: TObject);
procedure imgSmileyClick(Sender: TObject);
procedure imgOverlayClick(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure imgBoardPaint(Sender: TObject); // Use OnPaint for direct drawing if preferred
procedure imgSmileyPaint(Sender: TObject); // Use OnPaint for smiley
private
{ Private declarations }
FGrid: array of array of TCell;
FGridWidth, FGridHeight: Integer;
FMineCount: Integer;
FCellSize: Integer;
FGameState: TGameState;
FFirstClick: Boolean;
FStartTime: TDateTime;
FEndTime: TDateTime;
FRevealedCount: Integer;
FFlagsPlaced: Integer;
FGameLostMineX, FGameLostMineY: Integer;
FCurrentDifficulty: TDifficulty;
FCurrentSmileyState: TSmileyState; // Track current smiley state
// Graphics Resources REMOVED - No longer needed
// FTiles: TBitmap;
// FSmileyNormal, FSmileyOoh, FSmileyWin, FSmileyLost: TBitmap;
// FIntroBitmap, FWinBitmap, FLostBitmap: TBitmap;
// For animation:
FAnimationStep: Integer;
FCellsToReveal: TList; // Still used for loss animation logic
// Graphics drawing procedures
procedure DrawBevel(ACanvas: TCanvas; R: TRect; Width: Integer; Up: Boolean);
procedure DrawTilePrimitive(ACanvas: TCanvas; DestRect: TRect; TileIndex: Integer);
procedure DrawSmileyFace(ACanvas: TCanvas; DestRect: TRect; State: TSmileyState);
procedure DrawIntroScreen(ACanvas: TCanvas; Width, Height: Integer; Step: Integer);
procedure DrawWinScreen(ACanvas: TCanvas; Width, Height: Integer; Step: Integer);
procedure DrawLossScreen(ACanvas: TCanvas; Width, Height: Integer; Step: Integer);
procedure DrawFlag(ACanvas: TCanvas; R: TRect);
procedure DrawMine(ACanvas: TCanvas; R: TRect; IsHit: Boolean = False);
procedure DrawWrongMine(ACanvas: TCanvas; R: TRect);
// Game Logic procedures (mostly unchanged internally, but drawing calls modified)
procedure SetupGame(ADifficulty: TDifficulty);
procedure InitializeGrid;
procedure PlaceMines(ExcludeX, ExcludeY: Integer);
procedure CalculateAdjacentMines;
procedure DrawBoard; // Will now call DrawTilePrimitive
procedure DrawCell(ACanvas: TCanvas; GridX, GridY: Integer); // Calls DrawTilePrimitive
// procedure DrawTile(...) REMOVED - Replaced by DrawTilePrimitive
procedure RevealCell(GridX, GridY: Integer);
procedure FloodFill(GridX, GridY: Integer);
procedure CheckWinCondition;
procedure GameOver(Win: Boolean);
procedure UpdateStatusBar;
procedure UpdateSmiley(Scared: Boolean = False); // Will call DrawSmileyFace
procedure SetDifficultyCheckmark(ADifficulty: TDifficulty);
procedure StartIntro;
procedure StartWinAnimation;
procedure StartLossAnimation;
function GetTileIndex(GridX, GridY: Integer): Integer;
function IsValidCoord(GridX, GridY: Integer): Boolean;
procedure CenterBoard;
procedure EnsureImageBitmaps; // Helper to make sure TImage bitmaps exist
public
{ Public declarations }
end;
const
// Tile indices remain the same conceptually
TILE_COVERED = 0;
TILE_FLAG = 1;
TILE_QUESTION = 2; // Keep index, but maybe don't implement drawing
TILE_0 = 3;
TILE_1 = 4;
TILE_2 = 5;
TILE_3 = 6;
TILE_4 = 7;
TILE_5 = 8;
TILE_6 = 9;
TILE_7 = 10;
TILE_8 = 11;
TILE_MINE = 12;
TILE_MINE_HIT = 13;
TILE_MINE_WRONG = 14;
DEFAULT_CELL_SIZE = 24; // Adjusted default size for primitive drawing
BEVEL_WIDTH = 2; // How many pixels for the 3D bevel effect
var
MainForm: TMainForm;
implementation
{$R *.dfm} // Make sure the DFM matches the structure
uses System.DateUtils; // For SecondsBetween
// --- Initialization and Cleanup ---
procedure TMainForm.FormCreate(Sender: TObject);
begin
// Double Buffering still useful to avoid flicker when drawing primitives
imgBoard.ControlStyle := imgBoard.ControlStyle + [csOpaque];
imgSmiley.ControlStyle := imgSmiley.ControlStyle + [csOpaque];
imgOverlay.ControlStyle := imgOverlay.ControlStyle + [csOpaque];
FCellSize := DEFAULT_CELL_SIZE;
FCellsToReveal := TList.Create;
Randomize;
// LoadGraphics REMOVED
FCurrentDifficulty := dEasy;
SetDifficultyCheckmark(FCurrentDifficulty);
// Adjust Smiley Size based on constants (doesn't rely on loaded bitmap anymore)
imgSmiley.Width := FCellSize * 2; // Example size
imgSmiley.Height := FCellSize * 2;
imgSmiley.AutoSize := False; // Important: We control the size now
StartIntro;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
var
i: Integer;
ptr: Pointer;
begin
// Free any remaining pointers in the list if animation was interrupted
for i := FCellsToReveal.Count - 1 downto 0 do
begin
ptr := FCellsToReveal[i];
FreeMem(ptr);
end;
FCellsToReveal.Free;
// FreeGraphics REMOVED
end;
// --- NEW: Graphics Primitive Drawing Functions ---
// Helper to draw simple 3D bevels
procedure TMainForm.DrawBevel(ACanvas: TCanvas; R: TRect; Width: Integer; Up: Boolean);
var
i: Integer;
TopLeftColor, BottomRightColor: TColor;
begin
if Up then // Raised bevel
begin
TopLeftColor := clBtnHighlight;
BottomRightColor := clBtnShadow;
end else // Lowered bevel
begin
TopLeftColor := clBtnShadow;
BottomRightColor := clBtnHighlight;
end;
ACanvas.Pen.Width := 1;
for i := 0 to Width - 1 do
begin
// Top and Left lines
ACanvas.Pen.Color := TopLeftColor;
ACanvas.MoveTo(R.Left + i, R.Bottom - 1 - i);
ACanvas.LineTo(R.Left + i, R.Top + i);
ACanvas.LineTo(R.Right - 1 - i, R.Top + i);
// Bottom and Right lines
ACanvas.Pen.Color := BottomRightColor;
ACanvas.MoveTo(R.Right - 1 - i, R.Top + i);
ACanvas.LineTo(R.Right - 1 - i, R.Bottom - 1 - i);
ACanvas.LineTo(R.Left + i, R.Bottom - 1 - i);
end;
end;
// Draws a Flag primitive
procedure TMainForm.DrawFlag(ACanvas: TCanvas; R: TRect);
var
PoleX, MidY: Integer;
FlagPoints: array[0..2] of TPoint;
begin
PoleX := R.Left + R.Width div 2;
MidY := R.Top + R.Height div 2;
// Pole
ACanvas.Pen.Color := clBlack;
ACanvas.Pen.Width := Max(1, R.Width div 10); // Scale pole width
ACanvas.MoveTo(PoleX, R.Top + 2);
ACanvas.LineTo(PoleX, R.Bottom - 2);
// Base
ACanvas.MoveTo(R.Left + 2, R.Bottom - 3);
ACanvas.LineTo(R.Right - 2, R.Bottom - 3);
// Flag (Triangle)
FlagPoints[0] := Point(PoleX, R.Top + 3);
FlagPoints[1] := Point(R.Left + 3, MidY - 2);
FlagPoints[2] := Point(PoleX, MidY + 1);
ACanvas.Brush.Color := clRed;
ACanvas.Pen.Color := clBlack; // Outline flag
ACanvas.Pen.Width := 1;
ACanvas.Polygon(FlagPoints);
end;
// Draws a Mine primitive
procedure TMainForm.DrawMine(ACanvas: TCanvas; R: TRect; IsHit: Boolean = False);
var
CenterX, CenterY, Radius, SmallRadius: Integer;
begin
CenterX := R.Left + R.Width div 2;
CenterY := R.Top + R.Height div 2;
Radius := Min(R.Width, R.Height) * 2 div 5; // 40% radius
SmallRadius := Radius div 4;
// Background if hit
if IsHit then
begin
ACanvas.Brush.Color := clRed;
ACanvas.FillRect(R);
end;
// Main body
ACanvas.Brush.Color := clBlack;
ACanvas.Pen.Color := clBlack;
ACanvas.Ellipse(CenterX - Radius, CenterY - Radius, CenterX + Radius, CenterY + Radius);
// Simple "spikes"
ACanvas.Pen.Width := Max(1, R.Width div 12);
ACanvas.MoveTo(CenterX, R.Top + 2); ACanvas.LineTo(CenterX, R.Bottom - 2); // Vertical
ACanvas.MoveTo(R.Left + 2, CenterY); ACanvas.LineTo(R.Right - 2, CenterY); // Horizontal
ACanvas.MoveTo(R.Left + 3, R.Top + 3); ACanvas.LineTo(R.Right - 3, R.Bottom - 3); // Diagonal
ACanvas.MoveTo(R.Left + 3, R.Bottom - 3); ACanvas.LineTo(R.Right - 3, R.Top + 3); // Diagonal
ACanvas.Pen.Width := 1; // Reset pen width
// Optional center highlight
ACanvas.Brush.Color := clWhite;
ACanvas.Pen.Color := clWhite;
ACanvas.Ellipse(CenterX - SmallRadius, CenterY - SmallRadius, CenterX + SmallRadius, CenterY + SmallRadius);
end;
// Draws a Mine with a Red X over it
procedure TMainForm.DrawWrongMine(ACanvas: TCanvas; R: TRect);
begin
// Draw the regular mine first
DrawMine(ACanvas, R, False);
// Draw Red X
ACanvas.Pen.Color := clRed;
ACanvas.Pen.Width := Max(2, R.Width div 8); // Thicker X
ACanvas.MoveTo(R.Left + 2, R.Top + 2);
ACanvas.LineTo(R.Right - 2, R.Bottom - 2);
ACanvas.MoveTo(R.Left + 2, R.Bottom - 2);
ACanvas.LineTo(R.Right - 2, R.Top + 2);
ACanvas.Pen.Width := 1; // Reset
end;
// The main replacement for DrawTile using a bitmap strip
procedure TMainForm.DrawTilePrimitive(ACanvas: TCanvas; DestRect: TRect; TileIndex: Integer);
var
NumStr: string;
NumColor: TColor;
TextRect: TRect;
TextWidth, TextHeight: Integer;
OldBkMode: Integer;
begin
// Common setup
ACanvas.Font.Name := 'Arial'; // Or Tahoma
ACanvas.Font.Size := Max(8, DestRect.Height * 2 div 4); // Scale font size
ACanvas.Font.Style := [fsBold];
TextRect := DestRect; // Copy rect for text centering
case TileIndex of
TILE_COVERED:
begin
ACanvas.Brush.Color := clBtnFace;
ACanvas.FillRect(DestRect);
DrawBevel(ACanvas, DestRect, BEVEL_WIDTH, True); // Raised
end;
TILE_FLAG:
begin
// Draw covered base first
ACanvas.Brush.Color := clBtnFace;
ACanvas.FillRect(DestRect);
DrawBevel(ACanvas, DestRect, BEVEL_WIDTH, True);
// Draw flag on top
{HANDMODIFIED WAS
DrawFlag(ACanvas, InflateRect(DestRect, -BEVEL_WIDTH-1, -BEVEL_WIDTH-1));
}
InflateRect(DestRect, -BEVEL_WIDTH-1, -BEVEL_WIDTH-1);
DrawFlag(ACanvas, DestRect);
end;
TILE_0: // Revealed empty cell
begin
ACanvas.Brush.Color := clSilver; // Or a slightly darker gray
ACanvas.FillRect(DestRect);
DrawBevel(ACanvas, DestRect, 1, False); // Sunken slightly
end;
TILE_1..TILE_8:
begin
// Draw sunken base
ACanvas.Brush.Color := clSilver;
ACanvas.FillRect(DestRect);
DrawBevel(ACanvas, DestRect, 1, False); // Sunken slightly
// Determine Number Color
case TileIndex of
TILE_1: NumColor := clBlue;
TILE_2: NumColor := TColor($008000); // Dark Green
TILE_3: NumColor := clRed;
TILE_4: NumColor := TColor($800000); // Dark Blue (Navy)
TILE_5: NumColor := TColor($000080); // Maroon
TILE_6: NumColor := TColor($808000); // Teal
TILE_7: NumColor := clBlack;
TILE_8: NumColor := clGray;
else
NumColor := clBlack;
end;
NumStr := IntToStr(TileIndex - TILE_0);
ACanvas.Font.Color := NumColor;
// Center Text
TextWidth := ACanvas.TextWidth(NumStr);
TextHeight := ACanvas.TextHeight(NumStr);
TextRect.Left := DestRect.Left + (DestRect.Width - TextWidth) div 2;
TextRect.Top := DestRect.Top + (DestRect.Height - TextHeight) div 2;
OldBkMode := SetBkMode(ACanvas.Handle, TRANSPARENT); // Draw text transparently
ACanvas.TextRect(TextRect, TextRect.Left, TextRect.Top, NumStr);
SetBkMode(ACanvas.Handle, OldBkMode); // Restore background mode
end;
TILE_MINE:
begin
ACanvas.Brush.Color := clSilver; // Background for non-hit mine shown at end
ACanvas.FillRect(DestRect);
DrawBevel(ACanvas, DestRect, 1, False);
{HANDMODIFIED WAS
DrawMine(ACanvas, InflateRect(DestRect, -2, -2));
}
InflateRect(DestRect, -2, -2);
DrawMine(ACanvas, DestRect);
end;
TILE_MINE_HIT:
begin
// Base is already drawn by DrawMine with IsHit=True
DrawMine(ACanvas, DestRect, True);
end;
TILE_MINE_WRONG:
begin
ACanvas.Brush.Color := clSilver;
ACanvas.FillRect(DestRect);
DrawBevel(ACanvas, DestRect, 1, False);
{HANDMODIFIED WAS
DrawWrongMine(ACanvas, InflateRect(DestRect, -2, -2));
}
InflateRect(DestRect, -2, -2);
DrawWrongMine(ACanvas, DestRect);
end;
// TILE_QUESTION: // Optional implementation
// begin
// // Draw covered tile base
// ACanvas.Brush.Color := clBtnFace;
// ACanvas.FillRect(DestRect);
// DrawBevel(ACanvas, DestRect, BEVEL_WIDTH, True);
// // Draw '?'
// ACanvas.Font.Color := clBlack;
// NumStr := '?';
// TextWidth := ACanvas.TextWidth(NumStr);
// TextHeight := ACanvas.TextHeight(NumStr);
// TextRect.Left := DestRect.Left + (DestRect.Width - TextWidth) div 2;
// TextRect.Top := DestRect.Top + (DestRect.Height - TextHeight) div 2;
// OldBkMode := SetBkMode(ACanvas.Handle, TRANSPARENT);
// ACanvas.TextRect(TextRect, TextRect.Left, TextRect.Top, NumStr);
// SetBkMode(ACanvas.Handle, OldBkMode);
// end;
else // Should not happen, draw as covered
begin
ACanvas.Brush.Color := clBtnFace;
ACanvas.FillRect(DestRect);
DrawBevel(ACanvas, DestRect, BEVEL_WIDTH, True); // Raised
end;
end; // case
end;
procedure TMainForm.DrawSmileyFace(ACanvas: TCanvas; DestRect: TRect; State: TSmileyState);
var
CenterX, CenterY, Radius, EyeRadius, EyeOffsetX, EyeY: Integer;
MouthRect: TRect;
Angle1, Angle2: Integer; // For Arc
begin
CenterX := DestRect.Left + DestRect.Width div 2;
CenterY := DestRect.Top + DestRect.Height div 2;
Radius := Min(DestRect.Width, DestRect.Height) * 9 div 20; // 90% of half size
EyeRadius := Max(1, Radius div 5);
EyeOffsetX := Radius * 2 div 5;
EyeY := CenterY - Radius * 1 div 5;
// Background / Bevel
ACanvas.Brush.Color := clBtnFace;
ACanvas.FillRect(DestRect);
DrawBevel(ACanvas, DestRect, BEVEL_WIDTH, True); // Raised button look
// Head
ACanvas.Brush.Color := clYellow;
ACanvas.Pen.Color := clBlack;
ACanvas.Pen.Width := 1;
ACanvas.Ellipse(CenterX - Radius, CenterY - Radius, CenterX + Radius, CenterY + Radius);
// Eyes
ACanvas.Brush.Color := clBlack;
if State = ssLost then // X Eyes
begin
ACanvas.Pen.Width := Max(1, EyeRadius);
// Left X
ACanvas.MoveTo(CenterX - EyeOffsetX - EyeRadius, EyeY - EyeRadius);
ACanvas.LineTo(CenterX - EyeOffsetX + EyeRadius, EyeY + EyeRadius);
ACanvas.MoveTo(CenterX - EyeOffsetX + EyeRadius, EyeY - EyeRadius);
ACanvas.LineTo(CenterX - EyeOffsetX - EyeRadius, EyeY + EyeRadius);
// Right X
ACanvas.MoveTo(CenterX + EyeOffsetX - EyeRadius, EyeY - EyeRadius);
ACanvas.LineTo(CenterX + EyeOffsetX + EyeRadius, EyeY + EyeRadius);
ACanvas.MoveTo(CenterX + EyeOffsetX + EyeRadius, EyeY - EyeRadius);
ACanvas.LineTo(CenterX + EyeOffsetX - EyeRadius, EyeY + EyeRadius);
ACanvas.Pen.Width := 1;
end
else // Normal round eyes (or sunglasses for Win)
begin
if State = ssWin then // Sunglasses
begin
ACanvas.Brush.Color := clBlack;
ACanvas.Pen.Color := clBlack;
ACanvas.Rectangle(CenterX - EyeOffsetX - EyeRadius*2, EyeY - EyeRadius,
CenterX - EyeOffsetX + EyeRadius*2, EyeY + EyeRadius);
ACanvas.Rectangle(CenterX + EyeOffsetX - EyeRadius*2, EyeY - EyeRadius,
CenterX + EyeOffsetX + EyeRadius*2, EyeY + EyeRadius);
// Bridge
ACanvas.MoveTo(CenterX - EyeOffsetX + EyeRadius*2, EyeY);
ACanvas.LineTo(CenterX + EyeOffsetX - EyeRadius*2, EyeY);
end
else // Round eyes
begin
ACanvas.Ellipse(CenterX - EyeOffsetX - EyeRadius, EyeY - EyeRadius, CenterX - EyeOffsetX + EyeRadius, EyeY + EyeRadius); // Left Eye
ACanvas.Ellipse(CenterX + EyeOffsetX - EyeRadius, EyeY - EyeRadius, CenterX + EyeOffsetX + EyeRadius, EyeY + EyeRadius); // Right Eye
end;
end;
// Mouth
ACanvas.Pen.Color := clBlack;
ACanvas.Pen.Width := Max(1, EyeRadius);
MouthRect := Rect(CenterX - Radius * 3 div 5, CenterY + Radius * 1 div 5,
CenterX + Radius * 3 div 5, CenterY + Radius * 4 div 5);
case State of
ssNormal, ssWin: // Smile
begin
Angle1 := 0; Angle2 := 180; // Bottom half of ellipse for smile
ACanvas.Arc(MouthRect.Left, MouthRect.Top, MouthRect.Right, MouthRect.Bottom,
MouthRect.Right, MouthRect.Top + MouthRect.Height div 2, // Start point on right
MouthRect.Left, MouthRect.Top + MouthRect.Height div 2); // End point on left
end;
ssOoh: // O shape
begin
ACanvas.Brush.Style := bsClear; // Hollow mouth
ACanvas.Ellipse(MouthRect.Left + EyeRadius, MouthRect.Top, MouthRect.Right - EyeRadius, MouthRect.Bottom);
ACanvas.Brush.Style := bsSolid;
end;
ssLost: // Frown
begin
Angle1 := 180; Angle2 := 360; // Top half of ellipse for frown
ACanvas.Arc(MouthRect.Left, MouthRect.Top - Radius div 3, MouthRect.Right, MouthRect.Bottom - Radius div 3, // Shift up a bit
MouthRect.Left, MouthRect.Top + MouthRect.Height div 2 - Radius div 3, // Start point on left
MouthRect.Right, MouthRect.Top + MouthRect.Height div 2 - Radius div 3); // End point on right
end;
end;
ACanvas.Pen.Width := 1; // Reset pen
end;
// --- Intro/Win/Loss Screen Drawing ---
procedure TMainForm.DrawIntroScreen(ACanvas: TCanvas; Width, Height: Integer; Step: Integer);
var
R: TRect;
MidX, MidY: Integer;
Title: string;
SubText: string;
Alpha: Byte; // For fading effect
begin
MidX := Width div 2;
MidY := Height div 2;
Title := 'MINESWEEPER DELPHI';
SubText := 'Click to Start';
// Background (Could animate color change based on Step)
ACanvas.Brush.Color := TColor($00A06000); // Darkish Blue/Green
ACanvas.FillRect(Rect(0, 0, Width, Height));
// Simple Animation: Text Fade In (Example)
Alpha := Min(255, Step * 10); // Fade over ~25 steps
// Title Text
ACanvas.Font.Name := 'Impact'; // Or another bold font
ACanvas.Font.Size := Max(18, Height div 10);
ACanvas.Font.Color := TColor( $FFFFFF or (Alpha shl 24) ); // White with alpha
ACanvas.Font.Style := [fsBold];
R := Rect(0, 0, Width, MidY);
ACanvas.Brush.Style := bsClear; // Transparent background for text
DrawText(ACanvas.Handle, Title, Length(Title), R, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
// Sub Text
ACanvas.Font.Name := 'Arial';
ACanvas.Font.Size := Max(10, Height div 25);
ACanvas.Font.Color := TColor( $D0D0D0 or (Alpha shl 24) ); // Light Gray with alpha
ACanvas.Font.Style := [];
R := Rect(0, MidY, Width, Height - 20);
DrawText(ACanvas.Handle, SubText, Length(SubText), R, DT_CENTER or DT_TOP or DT_SINGLELINE);
ACanvas.Brush.Style := bsSolid; // Reset brush
end;
procedure TMainForm.DrawWinScreen(ACanvas: TCanvas; Width, Height: Integer; Step: Integer);
var
R: TRect;
MidX, MidY: Integer;
Msg: string;
PulseColor: TColor;
ScoreText: string;
ElapsedTime: Int64;
begin
MidX := Width div 2;
MidY := Height div 2;
Msg := 'YOU WIN!';
// Pulsating Background (Example Animation)
// Simple sine wave for color pulse (adjust frequency/amplitude)
PulseColor := TColor( Round(100 + 80 * Sin(Step * 0.2)) shl 8 ); // Pulsing Green
ACanvas.Brush.Color := PulseColor;
ACanvas.FillRect(Rect(0, 0, Width, Height));
// Win Text
ACanvas.Font.Name := 'Impact';
ACanvas.Font.Size := Max(24, Height div 8);
ACanvas.Font.Color := clWhite;
ACanvas.Font.Style := [fsBold];
R := Rect(0, 0, Width, MidY + 20);
ACanvas.Brush.Style := bsClear;
DrawText(ACanvas.Handle, Msg, Length(Msg), R, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
// Show Score
if FEndTime > FStartTime then // Ensure times are valid
ElapsedTime := SecondsBetween(FEndTime, FStartTime)
else
ElapsedTime := 0;
ScoreText := Format('Time: %d seconds', [ElapsedTime]);
ACanvas.Font.Name := 'Arial';
ACanvas.Font.Size := Max(12, Height div 20);
ACanvas.Font.Color := clYellow;
ACanvas.Font.Style := [];
R := Rect(0, MidY + 20, Width, Height - 20);
DrawText(ACanvas.Handle, ScoreText, Length(ScoreText), R, DT_CENTER or DT_TOP or DT_SINGLELINE);
ACanvas.Brush.Style := bsSolid;
end;
procedure TMainForm.DrawLossScreen(ACanvas: TCanvas; Width, Height: Integer; Step: Integer);
var
R: TRect;
MidX, MidY: Integer;
Msg: string;
FlashColor: TColor;
begin
MidX := Width div 2;
MidY := Height div 2;
Msg := 'GAME OVER!';
// Flashing Background (Example Animation)
if Odd(Step div 5) then // Flash every 5 steps
FlashColor := clRed
else
FlashColor := TColor($0000A0); // Dark Red
ACanvas.Brush.Color := FlashColor;
ACanvas.FillRect(Rect(0, 0, Width, Height));
// Text
ACanvas.Font.Name := 'Impact';
ACanvas.Font.Size := Max(24, Height div 8);
ACanvas.Font.Color := clWhite;
ACanvas.Font.Style := [fsBold];
R := Rect(0, 0, Width, Height);
ACanvas.Brush.Style := bsClear;
DrawText(ACanvas.Handle, Msg, Length(Msg), R, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
ACanvas.Brush.Style := bsSolid;
end;
// --- Helper to Ensure TImage Bitmaps Exist ---
procedure TMainForm.EnsureImageBitmaps;
begin
// Ensure imgBoard bitmap exists and has correct size
if not Assigned(imgBoard.Picture.Bitmap) then
imgBoard.Picture.Bitmap := TBitmap.Create;
if (imgBoard.Picture.Bitmap.Width <> imgBoard.Width) or (imgBoard.Picture.Bitmap.Height <> imgBoard.Height) then
imgBoard.Picture.Bitmap.SetSize(imgBoard.Width, imgBoard.Height);
// Ensure imgSmiley bitmap exists and has correct size
if not Assigned(imgSmiley.Picture.Bitmap) then
imgSmiley.Picture.Bitmap := TBitmap.Create;
if (imgSmiley.Picture.Bitmap.Width <> imgSmiley.Width) or (imgSmiley.Picture.Bitmap.Height <> imgSmiley.Height) then
imgSmiley.Picture.Bitmap.SetSize(imgSmiley.Width, imgSmiley.Height);
// Ensure imgOverlay bitmap exists and has correct size
if not Assigned(imgOverlay.Picture.Bitmap) then
imgOverlay.Picture.Bitmap := TBitmap.Create;
if (imgOverlay.Picture.Bitmap.Width <> imgOverlay.Width) or (imgOverlay.Picture.Bitmap.Height <> imgOverlay.Height) then
imgOverlay.Picture.Bitmap.SetSize(imgOverlay.Width, imgOverlay.Height);
end;
// --- Game Setup ---
procedure TMainForm.SetupGame(ADifficulty: TDifficulty);
const
TOP_MARGIN = 5; // Space above smiley
SMILEY_BOARD_GAP = 10; // Space between smiley and board
BOTTOM_MARGIN = 10; // Space below board
begin
GameTimer.Enabled := False;
AnimationTimer.Enabled := False;
FCurrentDifficulty := ADifficulty;
SetDifficultyCheckmark(ADifficulty);
case ADifficulty of
dEasy: begin FGridWidth := 9; FGridHeight := 9; FMineCount := 10; end;
dMedium: begin FGridWidth := 16; FGridHeight := 16; FMineCount := 40; end;
dHard: begin FGridWidth := 30; FGridHeight := 16; FMineCount := 99; end;
else
begin FGridWidth := 9; FGridHeight := 9; FMineCount := 10; end;
end;
// Adjust image sizes based on CELL SIZE
imgBoard.Width := FGridWidth * FCellSize;
imgBoard.Height := FGridHeight * FCellSize;
// Use fixed smiley size set in DFM or FormCreate
// imgSmiley.Width := FCellSize * 2; // Or keep fixed DFM size
// imgSmiley.Height := FCellSize * 2;
// Position smiley near top of client area
imgSmiley.Top := TOP_MARGIN;
// Position board below smiley
imgBoard.Top := imgSmiley.Top + imgSmiley.Height + SMILEY_BOARD_GAP;
// Calculate required client dimensions
Self.ClientWidth := Max(imgBoard.Width + 40, imgSmiley.Left + imgSmiley.Width + 20); // Ensure wide enough for board+padding or smiley
Self.ClientHeight := imgBoard.Top + imgBoard.Height + StatusBar1.Height + BOTTOM_MARGIN; // Height based on elements + status bar + margins
EnsureImageBitmaps; // Create/resize backing bitmaps
CenterBoard; // Center controls horizontally and verify vertical positions
InitializeGrid;
FGameState := gsReady;
FFirstClick := True;
FRevealedCount := 0;
FFlagsPlaced := 0;
FGameLostMineX := -1;
FGameLostMineY := -1;
FCurrentSmileyState := ssNormal; // Reset smiley state
imgOverlay.Visible := False;
UpdateSmiley;
UpdateStatusBar;
DrawBoard; // Initial draw
end;
procedure TMainForm.CenterBoard;
const
TOP_MARGIN = 5; // Consistent margin (must match SetupGame or be passed)
SMILEY_BOARD_GAP = 10; // Consistent gap
begin
// Center Smiley horizontally, position near top
imgSmiley.Left := Max(5, (Self.ClientWidth - imgSmiley.Width) div 2);
imgSmiley.Top := TOP_MARGIN; // Use constant margin from top
// Center imgBoard horizontally, position below smiley
imgBoard.Left := Max(5, (Self.ClientWidth - imgBoard.Width) div 2);
imgBoard.Top := imgSmiley.Top + imgSmiley.Height + SMILEY_BOARD_GAP; // Position below updated smiley position
// Make overlay cover the client area
imgOverlay.BoundsRect := Self.ClientRect;
end;
// InitializeGrid, PlaceMines, CalculateAdjacentMines remain the same internally
procedure TMainForm.InitializeGrid;
var x,y: Integer;
begin
SetLength(FGrid, FGridWidth, FGridHeight);
for x := 0 to FGridWidth - 1 do
begin
for y := 0 to FGridHeight - 1 do
begin
FGrid[x, y].IsMine := False;
FGrid[x, y].IsRevealed := False;
FGrid[x, y].IsFlagged := False;
FGrid[x, y].AdjacentMines := 0;
end;
end;
end;
procedure TMainForm.PlaceMines(ExcludeX, ExcludeY: Integer);
var MinesToPlace, x,y : Integer;
begin
MinesToPlace := FMineCount;
while MinesToPlace > 0 do
begin
x := Random(FGridWidth);
y := Random(FGridHeight);
if not FGrid[x, y].IsMine and not ((x = ExcludeX) and (y = ExcludeY)) then
begin
FGrid[x, y].IsMine := True;
Dec(MinesToPlace);
end;
end;
end;
procedure TMainForm.CalculateAdjacentMines;
var x,y,i,j,nx,ny, count: Integer;
begin
for x := 0 to FGridWidth - 1 do
begin
for y := 0 to FGridHeight - 1 do
begin
if not FGrid[x, y].IsMine then
begin
count := 0;
for i := -1 to 1 do
begin
for j := -1 to 1 do
begin
if (i = 0) and (j = 0) then continue;
nx := x + i;
ny := y + j;
if IsValidCoord(nx, ny) and FGrid[nx, ny].IsMine then
begin
Inc(count);
end;
end;
end;
FGrid[x, y].AdjacentMines := count;
end;
end;
end;
end;
// --- Drawing ---
procedure TMainForm.DrawBoard;
var
x, y: Integer;
begin
EnsureImageBitmaps; // Make sure bitmap exists and is sized correctly
if imgBoard.Picture.Bitmap.Empty then Exit;
// Clear background (optional, primitives usually cover)
// imgBoard.Picture.Bitmap.Canvas.Brush.Color := clWhite;
// imgBoard.Picture.Bitmap.Canvas.FillRect(Rect(0, 0, imgBoard.Width, imgBoard.Height));
for x := 0 to FGridWidth - 1 do
begin
for y := 0 to FGridHeight - 1 do
begin
DrawCell(imgBoard.Picture.Bitmap.Canvas, x, y);
end;
end;
imgBoard.Invalidate; // Refresh the image control
end;
procedure TMainForm.DrawCell(ACanvas: TCanvas; GridX, GridY: Integer);
var
DestRect: TRect;
TileIndex: Integer;
begin
DestRect := Rect(GridX * FCellSize, GridY * FCellSize,
(GridX + 1) * FCellSize, (GridY + 1) * FCellSize);
TileIndex := GetTileIndex(GridX, GridY);
DrawTilePrimitive(ACanvas, DestRect, TileIndex);
end;
// GetTileIndex logic remains the same
function TMainForm.GetTileIndex(GridX, GridY: Integer): Integer;
begin
Result := TILE_COVERED; // Default
if (FGameState = gsLost) or (FGameState = gsWon) then
begin // Game Over states - reveal everything
if FGrid[GridX, GridY].IsMine then
begin
if FGrid[GridX, GridY].IsFlagged then
Result := TILE_FLAG // Show flag even if mine (consistent view)
else if (GridX = FGameLostMineX) and (GridY = FGameLostMineY) then
Result := TILE_MINE_HIT // The one that exploded
else
Result := TILE_MINE; // Unflagged mine
end
else // Not a mine
begin
if FGrid[GridX, GridY].IsFlagged then
Result := TILE_MINE_WRONG // Incorrectly flagged
else if FGrid[GridX, GridY].IsRevealed then // Check if it was revealed during play
Result := TILE_0 + FGrid[GridX, GridY].AdjacentMines
else // If not revealed (only possible in win scenario?), show base
Result := TILE_0;
end;
end
else // Game is playing or ready
begin
if FGrid[GridX, GridY].IsFlagged then
Result := TILE_FLAG
else if FGrid[GridX, GridY].IsRevealed then
begin
if FGrid[GridX, GridY].IsMine then // Should not happen
Result := TILE_MINE_HIT
else
Result := TILE_0 + FGrid[GridX, GridY].AdjacentMines;
end
else // Still covered
Result := TILE_COVERED;
// Optional: Add Question Mark state TILE_QUESTION
end;
end;
// --- Game Logic (Input, Reveal, Win/Loss Check) ---
// imgBoardMouseDown, RevealCell, FloodFill, CheckWinCondition, GameOver logic largely unchanged,
// but drawing calls inside them might update differently.
procedure TMainForm.imgBoardMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var GridX, GridY: Integer;
begin
if not (FGameState in [gsReady, gsPlaying]) then Exit;
GridX := X div FCellSize;
GridY := Y div FCellSize;
if not IsValidCoord(GridX, GridY) then Exit;
if Button = mbLeft then
begin
UpdateSmiley(True); // Show Ooh face
// Need to PumpMessages to allow smiley redraw before potential long operation (like flood fill)
Application.ProcessMessages; // Use with caution, can cause re-entrancy issues if not careful
if FGrid[GridX, GridY].IsRevealed or FGrid[GridX, GridY].IsFlagged then
begin
UpdateSmiley(False); // Back to normal/win/lost state
Exit;
end;
if FFirstClick then
begin
FFirstClick := False;
PlaceMines(GridX, GridY);
CalculateAdjacentMines;
FStartTime := Now;
GameTimer.Enabled := True;
FGameState := gsPlaying;
end;
if FGrid[GridX, GridY].IsMine then
begin
FGameLostMineX := GridX;
FGameLostMineY := GridY;
GameOver(False);
end
else
begin
RevealCell(GridX, GridY); // This will call DrawBoard indirectly
// DrawBoard; // Redraw AFTER revealing
if FGameState = gsPlaying then
CheckWinCondition;
end;
// Update smiley based on game state *after* action potentially changes it
UpdateSmiley(False);
end
else if Button = mbRight then // Right Click for Flag
begin