Thema: Delphi Minesweeper

Einzelnen Beitrag anzeigen

saii

Registriert seit: 20. Feb 2015
22 Beiträge
 
#32

AW: Minesweeper

  Alt 24. Mär 2015, 20:16
Sorry Ich versteh nicht was du meinst
Und was das mir bringt erst recht nicht.
Falls das hilft ist hier mal mein kompletter Code:
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure CannonFire(var A,B,m:integer; Sender: TObject);
    procedure PanelMatrixMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    procedure CreatePanelMatrix(x1, y1: Integer);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}



var
  PanelA: array[0..14, 0..14] of TPanel;
  IntA: array[0..14, 0..14] of integer;
  Panel: TPanel;
  A,B,m:integer;


procedure TForm1.CreatePanelMatrix(x1, y1: Integer);
var
  x,y:integer;
begin
  for x := 0 to 14 do
    for y := 0 to 14 do
    begin
      Panel := TPanel.Create(Self);
      Panel.Parent := Self;
      Panel.Name := 'P_' + IntToStr(x) + '_' + IntToStr(y);
      Panel.Width := 30;
      Panel.Height := 30;
      Panel.Caption := '';
      Panel.Left := x1 + (x * 30);
      Panel.Top := y1 + (y * 30);
      Panel.OnMouseDown := PanelMatrixMouseDown;
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var k,Ai,Bi: integer;
begin
  randomize;
  for k:=0 to 25 do
  begin
        A:=random(15);
        B:=random(15);
        IntA[A,B]:=9;
        StringGrid1.Cells[A,B]:='X';
  end;

  for Ai:=0 to 14 do
  begin
        for Bi:=0 to 14 do
        begin
                if IntA[Ai,Bi]<>9 then
                begin
                        m:=0;

                        if (Ai+1>=0) and (Ai+1<=14) and (Bi>=0) and (Bi<=14) then if IntA[Ai+1,Bi]=9 then m:=m+1;
                        if (Ai+1>=0) and (Ai+1<=14) and (Bi+1>=0) and (Bi+1<=14) then if IntA[Ai+1,Bi+1]=9 then m:=m+1;
                        if (Ai>=0) and (Ai<=14) and (Bi+1>=0) and (Bi+1<=14) then   if IntA[Ai,Bi+1]=9 then m:=m+1;
                        if (Ai-1>=0) and (Ai-1<=14) and (Bi+1>=0) and (Bi+1<=14) then if IntA[Ai-1,Bi+1]=9 then m:=m+1;
                        if (Ai-1>=0) and (Ai-1<=14) and (Bi>=0) and (Bi<=14) then   if IntA[Ai-1,Bi]=9 then m:=m+1;
                        if (Ai-1>=0) and (Ai-1<=14) and (Bi-1>=0) and (Bi-1<=14) then if IntA[Ai-1,Bi-1]=9 then m:=m+1;
                        if (Ai>=0) and (Ai<=14) and (Bi-1>=0) and (Bi-1<=14) then   if IntA[Ai,Bi-1]=9 then m:=m+1;
                        if (Ai+1>=0) and (Ai+1<=14) and (Bi-1>=0) and (Bi-1<=14) then if IntA[Ai+1,Bi-1]=9 then m:=m+1;

                        IntA[Ai,Bi]:=m;
                        StringGrid1.Cells[Ai,Bi]:=IntToStr(m);
                        if m=0 then StringGrid1.Cells[Ai,Bi]:='_';
                end;
        end;
  end;
  CreatePanelMatrix(20,20);

end;

procedure TForm1.CannonFire(var A,B,m:integer; Sender: TObject);
var h,j:integer;
begin
        Panel:=TPanel(Sender);

        if IntA[A,B]<>9 then
        begin
                m:=IntA[A,B];
                if IntA[A,B]=0 then
                begin
                        for h:= 0 to 14 do
                        begin
                                for j:=0 to 14 do
                                begin
                                        if IntA[h,j]=0 then PanelA[h,j].Caption:='_';

                                        if (h+1>=0) and (h+1<=14) and (j>=0) and (j<=14) then PanelA[h+1,j].Caption:='_';
                                        if (h+1>=0) and (h+1<=14) and (j+1>=0) and (j+1<=14) then PanelA[h+1,j+1+1].Caption:='_';
                                        if (h>=0) and (h<=14) and (j+1>=0) and (j+1<=14) then PanelA[h,j+1].Caption:='_';
                                        if (h-1>=0) and (h-1<=14) and (j+1>=0) and (j+1<=14) then PanelA[h-1,j+1].Caption:='_';
                                        if (h-1>=0) and (h-1<=14) and (j>=0) and (j<=14) then PanelA[h-1,j].Caption:='_';
                                        if (h-1>=0) and (h-1<=14) and (j-1>=0) and (j-1<=14) then PanelA[h-1,j-1].Caption:='_';
                                        if (h>=0) and (h<=14) and (j-1>=0) and (j-1<=14) then PanelA[h,j-1].Caption:='_';
                                        if (h+1>=0) and (h+1<=14) and (j-1>=0) and (j-1<=14) then PanelA[h+1,j-1].Caption:='_';
                                end;
                        end;
                end;
        end;
        
        if IntA[A,B]=9 then
        begin
                ShowMessage('Du hast Verloren!');
                close;
        end;
end;

procedure TForm1.PanelMatrixMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var s:string;
        c:char;
begin
  if not (Sender is TPanel) then Exit;

  Panel:=TPanel(Sender);
  A:=StrToInt(Panel.Name[3]); // <-- Hier ist
  B:=StrToInt(Panel.Name[5]); // <-- das Problem.


  if Button=mbRight then
  begin
        s := Panel.Caption;
        if s <> 'then c := s[1]
                else c := ' ';
        case c of
                ' ': c := 'F';
                'F': c := '?';
                else c := ' ';
        end;
        Panel.Caption := c;
  end;

  if Button=mbLeft then
  begin
        CannonFire(A,B,m,Sender);
        Panel.Caption:=IntToStr(m);
        if m=0 then Panel.Caption:='_';
        Panel.Enabled:=false;
  end;
end;

end.
  Mit Zitat antworten Zitat