document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. Public Class SudokuGen
  2.     \'Table that defines cordinates of cells within block regions
  3.     Private Shared Blks(,,) As Byte = {{{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}}, _
  4.                                        {{0, 3}, {0, 4}, {0, 5}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}}, _
  5.                                        {{0, 6}, {0, 7}, {0, 8}, {1, 6}, {1, 7}, {1, 8}, {2, 6}, {2, 7}, {2, 8}}, _
  6.                                        {{3, 0}, {3, 1}, {3, 2}, {4, 0}, {4, 1}, {4, 2}, {5, 0}, {5, 1}, {5, 2}}, _
  7.                                        {{3, 3}, {3, 4}, {3, 5}, {4, 3}, {4, 4}, {4, 5}, {5, 3}, {5, 4}, {5, 5}}, _
  8.                                        {{3, 6}, {3, 7}, {3, 8}, {4, 6}, {4, 7}, {4, 8}, {5, 6}, {5, 7}, {5, 8}}, _
  9.                                        {{6, 0}, {6, 1}, {6, 2}, {7, 0}, {7, 1}, {7, 2}, {8, 0}, {8, 1}, {8, 2}}, _
  10.                                        {{6, 3}, {6, 4}, {6, 5}, {7, 3}, {7, 4}, {7, 5}, {8, 3}, {8, 4}, {8, 5}}, _
  11.                                        {{6, 6}, {6, 7}, {6, 8}, {7, 6}, {7, 7}, {7, 8}, {8, 6}, {8, 7}, {8, 8}}}
  12.     \'Generates a new random sudoku solution, with a random number generator as input
  13.     Public Function Generate(Rnd As Random) As Byte()
  14.         \'Stores the entire board as a two demensional array of cells (9 rows,9 columns)
  15.         \' Stores the values of the finshed board to be returned
  16.         \'  Keeps track of what index in the return board is to be worked on
  17.         Dim B(8, 8) As Cell, index As Byte = 0, RetBoard(80) As Byte
  18.         \'Keeps retrying until valid solution is found
  19.         For Retry As SByte = 0 To 0
  20.             \'Initializes cells in the cell array
  21.             For y As Byte = 0 To 8
  22.                 For x As Byte = 0 To 8
  23.                     B(y, x) = New Cell
  24.                 Next
  25.             Next
  26.             \'Stores a list of valid blocks to try
  27.             Dim VB As List(Of Byte) = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8}.ToList
  28.             \'Repeate until valid blocks = 0 or if retry = -1
  29.             Do Until VB.Count = 0 OrElse Retry = -1
  30.                 \'Stores a list of valid cells to try in the block
  31.                 Dim VC As List(Of Byte) = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8}.ToList
  32.                 \'Selects a random block from the valid blocks list
  33.                 Dim nb As Byte = VB(Rnd.Next(VB.Count))
  34.                 \'Removes the chosen block from the valid blocks list so its not reused
  35.                 VB.Remove(nb)
  36.                 \'Repeats until all cells in the chosen block is populated
  37.                 Do Until VC.Count = 0
  38.                     \'Chooses a random cell from the valid cells list
  39.                     Dim C As Byte = VC(Rnd.Next(VC.Count))
  40.                     \'Gets the X cordinate on the board from the selected cell in the block
  41.                     \' Gets the Y cordinate on the board from the selected cell in the block
  42.                     \'  Placeholder for storing the chosen number of the cell being worked on
  43.                     Dim X As Byte = Blks(nb, C, 0), Y As Byte = Blks(nb, C, 1), N As Byte = 0
  44.                     \'Processes the current cell and sets the state of the current iteration
  45.                     Retry = B(X, Y).Process(N, Rnd)
  46.                     \'If current state failed then exit the current iteration to retry a new board iteration
  47.                     \' If current state suceeded then remove the cell from valid cells list so its not reused
  48.                     If Retry = -1 Then Exit Do Else VC.Remove(C)
  49.                     \'Invalidation loop
  50.                     For i As Byte = 0 To 8
  51.                         \'Invalidates the cells on the same X axis as the chosen cell
  52.                         B(X, i).VN.Remove(N)
  53.                         \'Invalidates the cells on the same Y axis as the chosen cell
  54.                         B(i, Y).VN.Remove(N)
  55.                         \'Invalidates the cells in the same Block as the chosen cell
  56.                         B(Blks(nb, i, 0), Blks(nb, i, 1)).VN.Remove(N)
  57.                     Next
  58.                 Loop
  59.             Loop
  60.         Next
  61.         \'Populates RetBoard with the value of each cell
  62.         For y As Byte = 0 To 8
  63.             For x As Byte = 0 To 8
  64.                 RetBoard(index) = B(y, x).Val
  65.                 index += 1
  66.             Next
  67.         Next
  68.         \'Returns the randomly generated Sudoku solution
  69.         Return RetBoard
  70.     End Function
  71.     \'Class used for each cell
  72.     Private Class Cell
  73.         \'Cell\'s value
  74.         Public Val As Byte = 0
  75.         \'List of valid number possible for the current cell
  76.         Public VN As List(Of Byte) = New Byte() {1, 2, 3, 4, 5, 6, 7, 8, 9}.ToList
  77.         \'Function to give the cell a value, reference N is the placeholder, Rnd as random number generator
  78.         Public Function Process(ByRef N As Byte, Rnd As Random) As SByte
  79.             \'If there are no valid numbers tell the current iteration it failed
  80.             If VN.Count = 0 Then Return -1
  81.             \'Picks randomly from valid numbers list, Does not remove because cell wont be processed again
  82.             Val = VN(Rnd.Next(VN.Count))
  83.             \'Tells the current iteration what the chosen number is to invalidate that number in other cells
  84.             N = Val
  85.             \'Tells the current iteration that assigning a value to the current cell was a success
  86.             Return 0
  87.         End Function
  88.     End Class
  89. End Class
');