_TITLE "Sudoku Unique Solution Test" 'B+ started 2019-05-06 '
' Check to see if a Sudoku puzzle has a unique soln or more than 1.
'
' Use your favorite editor and make a text file called - Check Unique Sudoku.txt
' Use any non number symbol for spaces (no commas),
' and type 9 rows of nine numbers and space symbols for the puzzle.
'
' Run this program in same folder and it will append the solution(s) found.
'
SCREEN _NEWIMAGE(840, 600, 32) 'wide enough to display solution in a string * 81 DIM solns$
(1 TO 9 * 4) ' try 36 tests, might have different solution for each SI = 0 'soln index
'get the puzzle to check from file
'PRINT fline$
grid
(col
, row
) = VAL(MID$(fline$
, col
+ 1, 1)) gridCopy(col, row) = grid(col, row) 'make copy
PRINT "Can't find: Check Unique Sudoku.txt file."
'get solution(s)
'resolve2 fills in a puzzle from the top left corner
resolve2 ns 'this sould solve everytime no matter where start N
GOSUB addSoln
'restore grid to pretest conditions grid(c, r) = gridCopy(c, r)
' now try fill in numbers in reverse order
FOR ns
= 9 TO 1 STEP -1 'resolve 2 fills in a puzzle from the top left corner resolve2 ns 'this sould solve everytime no matter where start N
GOSUB addSoln
'restore grid to pretest conditions grid(c, r) = gridCopy(c, r)
'resolve3 fills in Sudoku puzzle from the bottom right corner
resolve3 ns 'work the grid from the other end
'restore grid to pretest conditions
grid(c, r) = gridCopy(c, r)
resolve3 ns 'work the grid from the other end
'restore grid to pretest conditions
grid(c, r) = gridCopy(c, r)
' OK file results
s$ = ""
s$
= s$
+ MID$(solns$
(i
), r
* 9 + c
+ 1, 1) + " "
PRINT:
PRINT "Done checking puzzle, file updated with solutions found."
addSoln:
'string the soln
b$ = ""
'PRINT fline$
'check if we have a copy already
bIsCopy = 0
SI = SI + 1
solns$(SI) = b$
FOR nMod
= startN
TO startN
+ 8 temp = grid(c, r)
grid(c, r) = -n
resolve2 startN
grid(c, r) = temp
FOR nMod
= startN
TO startN
+ 8 temp = grid(c, r)
grid(c, r) = -n
resolve3 startN
grid(c, r) = temp
FUNCTION aok
(a
, c
, r
) 'check to see if a is OK to place at (c, r) aok = 0
IF grid
(c
, r
) = 0 THEN 'check cell empty FOR i
= 0 TO 8 'check row and column cbase
= INT(c
/ 3) * 3: rbase
= INT(r
/ 3) * 3 aok = 1
complete = 1