fb:porticula NoPaste
SuDoku generator
Uploader: | gast |
Datum/Zeit: | 10.04.2006 03:01:46 |
'Autor: DirkFist
dim shared board(9,9),best_board(9,9)
type list_type
as integer l(9)
end type
randomize timer
function list_shuffle() as list_type
dim as list_type R
for i=0 to 9
R.l(i)=i
next i
for i=1 to 9
k=int(rnd(1)*9+1):j=R.l(i):R.l(i)=R.l(k):R.l(k)=j
k=int(rnd(1)*9+1):j=R.l(i):R.l(i)=R.l(k):R.l(k)=j
next i
return R
end function
function square_solver( xx,yy ) as list_type
' determine unused numbers for square xx,yy
dim as list_type R
for i=1 to 9
R.l(i)=i 'mark number as unused
next i
for i=1 to 9
R.l(board(xx,i))=0 'mark number as used
R.l(board(i,yy))=0 'mark number as used
next i
j=1
for i=1 to 9
if R.l(i)<>0 then
R.l(j)=R.l(i):j+=1
endif
next i
for i=j to 9
R.l(i)=0
next i
R.l(0)=j
return R
end function
sub set_region(xx,yy,a as list_type)
x=xx*3-2:y=yy*3-2
board(x,y)=a.l(1): board(x+1,y)=a.l(2): board(x+2,y)=a.l(3):y+=1
board(x,y)=a.l(4): board(x+1,y)=a.l(5): board(x+2,y)=a.l(6):y+=1
board(x,y)=a.l(7): board(x+1,y)=a.l(8): board(x+2,y)=a.l(9)
end sub
dim as list_type n,z
do
for x=0 to 9
set_region(int(x/3)+1,(x mod 3)+1,z)
next x
set_region(1,1,list_shuffle())
set_region(2,2,list_shuffle())
set_region(3,3,list_shuffle())
do
done=1
mn =10
for y=1 to 9
for x=1 to 9
if board(x,y) =0 then
done =0
n= square_solver(x,y): if n.l(0)<mn then mn=n.l(0):mn_x=x:mn_y=y
end if
next x
next y
if mn<2 then
' no number available for mn_x,mn_y
done =1
elseif mn<10 then
n=square_solver(mn_x,mn_y)
board(mn_x,mn_y) = n.l( int(n.l(0)*rnd(1)+1) )
end if
locate 1,1
for y=1 to 9
for x=1 to 9
if board(x,y)=0 then
?" ";
else
?board(x,y);
end if
if x=3 or x=6 then ?" ";
best_board(x,y)=board(x,y)
next x
print
if y=3 or y=6 then print
next y
loop until done
cls
for y=1 to 9
for x=1 to 9
if board(x,y)=0 then
n=square_solver(x,y)
if n.l(0)=1 then
?" -";
else
?" +";
end if
else
?board(x,y);
end if
if x=3 or x=6 then ?" ";
best_board(x,y)=board(x,y)
next x
if y=3 or y=6 then print
print
next y
do
getmouse x,y,,b
k$=right$(inkey$,1)
x=int(x/2)+1+(x>5)+(x>13)
y+=1+(y>2)+(y>6)
if x<1 then x=1
if y<1 then y=1
if x>9 then x=9
if y>9 then y=9
n=square_solver(x,y)
locate 15,1: ?x,y,b,k$
for i=1 to 9: ?n.l(i);:next i
loop while k$=""
loop until k$=chr$(27)