Public Class SudokuGen
\'Table that defines cordinates of cells within block regions
Private Shared Blks(,,) As Byte = {{{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}}, _
{{0, 3}, {0, 4}, {0, 5}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}}, _
{{0, 6}, {0, 7}, {0, 8}, {1, 6}, {1, 7}, {1, 8}, {2, 6}, {2, 7}, {2, 8}}, _
{{3, 0}, {3, 1}, {3, 2}, {4, 0}, {4, 1}, {4, 2}, {5, 0}, {5, 1}, {5, 2}}, _
{{3, 3}, {3, 4}, {3, 5}, {4, 3}, {4, 4}, {4, 5}, {5, 3}, {5, 4}, {5, 5}}, _
{{3, 6}, {3, 7}, {3, 8}, {4, 6}, {4, 7}, {4, 8}, {5, 6}, {5, 7}, {5, 8}}, _
{{6, 0}, {6, 1}, {6, 2}, {7, 0}, {7, 1}, {7, 2}, {8, 0}, {8, 1}, {8, 2}}, _
{{6, 3}, {6, 4}, {6, 5}, {7, 3}, {7, 4}, {7, 5}, {8, 3}, {8, 4}, {8, 5}}, _
{{6, 6}, {6, 7}, {6, 8}, {7, 6}, {7, 7}, {7, 8}, {8, 6}, {8, 7}, {8, 8}}}
\'Generates a new random sudoku solution, with a random number generator as input
Public Function Generate(Rnd As Random) As Byte()
\'Stores the entire board as a two demensional array of cells (9 rows,9 columns)
\' Stores the values of the finshed board to be returned
\' Keeps track of what index in the return board is to be worked on
Dim B(8, 8) As Cell, index As Byte = 0, RetBoard(80) As Byte
\'Keeps retrying until valid solution is found
For Retry As SByte = 0 To 0
\'Initializes cells in the cell array
For y As Byte = 0 To 8
For x As Byte = 0 To 8
B(y, x) = New Cell
Next
Next
\'Stores a list of valid blocks to try
Dim VB As List(Of Byte) = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8}.ToList
\'Repeate until valid blocks = 0 or if retry = -1
Do Until VB.Count = 0 OrElse Retry = -1
\'Stores a list of valid cells to try in the block
Dim VC As List(Of Byte) = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8}.ToList
\'Selects a random block from the valid blocks list
Dim nb As Byte = VB(Rnd.Next(VB.Count))
\'Removes the chosen block from the valid blocks list so its not reused
VB.Remove(nb)
\'Repeats until all cells in the chosen block is populated
Do Until VC.Count = 0
\'Chooses a random cell from the valid cells list
Dim C As Byte = VC(Rnd.Next(VC.Count))
\'Gets the X cordinate on the board from the selected cell in the block
\' Gets the Y cordinate on the board from the selected cell in the block
\' Placeholder for storing the chosen number of the cell being worked on
Dim X As Byte = Blks(nb, C, 0), Y As Byte = Blks(nb, C, 1), N As Byte = 0
\'Processes the current cell and sets the state of the current iteration
Retry = B(X, Y).Process(N, Rnd)
\'If current state failed then exit the current iteration to retry a new board iteration
\' If current state suceeded then remove the cell from valid cells list so its not reused
If Retry = -1 Then Exit Do Else VC.Remove(C)
\'Invalidation loop
For i As Byte = 0 To 8
\'Invalidates the cells on the same X axis as the chosen cell
B(X, i).VN.Remove(N)
\'Invalidates the cells on the same Y axis as the chosen cell
B(i, Y).VN.Remove(N)
\'Invalidates the cells in the same Block as the chosen cell
B(Blks(nb, i, 0), Blks(nb, i, 1)).VN.Remove(N)
Next
Loop
Loop
Next
\'Populates RetBoard with the value of each cell
For y As Byte = 0 To 8
For x As Byte = 0 To 8
RetBoard(index) = B(y, x).Val
index += 1
Next
Next
\'Returns the randomly generated Sudoku solution
Return RetBoard
End Function
\'Class used for each cell
Private Class Cell
\'Cell\'s value
Public Val As Byte = 0
\'List of valid number possible for the current cell
Public VN As List(Of Byte) = New Byte() {1, 2, 3, 4, 5, 6, 7, 8, 9}.ToList
\'Function to give the cell a value, reference N is the placeholder, Rnd as random number generator
Public Function Process(ByRef N As Byte, Rnd As Random) As SByte
\'If there are no valid numbers tell the current iteration it failed
If VN.Count = 0 Then Return -1
\'Picks randomly from valid numbers list, Does not remove because cell wont be processed again
Val = VN(Rnd.Next(VN.Count))
\'Tells the current iteration what the chosen number is to invalidate that number in other cells
N = Val
\'Tells the current iteration that assigning a value to the current cell was a success
Return 0
End Function
End Class
End Class