Autor Beitrag
mimi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: Mo 01.11.04 11:13 
Hallo,
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:
procedure TForm1.Ausgang;
begin
  ShowMessage('Ziel gefunden');
end;

procedure TForm1.WegFinden(x,y:Integer);
begin
  if (Map[x,y].Name = '') or ((Ziel.X <> x) and (Ziel.Y <> y))  then begin
    if (Ziel.X = x) and (Ziel.Y = y) then
      Ausgang
    else begin
      if Map[x,y].Name = '' then begin
        Map[x,y].Name:='m';
        WegFinden (x,y+1);  {nach rechts}
        WegFinden (x+1,y);  {nach unten }
        WegFinden (x,y-1);  {nach links }
        WegFinden (x-1,y);  {nach oben  }
        Map[x,y].name:='';

      end;
    end;
  end;
end;

warum hängt sich das Programm auf, wenn er das ziel gefunden hat ?
ich habe eine karte die ich per zufall mit steinen fülle und das ziel wird auch zufällig gesetzt und der start auch.

_________________
MFG
Michael Springwald, "kann kein englisch...."
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mo 01.11.04 11:18 
Ich hab deinen Source nur mal überflogen.
Meine Vermutung:
Angenommen du kommst irgendwann über deinen ersten Fall (nach rechts) in der Rekursion zum Ausgang, werden nach dem Rücksprung aus der Rekursion immer noch die anderen 3 Fälle durchgerechnet.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 01.11.04 11:21 
Du musst die outine WegFinden als Funktion machen und im Erfolgsfall True, bei Misserfolg False zurückgeben und die Routine im Else-Zweig verlassen, wenn der Weg bereits gefunden wurde.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
mimi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: Mo 01.11.04 11:42 
BeenBE: das werde ich gleich mal testen
Jasocul: aber sobalt das ziel gefunden wird, müsste der else teiel doch nicht ausgewührt werden oder ?

_________________
MFG
Michael Springwald, "kann kein englisch...."
mimi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: Mo 01.11.04 11:46 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
function TForm1.WegFinden(x,y:Integer):Boolean;
begin
  if (Map[x,y].Name = '') or ((Ziel.X <> x) and (Ziel.Y <> y))  then begin
    if (Ziel.X = x) and (Ziel.Y = y) then begin
      Ausgang;
      result:=True;
    end;

    if result = False then begin
      if Map[x,y].Name = '' then begin
        Map[x,y].Name:='m';
        WegFinden (x,y+1);  {nach rechts}
        WegFinden (x+1,y);  {nach unten }
        WegFinden (x,y-1);  {nach links }
        WegFinden (x-1,y);  {nach oben  }
        Map[x,y].name:='';
        result:=False;
      end;
    end;
  end;
end;

jetzt habe ich das so, nun werden verschiende ziele ausgesucht aber ich woltle gerne, sobalt er das ziel gefunden hat, soll er die funktion beenden und den weg zum ziel anzeigen.

_________________
MFG
Michael Springwald, "kann kein englisch...."
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mo 01.11.04 11:57 
Kleine Änderung ist noch erforderlich:
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:
28:
function TForm1.WegFinden(x,y:Integer):Boolean;
var
  ergebnis : boolean;
begin  
  if (Map[x,y].Name = '') or ((Ziel.X <> x) and (Ziel.Y <> y))  then begin  
    if (Ziel.X = x) and (Ziel.Y = y) then begin  
      Ausgang;  
      result:=True;  
    end;  

 
    if result = False then begin  
      if Map[x,y].Name = '' then begin  
        Map[x,y].Name:='m';  
        Ergebnis := WegFinden (x,y+1);  {nach rechts}  
        if Ergebnis then exit;
        Ergebnis := WegFinden (x+1,y);  {nach unten }  
        if Ergebnis then exit;
        Ergebnis := WegFinden (x,y-1);  {nach links }  
        if Ergebnis then exit;
        Ergebnis := WegFinden (x-1,y);  {nach oben  }  
        if Ergebnis then exit;
        Map[x,y].name:='';  
        result:=False;  
      end;  
    end;  
  end;  
end;

sonst läuft er die anderen Aufrufe auch noch durch.

EDIT: Bevor einer mault: Das Exit habe ich hier zur besseren Übersicht eingebaut. Stylistisch besser ist natürlich ein geschachteltes if.
mimi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: Mo 01.11.04 12:58 
hängt sich leider immer noch auf:
wenn ich die funktion starte kommt einmal die showmessage aus der Ausgang funktion wenn ich dann auf OK klicke komme sie noch 2 mal und anschließend hängt sich das Programm auf.

_________________
MFG
Michael Springwald, "kann kein englisch...."
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Mo 01.11.04 13:05 
am anfang der funktion nen result := false hin?
mimi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: Mo 01.11.04 13:09 
nein, geht immer noch nicht so wie es gehen soll :(
(Hängt sich leider immer noch auf)

_________________
MFG
Michael Springwald, "kann kein englisch...."
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Mo 01.11.04 13:16 
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:
var weggefunden: boolean = false;
procedure TForm1.WegFinden(x,y:Integer); 
begin 
  if not weggefunden then
  begin
    if (Map[x,y].Name = '') or ((Ziel.X <> x) and (Ziel.Y <> y))  then
    begin 
      if (Ziel.X = x) and (Ziel.Y = y) then
      begin 
        Ausgang;
        weggefunden := true;
      end else 
      if Map[x,y].Name = '' then
      begin 
        Map[x,y].Name:='m'; 
        WegFinden (x,y+1);  {nach rechts} 
        WegFinden (x+1,y);  {nach unten } 
        WegFinden (x,y-1);  {nach links } 
        WegFinden (x-1,y);  {nach oben  } 
        Map[x,y].name:=''; 
      end;
    end;  
  end; 
end;
mimi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: Mo 01.11.04 13:24 
@uall@ogc
danke, es hat funtkioniert.

_________________
MFG
Michael Springwald, "kann kein englisch...."