## basic program to solve puzzels

Programs which generate, solve, and analyze Sudoku puzzles

### basic program to solve puzzels

i just wrote this simple basic program to solve puzzels
run it under Liberty basic:

copy and paste the program list below.

' PROGRAM WRITTEN BY D. DIMOND / JAN / 06
' PLS DO NOT REMOVE THIS HEADER
' AND DO DROP ME AN E-MAIL
'
' THIS PROGRAM WILL SOLVE ALL NUMBERS IN A SUDOKO PUZZLE THAT ARE POSSIBLE WITHOUT
' GUESSING
' LOAD PUZZLE IN DATA STRING AT END OF PROGRAM
' ADD TO THIS PROGRAM IF YOU WISH
' ENJOY!!
DIM A(9,9) 'DIM 9,9 ARRAY -- THE MAIN GRID
DIM B(81,9)'DIM 81,9 GRID - 81 NUMBERS IN GRID -- ALL COULD HAVE NUMBES 1 TO 9 IN EACH SPOT
REM A(1,1)=15
FOR X = 1 TO 9 '===
FOR Y = 1 TO 9 '
A(X,Y)=NUM1 '
NEXT Y '
NEXT X '===
FOR X= 1 TO 9 '===
FOR Y= 1 TO 9 '
PRINT A(Y,X);"|"; 'PRINT OUT ARRAY FOR DATA CHECK AND HOLD
NEXT Y '
PRINT '
NEXT X '
INPUT A\$ '===
FOR X=1 TO 81 '===
FOR Y = 1 TO 9 ' FILL NUMBER ARRAY WITH ALL NUMBER CHOICES
B(X,Y)=Y ' ALL NUMBERS COULD BE 1 TO 9
NEXT Y '
NEXT X '===
FOR X=1 TO 9 '===
FOR Y=1 TO 9 '
J=(X-1)*9+Y 'CHECK GRID ARRAY KNOWN NUMBERS
IF A(X,Y)<>0 THEN 'CLEAR NUMBER ARRAY OF OTHER NUMBER CHOICES OTHER THEN
C=A(X,Y) 'NUMBER THAT IS FOUND IN GRID ARRAY
FOR D=1 TO 9 '
IF D<>C THEN '
B(J,D)=0 '
END IF '
NEXT D '
END IF '
NEXT Y '
NEXT X '===
FOR SS=1 TO 25 ' RUN PROGRAM 25 TIMES
FOR X=1 TO 9 '===
FOR Y=1 TO 9 '
REM PRINT A(X,Y) '
IF A(X,Y) = 0 THEN '
FOR Z = 1 TO 9 '
IF A(X,Z)>0 THEN ' WHEN NUMBER IS FOUND IN GRID ARRAY
C=A(X,Z) ' CLEAR THAT CHOICE FROM ALL OTHERS CHOICES
J=(X-1)*9+Y ' IN NUMBER ARRAY FOR THAT ROW AND COLUNM
B(J,C)=0 '
END IF '
NEXT Z '
FOR Z= 1 TO 9 '
IF A(Z,Y) >0 THEN '
C=A(Z,Y) '
J=(X-1)*9+Y '
B(J,C)=0 '
REM PRINT X,Y,A(Z,X),C,J '
END IF '
NEXT Z '
END IF '
NEXT Y '
NEXT X '===
FOR X= 1 TO 9 ' ===
FOR Y= 1 TO 9 '
J=(X-1)*9+Y '
E=0 '
FOR T=1 TO 9 ' LOOK EACH BLANK IN GRID ARRAY
REM PRINT "B(J,T)",J,T,B(J,T) ' AND SEE IF ANY OTHER NUMBERS IN THAT COLUMN
IF B(J,T)>0 THEN ' ARE A OPTION.
E=E+1 '
REM PRINT X,Y,J,A(X,Y),B(J,T),E ' IF NO OTHER OPTIONS IN THAT COLUMN
END IF ' THEN CLEAR OTHER CHOICES FROM
NEXT T ' NUMBER ARRAY AND WRITE NUMBER
REM PRINT "E";E ' TO GRID ARRAY
IF E=1 THEN '
FOR S=1 TO 9 '
IF B(J,S)>0 THEN '
A(X,Y)=B(J,S) '
REM PRINT "ARRAY REPLACED";A(X,Y);B(J,S) '
END IF '
NEXT S '
END IF '
NEXT Y '
NEXT X '===
REM PRINT "CHECK" '===
FOR S=1 TO 81 '
REM PRINT S;" "; ' PRINT OUT CURRENT NUMBER CHOICES FROM NUMBER ARRAY
FOR T = 1 TO 9 ' AND VIEW CURRENT GRID
REM PRINT B(S,T); '
NEXT T '
REM PRINT '
NEXT S '
FOR X = 1 TO 9 '
FOR Y = 1 TO 9 '
REM PRINT A(X,Y); '
IF Y= 9 THEN '
REM PRINT '
END IF '
NEXT Y '
NEXT X '===
XX=1
XXX=3
YY=1
YYY=3
FOR H= 1 TO 9 '==== '
IF H=2 THEN '
YY=4 '
YYY=6 '
END IF '
IF H=3 THEN '
YY=7 '
YYY=9 '
END IF ' LOOK AT EACH GROUP
IF H=4 THEN ' OF NUMBERS IN ALL 9 BOXES
XX=4 '
XXX=6 ' CLEAR OUT CHOICES IN NUMBER ARRAY
YY=1 ' IF NUMBER IS FOUND IN BOX
YYY=3
END IF
IF H=5 THEN
YY=4
YYY=6
END IF
IF H=6 THEN
YY=7
YYY=9
END IF
IF H=7 THEN
XX=7
XXX=9
YY=1
YYY=3
END IF
IF H=8 THEN
YY=4
YYY=6
END IF
IF H=9 THEN
YY=7
YYY=9
END IF
REM PRINT H
FOR X=XX TO XXX
FOR Y=YY TO YYY
IF A(X,Y) = 0 THEN
FOR P = XX TO XXX
FOR Q = YY TO YYY
IF A(P,Q)>0 THEN
C=A(P,Q)
J=(X-1)*9+Y
REM PRINT "B(J,C)";B(J,C);"CLEARED";J;",";C
B(J,C)=0
END IF
NEXT Q
NEXT P
END IF
NEXT Y
NEXT X
NEXT H '===
FOR Y= 1 TO 9 '===
FOR X= 1 TO 9
J=(X-1)*9+Y
FOR Z=1 TO 9
IF B(J,Z)<> 0 THEN
REM PRINT "<> NOT 0 FOUND=";B(J,Z);"POSITION=";J,Z
NUM=Z
IF X=1 THEN
X1=1
X2=9
END IF
IF X=2 THEN
X1=10
X2=18
END IF ' CHECK IF NUMBER ARRAY HAS ANY
IF X=3 THEN ' OTHER CHOICES IN EACH COLUMN
X1= 19 ' SLOPPY PGM'N HERE BUT IT WORKS
X2= 27
END IF
IF X=4 THEN
X1 = 28
X2 = 36
END IF
IF X=5 THEN
X1= 37
X2= 45
END IF
IF X=6 THEN
X1= 46
X2 = 54
END IF
IF X=7 THEN
X1= 55
X2= 63
END IF
IF X=8 THEN
X1= 64
X2= 72
END IF
IF X=9 THEN
X1= 73
X2=81
END IF
FOR XX=X1 TO X2
SUM1=SUM1+B(XX,Z)
REM PRINT SUM1=";SUM1;"B(";XX;",";Z;")=";B(XX,Z)
NEXT XX
IF SUM1=Z THEN
FOR V= 1 TO 9
B(J,V)=0
REM PRINT "CLEARED B(";J;",";V;"=)";B(J,V);" X,Y=";X;",";Y
NEXT V
FOR XX=1 TO 9
JJ=(XX-1)*9+Y
REM PRINT "JJ=";JJ,Z
B(JJ,Z)=0
NEXT XX
B(J,Z)=Z
REM PRINT "B(";J;",";Z;")=";B(J,Z);"VALUE SET, XX=";XX;" Y=";Y
END IF
SUM1=0
END IF
NEXT Z
NEXT X
NEXT Y
FOR X= 1 TO 9
FOR Y= 1 TO 9
J=(Y-1)*9+X
FOR Z=1 TO 9
IF B(J,Z)<> 0 THEN
REM PRINT "<> NOT 0 FOUND=";B(J,Z);"POSITION=";J,Z
NUM=Z
IF X=1 THEN
X1=1
X2=73
END IF
IF X=2 THEN
X1=2
X2=74
END IF
IF X=3 THEN
X1= 3
X2= 75
END IF 'CHECK IF NUMBER ARRAY HAS OTHER CHOICES
IF X=4 THEN 'IN EACH ROW
X1 = 4
X2 = 76
END IF
IF X=5 THEN
X1=5
X2= 77
END IF
IF X=6 THEN
X1= 6
X2 = 78
END IF
IF X=7 THEN
X1= 7
X2= 79
END IF
IF X=8 THEN
X1= 8
X2=80
END IF
IF X=9 THEN
X1=9
X2=81
END IF
FOR XX=X1 TO X2 STEP 9
SUM1=SUM1+B(XX,Z)
REM PRINT SUM1=";SUM1;"B(";XX;",";Z;")=";B(XX,Z)
NEXT XX
IF SUM1=Z THEN
FOR V= 1 TO 9
B(J,V)=0
REM PRINT "CLEARED B(";J;",";V;"=)";B(J,V);" X,Y=";X;",";Y
NEXT V
FOR XX=1 TO 9
JJ=(Y-1)*9+X
REM PRINT "JJ=";JJ,Z,XX,Y
B(JJ,Z)=0
NEXT XX
B(J,Z)=Z
REM PRINT "B(";J;",";Z;")=";B(J,Z);"VALUE SET, XX=";XX;" Y=";Y
END IF
SUM1=0
END IF
NEXT Z
NEXT Y
NEXT X
FOR S=1 TO 81
FOR T = 1 TO 9
GRIDS=GRIDS+B(S,T) 'SUM ALL NUMBERS SO WE CAN SEE WHEN GRID STOPPED CHANGING
NEXT T
NEXT S
PRINT GRIDS
GRIDS=0
NEXT SS
FOR S=1 TO 81
PRINT S;" ";
FOR T = 1 TO 9
PRINT B(S,T);
NEXT T
PRINT
NEXT S
FOR X=1 TO 9
FOR Y=1 TO 9
PRINT A(Y,X);"|";
NEXT Y
PRINT
NEXT X

' ENTER GRID IN DATA FIELD -- ZEROS FOR BLANKS

DATA 0,0,0,6,0,0,4,0,5,0,0,0,0,0,5,0,6,0,4,0,0,8,0,1,0,7,2,0,0,7,0,0,0,0,3,0,9,6,0,0,1,0,0,5,7,0,3,0,0,0,0,1,0,0,5,2,0,1,0,6,0,0,9,0,7,0,2,0,0,0,0,0,8,0,3,0,0,4,0,0,0,0
XCRMAN

Posts: 1
Joined: 22 January 2006