Einzelnen Beitrag anzeigen

roboter202

Registriert seit: 6. Mär 2011
98 Beiträge
 
Delphi 6 Professional
 
#1

Stack Overflow - Dynamische Arrays

  Alt 3. Mai 2012, 21:36
Hi,


Ich generiere ein Labyrinth mit einer rekursiven Prozedur.



ich hab den Code:

Delphi-Quellcode:
procedure TWorld.generate(_x, _y: Integer);
var
  neighbours: array of TVect2i;
  r: ShortInt;
begin
  //list of unvisited neighbours
  if (_y <> 0) then
  begin
    if (cellMap[_x, _y -1] = false) then //TOP
    begin
      SetLength(neighbours, Length(neighbours) +1);
      neighbours[high(neighbours)].x := _x;
      neighbours[high(neighbours)].x := _y -1;
    end;
  end;

  if (_x <> 0) then
  begin
    if (cellMap[_x -1, _y] = false) then //LEFT
    begin
      SetLength(neighbours, Length(neighbours) +1);
      neighbours[high(neighbours)].x := _x -1;
      neighbours[high(neighbours)].x := _y ;
    end;
  end;

  if (_y <> height -1) then
  begin
    if (cellMap[_x, _y +1] = false) then //Bottom
    begin
      SetLength(neighbours, Length(neighbours) +1);
      neighbours[high(neighbours)].x := _x ;
      neighbours[high(neighbours)].x := _y +1;
    end;
  end;

  if (_x <> width -1) then
  begin
    if (cellMap[_x +1, _y] = false) then //RIGHT
    begin
      SetLength(neighbours, Length(neighbours) +1);
      neighbours[high(neighbours)].x := _x +1;
      neighbours[high(neighbours)].x := _y ;
    end;
  end;
  //

  if not cellMap[_x, _y] then //Was this cell vistited?
  begin
    //Mark as visited
    cellMap[_x, _y] := true;
    if Length(neighbours) > 1 then //Can I go on with other paths here?
    begin
      //Move to Stack
      setLength(cellStack, Length(cellStack) +1);
      cellStack[high(cellStack)].x := _x;
      cellStack[high(cellStack)].x := _y;
    end;
  end;


  if Length(neighbours) > 0 then
  begin
    //Pick a random neighbour
    r := Random(Length(neighbours));
    //Remove Wall
    if neighbours[r].x > _x then
    begin
      //Right
      wallMap[_x, _y, 1] := true;
    end else
    if neighbours[r].x < _x then
    begin
      //Left
      wallMap[_x -1, _y, 1] := true;
    end else
    if neighbours[r].y > _y then
    begin
      //Bottom
      wallMap[_x, _y, 0] := true;
    end else
    if neighbours[r].y < _y then
    begin
      //Top
      wallMap[_x, _y -1, 0] := true;
    end;
    generate(neighbours[r].x, neighbours[r].y);

  end else
  begin
    if stackPos < high(cellStack) then
    begin
      stackPos := stackPos +1;
      generate(cellStack[stackPos].x, cellStack[stackPos].y)
    end;
  end;
end;
und was auch noch wichtig sein könnte ist


Delphi-Quellcode:
     
cellMap: array of array of Boolean;
wallMap: array of array of array [0..1] of Boolean; //0 => Horizontal; 1 => Vertical; X,Y of Cell to Left Top
cellStack: array of TVect2i;
und

Delphi-Quellcode:
  
TVect2i = record
    x,y: Integer;
  end;
Jetzt bekomme ich wenn ich width und height auf 100 setze einen Stack Overflow. Warum ich hab gelesen das es bei einem rekursiven Prozeduraufruf passieren kann. (Hab ich ja) aber was kann ich denn jetzt tun. Muss ich das Zeug was die Prozedur braucht auf einen dynamischen Array schmeißen oder geht das auch anders?

Gruß roboter202
Christian
i := 0 ; While i = 0 do beep ;
  Mit Zitat antworten Zitat