Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Problem mit Würfelsimulation (https://www.delphipraxis.net/153697-problem-mit-wuerfelsimulation.html)

Sorakun 12. Aug 2010 22:31

Problem mit Würfelsimulation
 
Ich versuche gerade einen Würfelsimulator/Wahrscheinlichkeitsrechner für das P&P-RPG Double Cross The 3rd Edition zu schreiben und bin mit der Würfelsimulation eigentlich schon fertig, allerdings will es nicht ganz funktionieren...

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(Roll(StrToInt(DP.text),StrToInt(CT.text)));
end;

function TForm1.Roll(dipo, crth: integer) : string;
var t, c, i, j: integer;
    a: array of integer;
    m: string;
begin
  i:=0;
  t:=0;
  repeat
     c:=Dice(dipo);
     i:= i+1;
     SetLength(a,i);
     a[i-1]:= c;
     t:= t+c;
  until c < crth;
  m:= 'Result: ' + IntToStr(a[0]); //Exception 1
  j:=1;
  while j < i do
  begin
     m:= m + '+' + IntToStr(a[j]);
     j:= j+1;
  end;
  m:= m + ' = ' + IntToStr(t);
  result:= m;
end;

function TForm1.Dice(dipo: integer) : integer;
var a:array of integer;
    x,i:integer;
begin
  SetLength(a,dipo);               //Exception 2   
  for i:=1 to dipo do begin
      x:= Random(10)+1;
      a[i]:=x;
  end;
  for i:=2 to dipo do begin
      if a[i]>a[1] then
         a[1]:=a[i];
  end;
  Result := a[1];                  //Exception 3+4
end;
Die Idee dabei ist, dass man einen bestimmten Dice Pool, also eine bestimmte Anzahl von 10-seitigen Würfeln hat, mit denen man würfelt, wobei das Ergebnis der höchste Wurf ist. Ist dieser höchste Wurf gleich oder über dem vorher festgelegten Critical Threshold, so würfelt man noch mal und addiert dieses Ergebnis hinzu. Das geht so lange weiter, bis ein Wurf unter dem CT liegt.
Ich kann zwar keinen Fehler im Code entdecken, aber ich bekomme jedes Mal eine von diesen Exceptions, nach denen das Programm nicht weiter läuft. (Test mit DP: 1 und CT: 10)

Nach m:= 'Result: ' + IntToStr(a[0]);
Project Project1.exe raised exception class C0000005 with message 'access violation at 0x00401c70: write of address 0x0000000a'. Process stopped. Use Step or Run to continue.

Nach SetLength(a,dipo); (2. Aufruf)
Project Project1.exe raised exception class C0000005 with message 'access violation at 0x00401c70: write of address 0x0000000e'. Process stopped. Use Step or Run to continue.

Nach Result := a[1];
Project Project1.exe raised exception class C0000005 with message 'access violation at 0x00401c70: write of address 0x0000000c'. Process stopped. Use Step or Run to continue. (address immer unterschiedlich)
oder
Project Project1.exe raised exception class EInvalidPointer with message 'Invalid pointer operation'. Process stopped. Use Step or Run to continue.


Kann bitte jemand sagen, wo der Fehler liegt?
Vielen Dank im Voraus.

turboPASCAL 13. Aug 2010 05:15

AW: Problem mit Würfelsimulation
 
Delphi-Quellcode:
function TForm1.Dice(dipo: integer) : integer;
var a:array of integer;
    x,i:integer;
begin
  SetLength(a,dipo + 1); // <-<<
  //...
SetLength immer die Anzahl der Elemente + 1, das erste Element ist bei dem Index 0.

Sorakun 13. Aug 2010 14:43

AW: Problem mit Würfelsimulation
 
Vielen Dank.
Ich fühle mich gerade ganz schön dämlich... Daran hätte ich eigentlich denken müssen.


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:26 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz