I am writing a sudoku program from scratch in vb.net (which i just switched to).
I am having a fit of a time getting the grid to generate. Here's the code, which i don't even have all the number checks in place, and it still doesn't work.
I highlighted in red the trouble spot. This code is supposed to generate a 9x9 grid of numbers. The check to see if horizontal is unique works fine. Every row has 1-9 inclusive in a random order.Code:Randomize() Dim num As Integer, x As Integer, y As Integer, z As Integer, Unique As Boolean ReDim SudokuGrid(9, 9) 'erases the current numbers For y = 1 To 9 For x = 1 To 9 Do Unique = True num = Int(Rnd() * 9) + 1 'first compare to horizontal For z = 1 To x If z <> x Then If SudokuGrid(z, y) = num Then Unique = False : Exit For Next 'then compare to vertical If Unique = True Then For z = 1 To y If z <> y Then If SudokuGrid(x, z) = num Then Unique = False : Exit For Next End If 'then compare to others in this box ''place compare code here'' If Unique = True Then SudokuGrid(x, y) = num : Exit Do Loop Next Next 'debug output Dim debugoutput As String For y = 1 To 9 For x = 1 To 9 debugoutput = debugoutput & Str(SudokuGrid(x, y)) Next debugoutput = debugoutput + vbCrLf Next MsgBox(debugoutput) End Sub
But for whatever reason, it just won't work vertically. It gets stuck in a permanent loop. I've spent a few hours trying to figure out why. If no one can help, i'm going to try something different. but i sure would like to know what's wrong with this code.![]()




Reply With Quote