hey!
(like most of my snippets this one uses the a2plugin but its not necessary )
i made this litle piece of code just for fun, it is a basic sudokusolver, not that good so it wont solve anything very advanced...
it will solve the "haastava" sudokus from this site
http://alypaa.com/sudoku
and any harder ones it will fill partly.
there might be an easier way to do this but this is the way i didi it
use the numberkeys to select numbers and then place them with the mouse(click)
press enter(return) to unselect numbers
when you are ready, press space to start solving
if you press space at the start it will solve simultaneously as you fill in numbers, this can lead to false results
i think the fun thing with this one is that it only uses logic, so no iteration
Rem Project: sudoquick
Rem Created: Wednesday, January 04, 2012
Rem ***** Main Source File *****
sync on : sync rate 60 : hide mouse
type UDTsudobox
num$
locked : value
square
xparent : yparent
gen
endtype
dim s(9,9) as UDTsudobox
c1 as dword : c2 as dword : c3 as dword : c4 as dword
c1 = rgb(255,25,5) : c2 = rgb(255,255,255) :c3 = rgb(15,20,30) : c4 = rgb(0,30,0)
a2setblendmode 2,2,1
for i = 1 to 9 : for j = 1 to 9
s(i,j).num$ = "XXXXXXXXX" : s(i,j).square = -3+int((i-1)/3.0+1)+3*int((j-1)/3.0+1)
s(i,j).xparent = (int((i-1)/3.0)+1)*3-2 : s(i,j).yparent = (int((j-1)/3.0)+1)*3-2
next : next
dim f(9)
do
xnum = getboxx(100,50) : ynum = getboxy(100,50)
if xnum > 0 and xnum < 10 and ynum > 0 and ynum < 10
bx = s(xnum,ynum).xparent : by = s(xnum,ynum).yparent
for k = 1 to 9 : f(k) = 0 : next
for k = bx to bx+2 : for n = by to by+2
for o = 1 to 9
if getfreeval(s(k,n).num$,o) = 1 then inc f(o),1
next
NEXT : next
for i = 1 to 9 : print f(i) : next
endif
if scancode() > 1 and scancode()<= 10
selval = scancode()-1
endif
if returnkey() then selval = 0
if selval > 0 then text mousex(),mousey(),str$(selval)
for i = 0 to 9 : a2line i*50+100,100,i*50+100,550,c1 : a2line 100,i*50+100,550,i*50+100,c1 : next
for i = 0 to 9 step 3 : a2line i*50+100,100,i*50+100,550,c2 : a2line 100,i*50+100,550,i*50+100,c2 : next
for i = 1 to 9 : for j = 1 to 9
if s(i,j).locked = 1
text i*50+50,j*50+50,str$(s(i,j).value)
a2fillbox i*50+50,j*50+50,i*50+100,j*50+100,c3
if s(i,j).gen = 1
a2fillbox i*50+50,j*50+50,i*50+100,j*50+100,c3
endif
endif
if xnum = i and ynum = j
a2fillbox i*50+50,j*50+50,i*50+100,j*50+100,c3
endif
if getfreeval(s(i,j).num$,selval) = 1
a2fillbox i*50+50,j*50+50,i*50+100,j*50+100,c4
if mouseclick() = 1 and xnum = i and ynum = j
s(i,j).locked = 1 : s(i,j).value = selval
for k = 1 to 9
if s(i,k).locked = 0 then s(i,k).num$ = editfreeval(s(i,k).num$,selval)
if s(k,j).locked = 0 then s(k,j).num$ = editfreeval(s(k,j).num$,selval)
next
for k = s(i,j).xparent to s(i,j).xparent+2 : for n = s(i,j).yparent to s(i,j).yparent+2
if s(k,n).locked = 0 then s(k,n).num$ = editfreeval(s(k,n).num$,selval)
NEXT : next
s(i,j).num$ = ""
endif
ENDIF
next : next
if solve = 1
for i = 1 to 9
for j = 1 to 9 : f(j) = 0 : next
for j = 1 to 9 : for k = 1 to 9
if getfreeval(s(i,j).num$,k) = 1 then inc f(k),1
NEXT : next
for k = 1 to 9
if f(k) = 1
for j = 1 to 9
if getfreeval(s(i,j).num$,k) = 1
s(i,j).locked = 1 : s(i,j).value = k : s(i,j).gen = 1
for o = 1 to 9
if s(i,o).locked = 0 then s(i,o).num$ = editfreeval(s(i,o).num$,k)
if s(o,j).locked = 0 then s(o,j).num$ = editfreeval(s(o,j).num$,k)
next
for o = s(i,j).xparent to s(i,j).xparent+2 : for n = s(i,j).yparent to s(i,j).yparent+2
if s(o,n).locked = 0 then s(o,n).num$ = editfreeval(s(o,n).num$,k)
NEXT : next
s(i,j).num$ = ""
endif
next
ENDIF
next
next
for j = 1 to 9
for i = 1 to 9 : f(i) = 0 : next
for i = 1 to 9 : for k = 1 to 9
if getfreeval(s(i,j).num$,k) = 1 then inc f(k),1
NEXT : next
for k = 1 to 9
if f(k) = 1
for i = 1 to 9
if getfreeval(s(i,j).num$,k) = 1
s(i,j).locked = 1 : s(i,j).value = k : s(i,j).gen = 1
for o = 1 to 9
if s(i,o).locked = 0 then s(i,o).num$ = editfreeval(s(i,o).num$,k)
if s(o,j).locked = 0 then s(o,j).num$ = editfreeval(s(o,j).num$,k)
next
for o = s(i,j).xparent to s(i,j).xparent+2 : for n = s(i,j).yparent to s(i,j).yparent+2
if s(o,n).locked = 0 then s(o,n).num$ = editfreeval(s(o,n).num$,k)
NEXT : next
s(i,j).num$ = ""
endif
next
ENDIF
next
next
for i = 1 to 3 : for j = 1 to 3
for k = 1 to 9 : f(k) = 0 : next
for k = i*3-2 to i*3 : for n = j*3-2 to j*3
for o = 1 to 9
if getfreeval(s(k,n).num$,o) = 1 then inc f(o),1
next
NEXT : next
for o = 1 to 9
if f(o) = 1
for k = i*3-2 to i*3 : for n = j*3-2 to j*3
if getfreeval(s(k,n).num$,o) = 1
s(k,n).locked = 1 : s(k,n).value = o : s(k,n).gen = 1
for p = 1 to 9
if s(k,p).locked = 0 then s(k,p).num$ = editfreeval(s(k,p).num$,o)
if s(p,n).locked = 0 then s(p,n).num$ = editfreeval(s(p,n).num$,o)
next
for p = s(k,n).xparent to s(k,n).xparent+2 : for l = s(k,n).yparent to s(k,n).yparent+2
if s(p,l).locked = 0 then s(p,l).num$ = editfreeval(s(p,l).num$,o)
NEXT : next
s(k,n).num$ = ""
endif
next : next
ENDIF
next
next : next
for i = 1 to 3 : for j = 1 to 3
for k = 1 to 9 : f(k) = 0 : next
for k = i*3-2 to i*3 : for n = j*3-2 to j*3
for o = 1 to 9
if getfreeval(s(k,n).num$,o) = 1 then inc f(o),1
next
NEXT : next
for o = 1 to 9
if f(o) = 2
s1 = 0
for k = i*3-2 to i*3 : for n = j*3-2 to j*3
if getfreeval(s(k,n).num$,o) = 1 and s1 = 1 then x2 = k : y2 = n : s1 = 2
if getfreeval(s(k,n).num$,o) = 1 and s1 = 0 then x1 = k : y1 = n : s1 = 1
if s1 = 2
if y1 = y2
for m = 1 to 9
if s(m,y2).locked = 0 and m <> x2 and m <> x1 then s(m,y2).num$ = editfreeval(s(m,y2).num$,o)
next
endif
if x1 = x2
for m = 1 to 9
if s(x2,m).locked = 0 and m <> y2 and m <> y1 then s(x2,m).num$ = editfreeval(s(x2,m).num$,o)
next
endif
s1 = 0
endif
next : next
ENDIF
next
next : next
for i = 1 to 3 : for j = 1 to 3
for k = 1 to 9 : f(k) = 0 : next
for k = i*3-2 to i*3 : for n = j*3-2 to j*3
for o = 1 to 9
if getfreeval(s(k,n).num$,o) = 1 then inc f(o),1
next
NEXT : next
for o = 1 to 9
if f(o) = 3
s1 = 0
for k = i*3-2 to i*3 : for n = j*3-2 to j*3
if getfreeval(s(k,n).num$,o) = 1 and s1 = 2 then x3 = k : y3 = n : s1 = 3
if getfreeval(s(k,n).num$,o) = 1 and s1 = 1 then x2 = k : y2 = n : s1 = 2
if getfreeval(s(k,n).num$,o) = 1 and s1 = 0 then x1 = k : y1 = n : s1 = 1
if s1 = 3
if y1 = y2 and y1 = y3
for m = 1 to 9
if s(m,y2).locked = 0 and m <> x2 and m <> x1 and m <> x3 then s(m,y2).num$ = editfreeval(s(m,y2).num$,o)
next
endif
if x1 = x2 and x1 = x3
for m = 1 to 9
if s(x2,m).locked = 0 and m <> y2 and m <> y1 and m <> y3 then s(x2,m).num$ = editfreeval(s(x2,m).num$,o)
next
endif
s1 = 0
endif
next : next
ENDIF
next
next : next
endif
if spacekey() then solve = 1
sync
cls
LOOP
function editfreeval(s$,v)
s$ = fast left$(s$,v-1)+"O"+fast right$(s$,9-v)
ENDFUNCTION s$
function getfreeval(s$,v)
if mid$(s$,v) = "X" then y = 1
ENDFUNCTION y
function getboxx(x#,w#)
xnum = int((mousex()-x#)/w#)+1
ENDFUNCTION xnum
function getboxy(y#,h#)
ynum = int((mousey()-y#)/h#)+1
endfunction ynum