Sudoku SOLVED by LOGIC

Miscellaneous Forums/General Discussion/Sudoku SOLVED by LOGIC

I've only done sudoku a couple times in the newspaper, but that article is the same way I naturally tried to solve them. (Except I didnt have an eraser both times...)

I just made a sudoku generator in blitz. It only took me a half hour cuz I am super smart!
It fails sometimes, so it just restarts.
SeedRnd MilliSecs()

Type cell
	Field value
	Field cantbe[9]
	Field cants
End Type

Dim grid.cell(9,9)


.top
For y=1 To 9
	For x=1 To 9
		grid(x,y)=New cell
	Next
Next


;draw the lines that make the grid
For y=1 To 9
	For x=1 To 9
		quadx=(x/3*3=x)
		quady=(y/3*3=y)
		Rect x*20,y*20,20-quadx,20-quady,0
	Next
Next

Local c.cell
Local best.cell
For y=1 To 9
	For am=1 To 9
	
		;find the cell on this row that can be the least amount of values
		best=Null
		For x=1 To 9
			c=grid(x,y)
			If c\value=0 Then
				If best=Null Then
					best=c
					cx=x
				ElseIf c\cants>best\cants Then
					best=c
					cx=x
				EndIf
			EndIf
		Next
		If best=Null Then Stop
		c=best
		
		;test if the cell can't be anything
		If c\cants=9 Then
			Cls
			Delete Each cell
			Goto top
		EndIf
		
		;give the cell a random number that it can be
		Repeat
			num=Rand(1,9)
			If c\cantbe[num]=0 Then Exit
			Delay 9
		Forever
		c\value=num
		Text cx*20,y*20,c\value
		
		;all in this row now cant be this value
		For x2=1 To 9
			If grid(x2,y)\cantbe[num]=0 Then
				grid(x2,y)\cantbe[num]=1
				grid(x2,y)\cants=grid(x2,y)\cants+1
			EndIf
		Next
		
		;all in this column now cant be this value
		For y2=y To 9
			If grid(cx,y2)\cantbe[num]=0 Then
				grid(cx,y2)\cantbe[num]=1
				grid(cx,y2)\cants=grid(cx,y2)\cants+1
			EndIf
		Next
		
		;all in this quadrant now cant be this value
		quadx=(cx-1)/3*3+1
		quady=(y-1)/3*3+1
		For y2=quady To quady+2
			For x2=quadx To quadx+2
				If grid(x2,y2)\cantbe[num]=0 Then
					grid(x2,y2)\cantbe[num]=1
					grid(x2,y2)\cants=grid(x2,y2)\cants+1
				EndIf
			Next
		Next
	Next
Next

;For y=1 To 9
;	For x=1 To 9
;		Text x*20,y*20,grid(x,y)\value
;	Next
;Next
Text 1,1,"done!"


WaitKey
End