# For TheBob

June 23 2005 at 1:41 PM
Forum Owner

DECLARE FUNCTION Generate\$ (Old\$)
Old\$ = "-15-6-4-------92-----3---59-2----6-43---"
Old\$ = Old\$ + "----15-7----3-15---2-----86-------4-1-39-"
IF LEN(Old\$) <> 81 THEN STOP: 'bug
New\$=Old\$
open "demo.txt" for output as #1
do
k = 0
FOR i = 1 TO 9: FOR j = 1 TO 9
k = k + 1: PRINT #1, MID\$(New\$, k, 1);
NEXT j: PRINT #1, "": NEXT i: PRINT #1, ""
New\$ = Generate(Old\$)
loop while inkey\$=""
close
SYSTEM

FUNCTION Generate\$ (Old\$)
DIM S(9) AS STRING: GOSUB LoadS
DIM m(9, 9) AS STRING * 1: ' Matrix to be returned
DIM t(9, 9) AS STRING * 1: ' Temporary matrix
FOR i = 1 TO 9: FOR j = 1 TO 9
k = k + 1
m(i, j) = MID\$(Old\$, k, 1)
NEXT j: NEXT i
FOR Doo = 1 TO 3
GOSUB Scramble1: 'Scramble the numbers
Mode\$ = "row": GOSUB Scramble2: 'Scramble rows within 3x3 rule
Mode\$ = "col": GOSUB Scramble2: 'Scramble cols within 3x3 rule
Mode\$ = "row": GOSUB Scramble3: 'Scramble Superrows
Mode\$ = "col": GOSUB Scramble3: 'Scramble cols within 3x3 rule
NEXT Doo
GOSUB Rotate
w\$ = ""
FOR i = 1 TO 9: FOR j = 1 TO 9
w\$ = w\$ + m(i, j)
NEXT j: NEXT i
Generate\$ = w\$
EXIT FUNCTION

Rotate:
FOR i = 1 TO 9: FOR j = 1 TO 9: t(i, j) = m(i, j): NEXT j: NEXT i
p = 1: q = 1: r=RND
SELECT CASE RND
CASE IS < .25: GOSUB Rotate1
CASE IS < .5: GOSUB Rotate2
CASE IS < .75: GOSUB Rotate3
CASE ELSE: GOSUB Rotate4
END SELECT
RETURN

Rotate1:
FOR i = 1 TO 9: FOR j = 1 TO 9
if r>.5 then m(i, j) = t(q, p) else m(i,j)=t(p,q)
q = q + 1: IF q > 9 THEN p = p + 1: q = 1
NEXT j: NEXT i
RETURN
Rotate2:
FOR i = 9 TO 1 STEP -1: FOR j = 1 TO 9
if r>.5 then m(i, j) = t(q, p) else m(i,j)=t(p,q)
q = q + 1: IF q > 9 THEN p = p + 1: q = 1
NEXT j: NEXT i
RETURN
Rotate3:
FOR i = 1 TO 9: FOR j = 9 TO 1 STEP -1
if r>.5 then m(i, j) = t(q, p) else m(i,j)=t(p,q)
q = q + 1: IF q > 9 THEN p = p + 1: q = 1
NEXT j: NEXT i
RETURN
Rotate4:
FOR i = 9 TO 1 STEP -1: FOR j = 9 TO 1 STEP -1
if r>.5 then m(i, j) = t(q, p) else m(i,j)=t(p,q)
q = q + 1: IF q > 9 THEN p = p + 1: q = 1
NEXT j: NEXT i
RETURN

Scramble3:
GOSUB GetRnds
r1 = (r1 - 1) * 3: r2 = (r2 - 1) * 3
FOR i = 1 TO 3
FOR j = 1 TO 9
IF Mode\$ = "row" THEN
SWAP m(i + r1, j), m(i + r2, j)
ELSE
SWAP m(j, i + r1), m(j, i + r2)
END IF
NEXT j
NEXT i
RETURN

Scramble1:
FOR i = 1 TO 9: FOR j = 1 TO 9
v% = VAL(m(i, j))
IF v% > 0 THEN m(i, j) = RIGHT\$(STR\$(VAL(S(v%))), 1)
NEXT j: NEXT i
RETURN

Scramble2:
FOR i = 0 TO 6 STEP 3
GOSUB GetRnds
FOR j = 1 TO 9
IF Mode\$ = "row" THEN
SWAP m(i + r1, j), m(i + r2, j)
ELSE
SWAP m(j, i + r1), m(j, i + r2)
END IF
NEXT j
NEXT i
RETURN

GetRnds:
r1 = 1 + INT(RND * 3)
DO: r2 = 1 + INT(RND * 3): LOOP WHILE r1 = r2
RETURN

FOR i = 1 TO 9: S(i) = RIGHT\$(STR\$(i), 1): NEXT i
RANDOMIZE TIMER
DO
FOR i = 1 TO 9
r = INT(1 + (RND * 9))
SWAP S(i), S(r)
NEXT i
cnt = 0
FOR i = 1 TO 9
IF VAL(S(i)) = i THEN cnt = cnt + 1
NEXT i
LOOP WHILE cnt > 2
RETURN

END FUNCTION

 Respond to this message
 Current Topic - For TheBob