Autor Beitrag
Gurke
Hält's aus hier
Beiträge: 13



BeitragVerfasst: Sa 17.11.07 16:12 
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.

ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 148

XP
D6, D2007 prof
BeitragVerfasst: Sa 17.11.07 16:33 
Der Befehl Goto springt an ein Label, damit ist NICHT TLabel gemeint, sondern ein declariertes Label (Ansprungpunkt)
ausblenden 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:
ausblenden Delphi-Quelltext
1:
Label s1, s1, s3;					

_________________
Wir haben für jede Lösung das richtige Problem ;)
Gurke Threadstarter
Hält's aus hier
Beiträge: 13



BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 148

XP
D6, D2007 prof
BeitragVerfasst: Sa 17.11.07 16:47 
ausblenden 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 ;)

_________________
Wir haben für jede Lösung das richtige Problem ;)
Gurke Threadstarter
Hält's aus hier
Beiträge: 13



BeitragVerfasst: 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



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:
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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 148

XP
D6, D2007 prof
BeitragVerfasst: 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:

_________________
Wir haben für jede Lösung das richtige Problem ;)
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Sa 17.11.07 17:49 
Warum nicht einfach so?

ausblenden 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:

ausblenden 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.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)