Autor Beitrag
Jetstream
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 222



BeitragVerfasst: So 25.04.04 15:19 
Hi, ich hab folgendes Problem und fände es cool, wenn ihr mir helfen könntet:

Ich habe in Delphi ein Programm geschrieben, eine Art Brettspiel, bei dem es viel um Knöpfe geht. Die 24 Felder (6x4) sind nämlich jeweils mit einem Knopf versehen, die ich wie beim Schach mit Namen versehen habe. (A1 .. A6, B1 .. B6, C1 .. C6, D1 .. D6)

Ich habe für jede Spielfigur einen Zeiger, der auf einen solchen Button zeigt (Um den momentanen Aufenthaltsort abfragen zu können)

Wenn ich diesen Aufenthaltsort nun aber abfragen will, was ich ca. 40 mal mache, geht das nur ungefähr so:

ausblenden Delphi-Quelltext
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:
for Horizontal:='A' to 'D' do for Vertikal:=1 to 6 do
begin
    if (Horizontal='A') and (Vertikal=1) and (ZeigerWeissStein=@Form1.A1) then MomentanerStein:='WeissStein';
    if (Horizontal='A') and (Vertikal=2) and (ZeigerWeissStein=@Form1.A2) then MomentanerStein:='WeissStein';
    if (Horizontal='A') and (Vertikal=3) and (ZeigerWeissStein=@Form1.A3) then MomentanerStein:='WeissStein';
    if (Horizontal='A') and (Vertikal=4) and (ZeigerWeissStein=@Form1.A4) then MomentanerStein:='WeissStein';
    if (Horizontal='A') and (Vertikal=5) and (ZeigerWeissStein=@Form1.A5) then MomentanerStein:='WeissStein';
    if (Horizontal='A') and (Vertikal=6) and (ZeigerWeissStein=@Form1.A6) then MomentanerStein:='WeissStein';
    if (Horizontal='B') and (Vertikal=1) and (ZeigerWeissStein=@Form1.B1) then MomentanerStein:='WeissStein';
    if (Horizontal='B') and (Vertikal=2) and (ZeigerWeissStein=@Form1.B2) then MomentanerStein:='WeissStein';
    if (Horizontal='B') and (Vertikal=3) and (ZeigerWeissStein=@Form1.B3) then MomentanerStein:='WeissStein';
    if (Horizontal='B') and (Vertikal=4) and (ZeigerWeissStein=@Form1.B4) then MomentanerStein:='WeissStein';
    if (Horizontal='B') and (Vertikal=5) and (ZeigerWeissStein=@Form1.B5) then MomentanerStein:='WeissStein';
    if (Horizontal='B') and (Vertikal=6) and (ZeigerWeissStein=@Form1.B6) then MomentanerStein:='WeissStein';
    if (Horizontal='C') and (Vertikal=1) and (ZeigerWeissStein=@Form1.C1) then MomentanerStein:='WeissStein';
    if (Horizontal='C') and (Vertikal=2) and (ZeigerWeissStein=@Form1.C2) then MomentanerStein:='WeissStein';
    if (Horizontal='C') and (Vertikal=3) and (ZeigerWeissStein=@Form1.C3) then MomentanerStein:='WeissStein';
    if (Horizontal='C') and (Vertikal=4) and (ZeigerWeissStein=@Form1.C4) then MomentanerStein:='WeissStein';
    if (Horizontal='C') and (Vertikal=5) and (ZeigerWeissStein=@Form1.C5) then MomentanerStein:='WeissStein';
    if (Horizontal='C') and (Vertikal=6) and (ZeigerWeissStein=@Form1.C6) then MomentanerStein:='WeissStein';
    if (Horizontal='D') and (Vertikal=1) and (ZeigerWeissStein=@Form1.D1) then MomentanerStein:='WeissStein';
    if (Horizontal='D') and (Vertikal=2) and (ZeigerWeissStein=@Form1.D2) then MomentanerStein:='WeissStein';
    if (Horizontal='D') and (Vertikal=3) and (ZeigerWeissStein=@Form1.D3) then MomentanerStein:='WeissStein';
    if (Horizontal='D') and (Vertikal=4) and (ZeigerWeissStein=@Form1.D4) then MomentanerStein:='WeissStein';
    if (Horizontal='D') and (Vertikal=5) and (ZeigerWeissStein=@Form1.D5) then MomentanerStein:='WeissStein';
    if (Horizontal='D') and (Vertikal=6) and (ZeigerWeissStein=@Form1.D6) then MomentanerStein:='WeissStein';
end;



Dass diese 24 Zeilen für eine einzige Abfrage zu lang sind, ist wohl jedem klar.
Man muss Delphi noch klar sagen, dass wenn man 'A' und 1 eingibt, es doch bitte den Button A1 ensteuert.
Wenn man das für jeden Button macht, etsteht das Gebilde da oben.
Diese 24 Zeilen lassen sichvom Prinzip her auf eine Zeile vereinfachen durch eine kleine Funktion:

ausblenden Delphi-Quelltext
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:
function Knopf(Hor:char;Ver:integer):TSpeedButton;
begin
  if (Hor='A') and (Ver=1) then Knopf:=Form1.A1;
  if (Hor='A') and (Ver=2) then Knopf:=Form1.A2;
  if (Hor='A') and (Ver=3) then Knopf:=Form1.A3;
  if (Hor='A') and (Ver=4) then Knopf:=Form1.A4;
  if (Hor='A') and (Ver=5) then Knopf:=Form1.A5;
  if (Hor='A') and (Ver=6) then Knopf:=Form1.A6;
  if (Hor='B') and (Ver=1) then Knopf:=Form1.B1;
  if (Hor='B') and (Ver=2) then Knopf:=Form1.B2;
  if (Hor='B') and (Ver=3) then Knopf:=Form1.B3;
  if (Hor='B') and (Ver=4) then Knopf:=Form1.B4;
  if (Hor='B') and (Ver=5) then Knopf:=Form1.B5;
  if (Hor='B') and (Ver=6) then Knopf:=Form1.B6;
  if (Hor='C') and (Ver=1) then Knopf:=Form1.C1;
  if (Hor='C') and (Ver=2) then Knopf:=Form1.C2;
  if (Hor='C') and (Ver=3) then Knopf:=Form1.C3;
  if (Hor='C') and (Ver=4) then Knopf:=Form1.C4;
  if (Hor='C') and (Ver=5) then Knopf:=Form1.C5;
  if (Hor='C') and (Ver=6) then Knopf:=Form1.C6;
  if (Hor='D') and (Ver=1) then Knopf:=Form1.D1;
  if (Hor='D') and (Ver=2) then Knopf:=Form1.D2;
  if (Hor='D') and (Ver=3) then Knopf:=Form1.D3;
  if (Hor='D') and (Ver=4) then Knopf:=Form1.D4;
  if (Hor='D') and (Ver=5) then Knopf:=Form1.D5;
  if (Hor='D') and (Ver=6) then Knopf:=Form1.D6;
end;



Mit dieser Funktion müsste es nun möglich sein, den Text von weiter oben wie folgt zusammenzufassen:

for Horizontal:='A' to 'D' do for Vertikal:=1 to 6 do If ZeigerWeissStein=@Knopf(Horizontal,Vertikal) then MomentanerStein:='WeissStein';

Das dumme ist nur, weshalb ich das hier auch poste, dass ich dabei eine Fehlermeldung erhalte: [Error] Spiel.pas (139): Variable required

Der Cursor springt dabei hinter das n von then.


Die Funktion Knopf an sich funktioniert einwandfrei bei Befehlen wie:

Form1.WeissStein.Top:=Knopf(Horizontal,Vertikal).Top;

Nur bei Pointern hat er Probleme.


Wenn ihr mir helfen könntet, würde mein Programm von ca. 2000 Zeilen auf 200 schrumpfen,
schon mal Danke im Vorraus . :)

Moderiert von user profile iconMotzi: Delphi-Tags hinzugefügt.
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: So 25.04.04 18:10 
Schon mal was von CASE gehört ?

_________________
Gruß
Hansa
Jetstream Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 222



BeitragVerfasst: So 25.04.04 19:02 
was bringt mir case ? dann sind es immernoch 24 Zeilen.
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: So 25.04.04 19:10 
Kannst ja auch alles in eine Zeile schreiben. 8)

_________________
Gruß
Hansa
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: So 25.04.04 19:45 
Warum überhaupt das "@" davor..? Objekte sind doch bereits Referenzen, also Pointer..!

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: So 25.04.04 21:12 
Hmm, wenn ich das richtig sehe gehst du Horizontal und Vertikal durch und schnappst dir alle Elemente von Form1, die "[Horizontal]+[Vertikal]" heißen.
Da könntest du das um einiges Verkürzen, indem du Suche in: Delphi-Forum, Delphi-Library FINDCOMPONENT vewendest:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
for Horizontal:='A' to 'D' do for Vertikal:=1 to 6 do
begin
  if ZeigerWeissStein^ = (Form1.FindComponent(Horizontal + Vertikal) as TSpeedButton) then
    MomentanerStein:='WeissStein';
end;
Ungetestet und ohne Wissen, auf welchen Typ genau "ZeigerWeissStein" zeigt, aber in etwa so sollte's klappen, zumindest vom Grundgedanken...

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
Jetstream Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 222



BeitragVerfasst: So 25.04.04 22:56 
Danke für die vielen Antworten (vor allem das mit FindComponent fand ich sehr interessant), ich habs inzwischen selber gelöst mit ner neuen Function.

ausblenden Delphi-Quelltext
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:
function ZeigerKnopf(Hor:char;Ver:integer):Pointer; 
begin 
  if (Hor='A') and (Ver=1) then Knopf:=@Form1.A1; 
  if (Hor='A') and (Ver=2) then Knopf:=@Form1.A2; 
  if (Hor='A') and (Ver=3) then Knopf:=@Form1.A3; 
  if (Hor='A') and (Ver=4) then Knopf:=@Form1.A4; 
  if (Hor='A') and (Ver=5) then Knopf:=@Form1.A5; 
  if (Hor='A') and (Ver=6) then Knopf:=@Form1.A6; 
  if (Hor='B') and (Ver=1) then Knopf:=@Form1.B1; 
  if (Hor='B') and (Ver=2) then Knopf:=@Form1.B2; 
  if (Hor='B') and (Ver=3) then Knopf:=@Form1.B3; 
  if (Hor='B') and (Ver=4) then Knopf:=@Form1.B4; 
  if (Hor='B') and (Ver=5) then Knopf:=@Form1.B5; 
  if (Hor='B') and (Ver=6) then Knopf:=@Form1.B6; 
  if (Hor='C') and (Ver=1) then Knopf:=@Form1.C1; 
  if (Hor='C') and (Ver=2) then Knopf:=@Form1.C2; 
  if (Hor='C') and (Ver=3) then Knopf:=@Form1.C3; 
  if (Hor='C') and (Ver=4) then Knopf:=@Form1.C4; 
  if (Hor='C') and (Ver=5) then Knopf:=@Form1.C5; 
  if (Hor='C') and (Ver=6) then Knopf:=@Form1.C6; 
  if (Hor='D') and (Ver=1) then Knopf:=@Form1.D1; 
  if (Hor='D') and (Ver=2) then Knopf:=@Form1.D2; 
  if (Hor='D') and (Ver=3) then Knopf:=@Form1.D3; 
  if (Hor='D') and (Ver=4) then Knopf:=@Form1.D4; 
  if (Hor='D') and (Ver=5) then Knopf:=@Form1.D5; 
  if (Hor='D') and (Ver=6) then Knopf:=@Form1.D6; 
end;


Jetzt funzt die Zeile
ausblenden Delphi-Quelltext
1:
for Horizontal:='A' to 'D' do for Vertikal:=1 to 6 do If ZeigerWeissStein=ZeigerKnopf(Horizontal,Vertikal) then MomentanerStein:='WeissStein';					


Mein Programm wurde somit von 2000 auf 800 Zeilen abgespeckt. :D


Moderiert von user profile icontommie-lie: Code- durch Delphi-Tags ersetzt
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: So 25.04.04 23:07 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
function ZeigerKnopf(Hor:char;Ver:integer):Pointer; 
begin 
   if (Hor='A') then begin
     case Ver of 
        1 : Knopf:=@Form1.A1; 
        2 : Knopf:=@Form1.A2; 
        3 : Knopf:=@Form1.A3; 
        4 : Knopf:=@Form1.A4; 
        5 : Knopf:=@Form1.A5; 
        6 : Knopf:=@Form1.A6;  
     end;
  end;
... usw.

end;


Die Zeilenanzahl bleibt in etwa gleich, aber das sieht doch wohl etwas übersichtlicher aus. 8)

_________________
Gruß
Hansa
Jetstream Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 222



BeitragVerfasst: Mo 26.04.04 06:18 
Hmmm, ich versteh beides ganz gut :)

Warum nicht gleich so:

ausblenden volle Höhe Delphi-Quelltext
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:
Function Knopf(Hor:char;Ver:integer):TSpeedButton;
begin
  case Hor of
    'A':
    begin
      case Ver of
        1: Knopf:=Form1.A1;
        2: Knopf:=Form1.A2;
        3: Knopf:=Form1.A3;
        4: Knopf:=Form1.A4;
        5: Knopf:=Form1.A5;
        6: Knopf:=Form1.A6;
      end;
    end;
    'B':
    begin
      case Ver of
        1: Knopf:=Form1.B1;
        2: Knopf:=Form1.B2;
        3: Knopf:=Form1.B3;
        4: Knopf:=Form1.B4;
        5: Knopf:=Form1.B5;
        6: Knopf:=Form1.B6;
      end;
    end;
    'C':
    begin
      case Ver of
        1: Knopf:=Form1.C1;
        2: Knopf:=Form1.C2;
        3: Knopf:=Form1.C3;
        4: Knopf:=Form1.C4;
        5: Knopf:=Form1.C5;
        6: Knopf:=Form1.C6;
      end;
    end;
    'D':
    begin
      case Ver of
        1: Knopf:=Form1.D1;
        2: Knopf:=Form1.D2;
        3: Knopf:=Form1.D3;
        4: Knopf:=Form1.D4;
        5: Knopf:=Form1.D5;
        6: Knopf:=Form1.D6;
      end;
    end;
  end;
end;


Das ist nicht wirklich eine Ersparnis.

//Edit Ups im Code vertan aber jetzt isser richtig

Moderiert von user profile iconMotzi: Code- durch Delphi-Tags ersetzt.