Hallo, liebe DPler,
Ich programmiere für die Schule Minesweeper in Delphi6 und bräuchte mal eure Hilfe.
Ich nutze ein zweidimensionales Array, in dem ich die Minen zufällig verteile
und in meiner Form1 sind 225 Panels quadratisch angeordnet.
Bisher kann man mit Rechtsklick Panels markieren ("F" als Flagge und "?") und
mit Linksklick wird die Zahl der umliegenden Minen kontrolliert und aufgeschrieben.
Falls man auf eine Mine linksklickt, ist das Spiel verloren.
Nun möchte ich, und dabei brauche ich eure Hilfe, dass wenn keine mit Minen besetzten Felder anliegen, dass passiert, was im Original auch passiert. Ihr wisst hoffentlich, was ich meine
Mein Problem dabei ist, dass ich kein Plan hab, wie ich das umsetzen kann.
Anbei mein QuellCode und ein Screenshot meiner Form1.
Hinweis: Das StringGrid ist temporär. Ich nutze es um zu wissen ob sich mein Programm verzählt.
Außerdem sind bisher nur 3 Panels eingebunden ([0,0], [0,1], [1,1])
Ich wäre euch echt unglaublich dankbar, wenn ihr mir da weiterhelfen würdet
Form1
Delphi-Quellcode:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, ExtCtrls, StdCtrls;
type
TForm1 =
class(TForm)
Panel1: TPanel;
P_0_0: TPanel;
P_0_1: TPanel;
P_0_2: TPanel;
P_0_3: TPanel;
P_0_4: TPanel;
P_0_5: TPanel;
P_0_6: TPanel;
P_0_7: TPanel;
P_0_8: TPanel;
P_0_9: TPanel;
P_0_10: TPanel;
P_0_11: TPanel;
P_0_12: TPanel;
P_0_13: TPanel;
P_0_14: TPanel;
P_1_0: TPanel;
P_1_1: TPanel;
P_1_2: TPanel;
P_1_3: TPanel;
P_1_4: TPanel;
P_1_5: TPanel;
P_1_6: TPanel;
P_1_7: TPanel;
P_1_8: TPanel;
P_1_9: TPanel;
P_1_10: TPanel;
P_1_11: TPanel;
P_1_12: TPanel;
P_1_13: TPanel;
P_1_14: TPanel;
P_2_0: TPanel;
P_2_1: TPanel;
P_2_2: TPanel;
P_2_3: TPanel;
P_2_4: TPanel;
P_2_5: TPanel;
P_2_6: TPanel;
P_2_7: TPanel;
P_2_8: TPanel;
P_2_9: TPanel;
P_2_13: TPanel;
P_2_14: TPanel;
P_2_10: TPanel;
P_2_11: TPanel;
P_3_0: TPanel;
P_3_1: TPanel;
P_3_2: TPanel;
P_3_3: TPanel;
P_2_12: TPanel;
P_3_4: TPanel;
P_3_5: TPanel;
P_3_6: TPanel;
P_3_7: TPanel;
P_3_8: TPanel;
P_3_9: TPanel;
P_3_10: TPanel;
P_3_11: TPanel;
P_3_12: TPanel;
P_3_13: TPanel;
P_3_14: TPanel;
P_4_0: TPanel;
P_4_1: TPanel;
P_4_2: TPanel;
P_4_3: TPanel;
P_4_4: TPanel;
P_4_5: TPanel;
P_4_6: TPanel;
P_4_7: TPanel;
P_4_8: TPanel;
P_4_9: TPanel;
P_4_10: TPanel;
P_4_11: TPanel;
P_4_12: TPanel;
P_4_13: TPanel;
P_4_14: TPanel;
P_5_0: TPanel;
P_5_1: TPanel;
P_5_2: TPanel;
P_5_3: TPanel;
P_5_4: TPanel;
P_5_5: TPanel;
P_5_6: TPanel;
P_5_7: TPanel;
P_5_8: TPanel;
P_5_9: TPanel;
P_5_10: TPanel;
P_5_11: TPanel;
P_5_12: TPanel;
P_5_13: TPanel;
P_5_14: TPanel;
P_6_0: TPanel;
P_6_1: TPanel;
P_6_2: TPanel;
P_6_3: TPanel;
P_6_4: TPanel;
P_6_5: TPanel;
P_6_6: TPanel;
P_6_7: TPanel;
P_6_8: TPanel;
P_6_9: TPanel;
P_6_10: TPanel;
P_6_11: TPanel;
P_7_0: TPanel;
P_6_12: TPanel;
P_6_13: TPanel;
P_6_14: TPanel;
P_7_1: TPanel;
P_7_2: TPanel;
P_7_3: TPanel;
P_7_4: TPanel;
P_7_5: TPanel;
P_7_6: TPanel;
P_7_7: TPanel;
P_7_8: TPanel;
P_7_9: TPanel;
P_7_10: TPanel;
P_7_11: TPanel;
P_7_12: TPanel;
P_7_13: TPanel;
P_7_14: TPanel;
P_8_0: TPanel;
P_8_1: TPanel;
P_8_2: TPanel;
P_8_3: TPanel;
P_8_4: TPanel;
P_8_5: TPanel;
P_8_6: TPanel;
P_8_7: TPanel;
P_8_8: TPanel;
P_8_9: TPanel;
P_8_10: TPanel;
P_8_11: TPanel;
P_8_12: TPanel;
P_8_13: TPanel;
P_8_14: TPanel;
P_9_0: TPanel;
P_9_1: TPanel;
P_9_2: TPanel;
P_9_3: TPanel;
P_9_4: TPanel;
P_9_5: TPanel;
P_9_6: TPanel;
P_9_7: TPanel;
P_9_8: TPanel;
P_9_9: TPanel;
P_9_10: TPanel;
P_9_11: TPanel;
P_9_12: TPanel;
P_9_13: TPanel;
P_9_14: TPanel;
P_10_0: TPanel;
P_10_1: TPanel;
P_10_2: TPanel;
P_10_3: TPanel;
P_10_4: TPanel;
P_10_5: TPanel;
P_10_6: TPanel;
P_10_7: TPanel;
P_10_8: TPanel;
P_10_9: TPanel;
P_10_10: TPanel;
P_10_11: TPanel;
P_10_12: TPanel;
P_10_13: TPanel;
P_10_14: TPanel;
P_11_0: TPanel;
P_11_1: TPanel;
P_11_2: TPanel;
P_11_3: TPanel;
P_11_4: TPanel;
P_11_5: TPanel;
P_11_6: TPanel;
P_11_7: TPanel;
P_11_8: TPanel;
P_11_9: TPanel;
P_11_10: TPanel;
P_11_11: TPanel;
P_11_12: TPanel;
P_11_13: TPanel;
P_11_14: TPanel;
P_12_0: TPanel;
P_12_1: TPanel;
P_12_2: TPanel;
P_12_3: TPanel;
P_12_4: TPanel;
P_12_5: TPanel;
P_12_6: TPanel;
P_12_7: TPanel;
P_12_8: TPanel;
P_12_9: TPanel;
P_12_10: TPanel;
P_12_11: TPanel;
P_12_12: TPanel;
P_12_13: TPanel;
P_12_14: TPanel;
P_13_0: TPanel;
P_13_1: TPanel;
P_14_0: TPanel;
P_14_1: TPanel;
P_13_2: TPanel;
P_14_2: TPanel;
P_13_3: TPanel;
P_14_3: TPanel;
P_13_4: TPanel;
P_14_4: TPanel;
P_13_5: TPanel;
P_14_5: TPanel;
P_13_6: TPanel;
P_14_6: TPanel;
P_13_7: TPanel;
P_14_7: TPanel;
P_13_8: TPanel;
P_14_8: TPanel;
P_13_9: TPanel;
P_14_9: TPanel;
P_13_10: TPanel;
P_14_10: TPanel;
P_13_11: TPanel;
P_14_11: TPanel;
P_13_12: TPanel;
P_14_12: TPanel;
P_13_13: TPanel;
P_14_13: TPanel;
P_13_14: TPanel;
P_14_14: TPanel;
StringGrid1: TStringGrid;
procedure CannonFire(
var A,B,m:integer);
procedure P_0_0MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure P_0_1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure P_1_1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
mf:
array[0..14, 0..14]
of boolean;
//mf = Minenfeld
A,B,m:integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
//Verteilung der Minen
var i: integer;
begin
randomize;
for i:=0
to 50
do
begin
A:=random(15);
B:=random(15);
mf[A,B]:=true;
StringGrid1.Cells[A,B]:='
M';
end;
end;
procedure TForm1.CannonFire(
var A,B,m:integer);
//Procedure für Linksklick
var z:integer;
begin
if mf[A,B]=false
then //Falls keine Mine...
begin
m:=0;
if (A+1>=0)
and (A+1<=14)
and (B>=0)
and (B<=14)
then if mf[A+1,B]=true
then m:=m+1;
if (A+1>=0)
and (A+1<=14)
and (B+1>=0)
and (B+1<=14)
then if mf[A+1,B+1]=true
then m:=m+1;
if (A>=0)
and (A<=14)
and (B+1>=0)
and (B+1<=14)
then if mf[A,B+1]=true
then m:=m+1;
if (A-1>=0)
and (A-1<=14)
and (B+1>=0)
and (B+1<=14)
then if mf[A-1,B+1]=true
then m:=m+1;
if (A-1>=0)
and (A-1<=14)
and (B>=0)
and (B<=14)
then if mf[A-1,B]=true
then m:=m+1;
if (A-1>=0)
and (A-1<=14)
and (B-1>=0)
and (B-1<=14)
then if mf[A-1,B-1]=true
then m:=m+1;
if (A>=0)
and (A<=14)
and (B-1>=0)
and (B-1<=14)
then if mf[A,B-1]=true
then m:=m+1;
if (A+1>=0)
and (A+1<=14)
and (B-1>=0)
and (B-1<=14)
then if mf[A+1,B-1]=true
then m:=m+1;
if m=0
then
begin //Hier soll der beschriebene Fall programmiert werden
repeat
until z=1;
end;
end;
if mf[A,B]=true
then //Falls Mine getroffen wurde..
begin
ShowMessage('
Du hast Verloren!');
close;
end;
end;
//---------------------------------------------------------------Folgend: Einbindung Panels
procedure TForm1.P_0_0MouseDown(Sender: TObject; Button: TMouseButton;
//Panel_0_0
Shift: TShiftState; X, Y: Integer);
var s:
string;
c:char;
begin
s := P_0_0.Caption;
//Rechtsklick
if s <> '
'
then c := s[1]
else
c := '
';
if (Button = mbRight)
then
case c
of
'
': c := '
F';
'
F': c := '
?';
else c := '
';
end;
P_0_0.Caption := c;
if (Button = mbLeft)
then //Linksklick
begin
A:=0;
B:=0;
CannonFire(A,B,m);
P_0_0.Caption:=IntToStr(m);
P_0_0.Enabled:=false;
end;
end;
procedure TForm1.P_0_1MouseDown(Sender: TObject; Button: TMouseButton;
//Panel_0_1
Shift: TShiftState; X, Y: Integer);
var s:
string;
c:char;
begin
s := P_0_1.Caption;
if s <> '
'
then c := s[1]
else
c := '
';
if (Button = mbRight)
then
case c
of
'
': c := '
F';
'
F': c := '
?';
else c := '
';
end;
P_0_1.Caption := c;
if (Button = mbLeft)
then
begin
A:=1;
B:=0;
CannonFire(A,B,m);
P_0_1.Caption:=IntToStr(m);
P_0_1.Enabled:=false;
end;
end;
procedure TForm1.P_1_1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var s:
string;
c:char;
begin
s := P_1_1.Caption;
if s <> '
'
then c := s[1]
else
c := '
';
if (Button = mbRight)
then
case c
of
'
': c := '
F';
'
F': c := '
?';
else c := '
';
end;
P_1_1.Caption := c;
if (Button = mbLeft)
then
begin
A:=1;
B:=1;
CannonFire(A,B,m);
P_1_1.Caption:=IntToStr(m);
P_1_1.Enabled:=false;
end;
end;
end.