![]() |
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:
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.
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; 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. |
AW: Problem mit Würfelsimulation
Delphi-Quellcode:
SetLength immer die Anzahl der Elemente + 1, das erste Element ist bei dem Index 0.
function TForm1.Dice(dipo: integer) : integer;
var a:array of integer; x,i:integer; begin SetLength(a,dipo + 1); // <-<< //... |
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 21:05 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz