Hallo, ich hab mal versucht einen Sudoku-Generator zu schreiben.
Leider hängt der sich jedesmal auf.
Könnt ihr da mal nen Blick draufwerfen?
Code:
constructor s.create();
var
i, j: integer;
begin
for i:=0 to 8 do
for j:=0 to 8 do
f[i,j]:=0;
end;
procedure s.generate();
var
i, j: integer;
s: string;
begin
i:=0;
while i<=8 do
begin
j:=0;
while j<=8 do
begin
f[i,j]:=Random(9)+1;
j:=j+1;
end;
if (fArrOK(f))or(i>5) then
i:=i+1;
end;
for i:=0 to 8 do
begin
s:=s+#13#10;
for j:=0 to 8 do
s:=s+' '+inttostr(f[j,i]);
end;
ShowMessage(s);
end;
function s.rArrOK(reihe: rArr): boolean;
var
i, a: integer;
b: boolean;
n: array[1..9] of integer;
begin
b:=true;
for a:=1 to 9 do
n[a]:=0;
for i:=0 to 8 do
for a:=1 to 9 do
if reihe[i]=a then
n[a]:=n[a]+1;
for a:=1 to 9 do
if n[a]>1 then
b:=false;
result:=b;
end;
function s.qArrOK(quadrat: qArr): boolean;
var
i, j, a: integer;
b: boolean;
n: array[1..9] of integer;
begin
b:=true;
for a:=1 to 9 do
n[a]:=0;
for i:=0 to 2 do
for j:=0 to 2 do
for a:=1 to 9 do
if quadrat[i,j]=a then
n[a]:=n[a]+1;
for a:=1 to 9 do
if n[a]>1 then
b:=false;
result:=b;
end;
function s.fArrOK(feld: fArr): boolean;
var
i, j, x, y: integer;
s, r: rArr;
q: qArr;
b: boolean;
begin
b:=true;
//Spalten
for i:=0 to 8 do
begin
for j:=0 to 8 do
s[j]:=f[i,j];
if not(rArrOK(s)) then
b:=false;
end;
//Reihen
for i:=0 to 8 do
begin
for j:=0 to 8 do
r[j]:=f[j,i];
if not(rArrOK(r)) then
b:=false;
end;
//Quadrate
for i:=0 to 2 do
for j:=0 to 2 do
begin
for x:=0 to 2 do
for y:=0 to 2 do
q[x,y]:=f[i*3+x,j*3+y];
if not(qArrOK(q)) then
b:=false;
end;
result:=b;
end;