Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - Wie geht das?? Goto label[array]; +random


Gurke - Sa 17.11.07 16:12
Titel: Wie geht das?? Goto label[array]; +random
Hi, ich habe ein Problem bei dem ich nicht weiterweiß.
Ich will das mein Programm ein random durchführt und dieses random soll dann halt angeben zu welchem label gesprungen werden soll.


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
s: array[1..3of label;
.
.
.
procedure TForm1.compnormal1();
 var m:integer;
 label s[1],s[2],s[3];

begin
randomize;
m:=random(3)+1;
goto s[m];


s[1]: if ...then... else
s[2]: if ...then... else
s[3]: if ...then... else ...;

Der Quelltext ist natürlich nicht vollständig also ends und alles weitere fehlt ja. das seht ihr ja.. ich wollt ja auch nur das ihr seht was ich meine

Versteht ihr was ich meine? das läuft so irgendwie nicht und ich weiß auch nicht wie ich das zum laufen bekomme oder ob das überhaupt geht.
Wäre echt dankbar für Hilfe :)

gruß
Gurke

Moderiert von user profile iconNarses: Code- durch Delphi-Tags ersetzt


TProgger - Sa 17.11.07 16:33

Der Befehl Goto springt an ein Label, damit ist NICHT TLabel gemeint, sondern ein declariertes Label (Ansprungpunkt)

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
Label Dahin;
begin
  //irgendwas machen
Dahin:
  //noch was machen
  if irgendneBedingung then Goto Dahin; //zum Label springen
end

Und Sprung-Labels können meines Wissens kein Array sein.
Mach doch einfach:

Delphi-Quelltext
1:
Label s1, s1, s3;                    


Gurke - Sa 17.11.07 16:44

hm ok , kann wohl sein das spung-labels keine arrays sein können.. weiß ich nicht
dann werde ich das schon mal entfernen.

aber wenn ich das "Label s1, s2, s3;" mache, wie mache ich das denn dann mit dem random?

der soll ja zu einem zufälligen sprung-label gehen.
ich muss dem ja irgendwie die zufallszahl geben.


TProgger - Sa 17.11.07 16:47


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
randomize;
m:=random(3)+1;
case m of
  1goto s1;
  2goto s2;
  3goto s3;
end;

Aber warum nimmst du überhaupt Goto?
Eigentlich sollte man das vermeiden ;)


Gurke - Sa 17.11.07 16:58

:) danke werds mal ausprobieren..

naja anders wüsste ich nicht wie ich das sonst machen sollte
es geht da um ein spiel und der soll halt zu einem zufälligem spielzug springen und dann alles durchlaufen :) und wenn der keinen da drunter oder so gefunden hat dann soll er die oberen auch nochmal durchlaufen und wenn da nichts gefunden wurde setz der halt auf nem random paltz ;)

---> ich weiß schlechte programmierung :) hauptsache es läuft




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:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
procedure TForm1.compnormal1();
  var m:integer;
 label l2,l3,l4,s1,s2,s3;

begin
randomize;
m:=random(3)+1;
case m of  
  1goto s1;  
  2goto s2;  
  3goto s3;
end


 l2:
if pfelder[1].caption='' then goto l3 else
if pfelder[2].caption='' then goto l3 else
if pfelder[3].caption='' then goto l3 else
if pfelder[4].caption='' then goto l3 else
if pfelder[5].caption='' then goto l3 else
if pfelder[6].caption='' then goto l3 else
if pfelder[7].caption='' then goto l3 else
if pfelder[8].caption='' then goto l3 else
if pfelder[9].caption='' then goto l3 else
goto l4;
l3:

...

