AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Minesweeper

Ein Thema von saii · begonnen am 21. Mär 2015 · letzter Beitrag vom 29. Mär 2015
Antwort Antwort
Seite 1 von 2  1 2      
saii

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

AW: Minesweeper

  Alt 24. Mär 2015, 17:40
Danke BadenPower und Mavarik.
Mal sehen wie weit ich komme
  Mit Zitat antworten Zitat
saii

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

AW: Minesweeper

  Alt 24. Mär 2015, 18:11
Ich wollte mit
A:=StrToInt(Panel.Name[3]); (falls das funktioniert hätte) A aus dem Namen des Panels bestimmen,
aber das Problem ist, dass der Name des Panels an der Stelle (zB P_4_7) auch zweistellig sein kann. (P_10_13)

Wenn ihr wisst was ich meine.. wie löse ich das Problem? oder gibt es eh einen besseren Weg?
  Mit Zitat antworten Zitat
BadenPower

Registriert seit: 17. Jun 2009
616 Beiträge
 
#3

AW: Minesweeper

  Alt 24. Mär 2015, 18:20
Für was brauchst Du das A?

Auf das auslösende Panel kannst Du in der Event-Prozedure mit DiesesPanel := TPanel(Sender); zugreifen.
Programmieren ist die Kunst aus Nullen und Einsen etwas sinnvollen zu gestalten.
Der bessere Künstler ist allerdings der Anwender, denn dieser findet Fehler, welche sich der Programmierer nicht vorstellen konnte.
  Mit Zitat antworten Zitat
saii

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

AW: Minesweeper

  Alt 24. Mär 2015, 18:40
Ich nutze jetzt zusätzlich zum PanelArray ein weiteres IntegerArray weil ich die einzelnen Panels einen Wert von 0 bis 9 zuweise und Ich nicht weiß wie ich das mit einem PanelArray anstellen soll.

Ich brauche A, um aus dem TPanel die Koordinaten zu entnehmen.

DAs geht bestimmt einfacher, oder?
  Mit Zitat antworten Zitat
saii

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

AW: Minesweeper

  Alt 24. Mär 2015, 19:00
Himitsu, ich erstelle die Panels automatisch und benenne sie (warum weiß ich auch nicht, wie ich gerade bemerke)

Von daher weiß ich auch nicht, wie ich die zweistellig benennen soll.
Delphi-Quellcode:
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;
  Mit Zitat antworten Zitat
Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#6

AW: Minesweeper

  Alt 24. Mär 2015, 19:44
Du brauchst nur die Pointer abspeichern.
Delphi-Quellcode:
var
  Panels: array[0..14, 0..14] of TPanel;

function IndexOfPanel(Panel: TPanel): TPoint;
var
  X, Y: integer;
begin
  Result.X := -1;
  Result.Y := -1;
  for X := 0 to 14 do
    for Y := 0 to 14 do
      if Panels[X, Y] = Panel then
      begin
        Result.X := X;
        Result.Y := Y;
      end;
end;
Und in deiner CreatePanelMatrix Panels[I, J] := Panel;
  Mit Zitat antworten Zitat
saii

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

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
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.361 Beiträge
 
Delphi 12 Athens
 
#8

AW: Minesweeper

  Alt 25. Mär 2015, 01:05
Von daher weiß ich auch nicht, wie ich die zweistellig benennen soll.
Delphi-Quellcode:
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);
      ...
Jupp, es gibt nicht nur IntToStr (z.B. Format, wie bereits genannt) und warum ist bei dem Code das Panel eine globale Variable, wo dort doch nur "Schrott" (nur das letzte Panel) drin steht und es eigentlich ausschließtlich lokal benutzt wird?

Und wie bereits mehrfach erwähnt wurde, sollte man die Panels gleich speichern (siehe, wie ebenfalls grade nochmal von Jemandem gezeigt wurde)
Panels[x, y] := TPanel.Create(Self); und schon kann man den Mist mit dm Suchen weg lassen, da man die Instanzen bereits hat.
Ein Therapeut entspricht 1024 Gigapeut.
  Mit Zitat antworten Zitat
saii

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

AW: Minesweeper

  Alt 25. Mär 2015, 06:34
Vielen Dank, Leute.
Ich werde mich heute nachmittag mal dran setzen
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.361 Beiträge
 
Delphi 12 Athens
 
#10

AW: Minesweeper

  Alt 24. Mär 2015, 18:25
Und anstatt du Dinger nachträglich zu suchen, hätte man die auch automatisch erstellen können, dabei einem zweidimensionalem Array zugewiesen und hätte den ganze Quatsch mit den Namen auch lassen können.

PS: Man kann Zahlen auch immer zweistellig schreiben. 01
Ein Therapeut entspricht 1024 Gigapeut.
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 01:10 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz