Sodoku Solver (Darkbasic Pro Source Code Only)
Introduction
This is a small selection of code that will solve a sodoku grid. Their is no GUI (graphical user interface), the sodoku you wish to solve has to be entered into the code. A GUI could be easily made using BLUEGUI or a few sprite commands
Heres the 20 line code:
dim board$(9,9) : for x = 1 to 9 : for y = 1 to 9 : board$(x,y)="123456789" : next y : next x
`add the values of the sodoku we want to solve
`function should be called using setvalue(value,x,y)
setvalue(9,2,1) : setvalue(7,8,1) : setvalue(2,3,2) : setvalue(3,4,2) : setvalue(8,6,2) : setvalue(5,7,2)
setvalue(4,2,3) : setvalue(5,3,3) : setvalue(3,7,3) : setvalue(2,8,3) : setvalue(2,2,4) : setvalue(6,4,4) : setvalue(8,5,4) : setvalue(5,6,4) : setvalue(3,8,4)
setvalue(5,1,5) : setvalue(3,3,5) : setvalue(4,7,5) : setvalue(2,9,5) : setvalue(8,2,6) : setvalue(4,4,6) : setvalue(3,5,6) : setvalue(2,6,6) :setvalue(6,8,6)
setvalue(3,2,7) : setvalue(6,3,7) : setvalue(7,7,7) : setvalue(4,8,7) : setvalue(4,3,8) : setvalue(2,4,8) : setvalue(1,6,8) : setvalue(8,7,8) : setvalue(1,2,9) : setvalue(5,8,9)
FOR S = 1 TO 9 : for x = 1 to 9 step 3 : for y = 1 to 9 step 3 : for t = x to x + 2 : for u = y to y + 2
if len(board$(t,u))= 1 : markXY(val(board$(t,u)),t,u) : markBLOCK(val(board$(t,u)),x,Y) : ENDIF : next u : next t : next y : next x : NEXT S
for x = 1 to 9 : for y = 1 to 9 : text x*30,y*30,board$(x,y) : next y : next x : wait key
function markBLOCK(number,x,y) : for t = x to x + 2 : for u = y to y + 2 : IF len(board$(t,u)) > 1 THEN StripNumber(Number,T,U)
next u : next t
ENDFUNCTION
function markXY(number,x,y) : for t = 1 to 9 : if t <> y then StripNumber(Number,x,t) : if t <> x then StripNumber(Number,t,y)
next t
endfunction
function SetValue(Number,x,y) : board$(x,y) = str$(number)
Endfunction
Function StripNumber(Number,x,y) : tempstring$ = "" : for t = 1 to len(board$(x,y)) : if mid$(board$(x,y),t) <> str$(number) then tempstring$ = tempstring$ + mid$(board$(x,y),t)
next t : board$(x,y) = tempstring$
endfunction
If you wish you can also download the source in nice readable commented style from:
http://www.atnasonline.co.uk/index.php?page=sodokusolve
ThanXs - SAnTA