s1: if (pfelder[1].caption='X'and (pfelder[2].caption='X'and (pfelder[3].caption=''then begin pfelder[3].caption:='O'; pfelder[3].font.Color:=clred; label1.Caption:=edit1.Text; label1.font.Color:=cllime; end else
s2: if (pfelder[2].caption='X'and (pfelder[3].caption='X'and (pfelder[1].caption=''then begin pfelder[1].caption:='O'; pfelder[1].font.Color:=clred; label1.Caption:=edit1.Text; label1.font.Color:=cllime; end else
s3: if (pfelder[1].caption='X'and (pfelder[3].caption='X'and (pfelder[2].caption=''then begin pfelder[2].caption:='O'; pfelder[2].font.Color:=clred; label1.Caption:=edit1.Text; label1.font.Color:=cllime; end else
if (pfelder[4].caption='X'and (pfelder[5].caption='X'and (pfelder[6].caption=''then begin pfelder[6].caption:='O'; pfelder[6].font.Color:=clred; label1.Caption:=edit1.Text; label1.font.Color:=cllime; end else
if (pfelder[5].caption='X'and (pfelder[6].caption='X'and (pfelder[4].caption=''then begin pfelder[4].caption:='O'; pfelder[4].font.Color:=clred; label1.Caption:=edit1.Text; label1.font.Color:=cllime; end else
if (pfelder[4].caption='X'and (pfelder[6].caption='X'and (pfelder[5].caption=''then begin pfelder[5].caption:='O'; pfelder[5].font.Color:=clred; label1.Caption:=edit1.Text; label1.font.Color:=cllime; end else
if (pfelder[7].caption='X'and (pfelder[8].caption='X'and (pfelder[9].caption=''then begin pfelder[9].caption:='O'; pfelder[9].font.Color:=clred; label1.Caption:=edit1.Text; label1.font.Color:=cllime; end else
if (pfelder[8].caption='X'and (pfelder[9].caption='X'and (pfelder[7].caption=''then begin pfelder[7].caption:='O'; pfelder[7].font.Color:=clred; label1.Caption:=edit1.Text; label1.font.Color:=cllime; end else
if (pfelder[7].caption='X'and (pfelder[9].caption='X'and (pfelder[8].caption=''then begin pfelder[8].caption:='O'; pfelder[8].font.Color:=clred; label1.Caption:=edit1.Text; label1.font.Color:=cllime; end else
if (pfelder[7].caption='X'and (pfelder[4].caption='X'and (pfelder[1].caption=''then begin pfelder[1].caption:='O'; pfelder[1].font.Color:=clred; label1.Caption:=edit1.Text; label1.font.Color:=cllime; end else
if (pfelder[4].caption='X'and (pfelder[1].caption='X'and (pfelder[7].caption=''then begin pfelder[7].caption:='O'; pfelder[7].font.Color:=clred; label1.Caption:=edit1.Text; label1.font.Color:=cllime; end else
[...]

...


begin
randomize;
k:=random(9)+1;


if (pfelder[k].caption<>'X')and(pfelder[k].caption<>'O'then begin
pfelder[k].caption:='O';
pfelder[k].font.Color:=clred;
label1.Caption:=edit1.Text;
label1.font.Color:=cllime;
end else goto l2;
end;
l4:
end;



Alles klar läuft bestens :)
DANKE TProgger ;)

Moderiert von user profile iconNarses: Code- durch Delphi-Tags ersetzt


TProgger - Sa 17.11.07 17:09

hehe, du solltest am Wettbewerb für den besten Spaghetti-Code teilnehmen :wink:
Aber solange es klappt und du noch durchblickst, ist das schon okay, aber ich würd vorschlagen, für die Zukunft andere, effektivere Lösungen für sowas zu nehmen. :lol:


GTA-Place - Sa 17.11.07 17:49

Warum nicht einfach so?


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
X := Random(3);

if X = 1 then
begin
  // Tue was
end;

if X = 2 then
begin
  // Tue was anderes
end;


Oder noch einfacher:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
case Random(3of 
  1
    begin
      // Tue was
    end;

  2:
    begin
      // Tue was anderes
    end;
end;


Ein Goto findet für andere Dinge Verwendung, aber nicht in sowas simplen.