Sudoku puzzles can be generated by a switching the rows and columns of a valid puzzle.
The switch has to be done between the 123, 456 and 789 rows/columns. ie; 1 cannot be switched with 7 for example.
A random pair can be generated and if a loop is run say 50 times, we get a new puzzle.
Then the cells can be hidden again randomly.
The following code generates the puzzle from a base character string which is converted to a 81 length Char array.
The output is the solution as well as the puzzle with blank (or 0s)
Dim charArray() As Char = stdArray.ToCharArray
Dim rng As New Random
Dim m As Integer
Dim row1 As Integer
Dim row2 As Integer
Dim col1 As Integer
Dim col2 As Integer
Dim i As Integer
Dim cell1 As Integer
Dim cell2 As Integer
Dim tempC As Char
Dim k As Integer
For k = 1 To 50
m = rng.Next(1, 4)
row1 = rng.Next(1, 4)
row2 = rng.Next(1, 4)
If row2 = row1 Then row2 = rng.Next(1, 4)
If row2 = row1 Then row2 = rng.Next(1, 4)
If row2 = row1 Then row2 = rng.Next(1, 4)
row1 = row1 + ((m - 1) * 3)
row2 = row2 + ((m - 1) * 3)
m = rng.Next(1, 4)
col1 = rng.Next(1, 4)
col2 = rng.Next(1, 4)
If col2 = col1 Then col2 = rng.Next(1, 4)
If col2 = col1 Then col2 = rng.Next(1, 4)
If col2 = col1 Then col2 = rng.Next(1, 4)
col1 = col1 + ((m - 1) * 3)
col2 = col2 + ((m - 1) * 3)
For i = 1 To 9
cell1 = ((row1 - 1) * 9) + i
cell2 = ((row2 - 1) * 9) + i
tempC = charArray(cell1 - 1)
charArray(cell1 - 1) = charArray(cell2 - 1)
charArray(cell2 - 1) = tempC
Next
For i = 1 To 9
cell1 = ((i - 1) * 9) + col1
cell2 = ((i - 1) * 9) + col2
tempC = charArray(cell1 - 1)
charArray(cell1 - 1) = charArray(cell2 - 1)
charArray(cell2 - 1) = tempC
Next
Next
Dim px0(80) As Char
Dim pxb(80) As Char
For k = 0 To 80
px0(k) = charArray(k)
pxb(k) = charArray(k)
Next
For k = 1 To 52
m = rng.Next(0, 81)
px0(m) = "0"
pxb(m) = " "
Next
The switch has to be done between the 123, 456 and 789 rows/columns. ie; 1 cannot be switched with 7 for example.
A random pair can be generated and if a loop is run say 50 times, we get a new puzzle.
Then the cells can be hidden again randomly.
The following code generates the puzzle from a base character string which is converted to a 81 length Char array.
The output is the solution as well as the puzzle with blank (or 0s)
Dim stdArray As String = "317849265245736891869512473456398712732164958981257634174925386693481527528673149"Dim charArray() As Char = stdArray.ToCharArray
Dim rng As New Random
Dim m As Integer
Dim row1 As Integer
Dim row2 As Integer
Dim col1 As Integer
Dim col2 As Integer
Dim i As Integer
Dim cell1 As Integer
Dim cell2 As Integer
Dim tempC As Char
Dim k As Integer
For k = 1 To 50
m = rng.Next(1, 4)
row1 = rng.Next(1, 4)
row2 = rng.Next(1, 4)
If row2 = row1 Then row2 = rng.Next(1, 4)
If row2 = row1 Then row2 = rng.Next(1, 4)
If row2 = row1 Then row2 = rng.Next(1, 4)
row1 = row1 + ((m - 1) * 3)
row2 = row2 + ((m - 1) * 3)
m = rng.Next(1, 4)
col1 = rng.Next(1, 4)
col2 = rng.Next(1, 4)
If col2 = col1 Then col2 = rng.Next(1, 4)
If col2 = col1 Then col2 = rng.Next(1, 4)
If col2 = col1 Then col2 = rng.Next(1, 4)
col1 = col1 + ((m - 1) * 3)
col2 = col2 + ((m - 1) * 3)
For i = 1 To 9
cell1 = ((row1 - 1) * 9) + i
cell2 = ((row2 - 1) * 9) + i
tempC = charArray(cell1 - 1)
charArray(cell1 - 1) = charArray(cell2 - 1)
charArray(cell2 - 1) = tempC
Next
For i = 1 To 9
cell1 = ((i - 1) * 9) + col1
cell2 = ((i - 1) * 9) + col2
tempC = charArray(cell1 - 1)
charArray(cell1 - 1) = charArray(cell2 - 1)
charArray(cell2 - 1) = tempC
Next
Next
Dim px0(80) As Char
Dim pxb(80) As Char
For k = 0 To 80
px0(k) = charArray(k)
pxb(k) = charArray(k)
Next
For k = 1 To 52
m = rng.Next(0, 81)
px0(m) = "0"
pxb(m) = " "
Next
Comments
Post a Comment