Solving sudoku's in BASIC
Great !
I was thinking to this problem, but I haven't yet had the right idea to solve the game....
Good work
By Google
ah well, backtracking isnt the best solution to the problem. but it will solve all correct sudoku's if you give it enough time to run 
if you have a decent language at your disposal i advice you to try the famous Donald Knuth's Dancing LInks algorithm that will solve all NP-Complete problems including sudoku.
I haven't finished my DLX implementation on C# yet, but it'll come..
I tried it out, and it works perfectly 
hahaha
how nice of you to test it and post a screenshot of it Gilneas2 
EDIT:
oh, i see it now... i remember the first 3 digits and its the sudoku from the data lines. of course it works because i tested that one 
but don't worry, i didnt test more sudoku's but my C# program solves them all, and so will this one... it has to!
Completely off-topic:
Gilneas2, which window manager/theme are you using ?!
Blizzard's official World of Warcraft theme for some Stardock theme manager (although I have never played WoW...)
Gilneas2, Are you planning to do a sudoku generator too? Would be nice to see a Sudoku game on MSX. I love sudoku (Although I prefer to print it on paper before solving it, it would be nice to see an MSX version).
Gilneas2, Are you planning to do a sudoku generator too? Would be nice to see a Sudoku game on MSX. I love sudoku (Although I prefer to print it on paper before solving it, it would be nice to see an MSX version).
please correct me if im wrong.. maybe you mistake Glineas2 for me 
anyway, IF the question is for me:
i have nothing in my planning, and an MSX is far too slow to generate HUGE ammounts of sudoku's since you need a VERY fast solver for it.. there is, however, a way to quickly come up with new sudoku's...
(FYI: the simplest/quickest way to come up with new sudoku's is permutating others, if i generate a huge database with my C# program, but it isnt able to generate YET)
anyway, i HAVE to do do it in asm them and not in basic.. at least, it is a bit faster than basic.
here is the code for the newer version: this one displays the backtracking steps visually.. so give it a try! it looks better, especially with openmsx at 500% speed !
10 DATA 0,2,0,0,0,0,0,8,0 20 DATA 0,0,8,0,9,0,0,0,7 30 DATA 3,0,0,0,0,1,0,2,0 40 DATA 2,0,5,1,7,0,0,0,4 50 DATA 0,0,0,8,0,2,0,0,0 60 DATA 9,0,0,0,5,6,1,0,2 70 DATA 0,9,0,7,0,0,0,0,5 80 DATA 1,0,0,0,6,0,7,0,0 90 DATA 0,4,0,0,0,0,0,3,0 100 ' Create 2D array from data, count empty cells and display 110 CLS 120 DIMGP(8, 8) ' Game pattern 130 X=0:Y=0:EC=0 ' Number of empty cells 140 FORI=0TO80:READA:GP(X,Y)=A 150 IFA=0THENEC=EC+1 160 LOCATE28+(X*2),2+(Y*2):PRINTCHR$(A+48) 170 X=X+1:IFX=9THENX=0:Y=Y+1 180 NEXT 190 ' Draw board lines 200 FORY=2TO18:LOCATE33,Y:PRINT"G":LOCATE39,Y:PRINT"G":NEXT 210 FORX=28TO44:LOCATEX,7:PRINT"G":LOCATEX,13:PRINT"G":NEXT 220 ' Create empty cell lists 230 DIMEX(EC-1) ' Empty cell X positions 240 DIMEY(EC-1) ' Empty cell Y positions 250 PO=0:FORY=0TO8:FORX=0TO8 260 IFGP(X,Y)=0THENEX(PO)=X:EY(PO)=Y:PO=PO+1 270 NEXTX,Y 280 ' Initialize solving loop 290 EP=0 ' Empty cell pointer 300 MP=0 ' Previous moves pointer 310 CI=0 ' Candidate index pointer 320 DIMCL(9) ' Candidate array 330 DIMPI(EC) ' Previous move candidate index pointer array 340 ' Start solving loop 350 IFEP>EC-1THENEND 360 X=EX(EP):Y=EY(EP) ' Get X,Y locations for current empty cell 370 GOSUB550 ' Obtain candidates for x,y in cl array 380 ' Check if there are candidates 390 IF CO = 0 THEN GOTO 490 400 ' Check if we haven't tried all candidates 410 IF CO < CI + 1 THEN GOTO 490 420 ' Ah, let's put the number on the board shall we 430 GP(X,Y) = CL(CI) 440 ' Add this move to the previous moves lists and advance to next cell 450 PI(MP)=CI:MP=MP+1:CI=0:EP=EP+1 460 ' Display the placed cell 470 LOCATE28+(X*2),2+(Y*2):PRINTCHR$(GP(X,Y)+48):GOTO350 480 ' Undo and revert to previous cell 490 EP=EP-1 ' Set emptyCell pointer to the previous one 500 MP=MP-1 ' Set the moves pointer to the previous one 510 CI=PI(MP)+1 ' Set cIndex to the one of the previous move and increment 520 GP(EX(EP),EY(EP))=0 ' Reset the previous value on the board 530 LOCATE28+(EX(EP)*2),2+(EY(EP)*2):PRINTCHR$(48):GOTO350 540 ' Get all candidates for the specified cell 550 FORI=0TO9:CL(I)=I:NEXTI 560 ' Apply region constraint 570 XX=(X\3)*3:YY=(Y\3)*3 580 FORTY=YYTOYY+2:FORTX=XXTOXX+2 590 IFGP(TX,TY)>0THENCL(GP(TX,TY))=0 600 NEXTTX,TY 610 ' Apply row constraint 620 FORXX=0TO8 630 IFGP(XX,Y)>0THENCL(GP(XX,Y))=0 640 NEXTXX 650 ' Apply column constraint 660 FORYY=0TO8 670 IFGP(X,YY)>0THENCL(GP(X,YY))=0 680 NEXTYY 690 ' Sequence the cl array and count instances 700 K=0:CO=0 710 FORI=1TO9 720 IFCL(I)>0THENA=CL(I):CL(I)=0:CL(K)=A:K=K+1:CO=CO+1 730 NEXTI:RETURN
Glineas2: please update your openMSX!
how would i ever recurse in basic without using assembly?
1. Predict how deep your algorithm will recurse (81 times would be a pretty decent upper limit in the case of sudoku :-))
2. put all parameters to the recursive algorithm in an array dimensioned to the result of step 1
(like DIM P(81))
3. create a "stack pointer" variable that will index the array
(like: SP=0)
4. create resursive calls like this:
P[SP+1] = 3 * P[SP] + 1 ' or sumthn
SP=SP+1: GOSUB 1000: SP=SP-1 ' the recursive routine starts at 1000
5. Be sure to restore variables used in the recursive routine get set back to their old values after the recursive call
6. Watch your recursion do its do...
Ob-FIB
5 DIM R[70] 10 INPUT"FIB of"; N: If N > 69 THEN PRINT "Oh, please, get real...": GOTO 10 20 R=0: SP=0: GOSUB 100 30 PRINT R 40 GOTO 10 100 ' Calc FIB(N) = FIB(N-1)+FIB(N-2), for N >= 2; FIB(N) = N for N=0,1 110 IF N < 2 THEN R = N: RETURN ' The easy case 120 SP=SP+1: N=N-1: GOSUB 100 ' Do the recursive call : FIB(N-1) 130 N=N+1: SP=SP-1: R[SP] = R ' Restore old values and save result 140 SP=SP+1: N=N-2: GOSUB 100 ' Do recursive call FIB(N-2) 150 N=N+2: SP=SP-1: R[SP] = R[SP] + R ' Restore old values and calc result 160 R = R[SP] ' set result 170 RETURN
The above example is of course for educational purposes only - it can be modified, optimized etc etc. In the end, who needs recursion to calculate Fibonacci?

By DarQ
Paragon (1038)
15-12-2005, 13:28