Imports System.IO Imports System.Reflection Imports System.Drawing.Printing Public Class Form1 Dim _assembly As [Assembly] 'to get at pdf file Dim r As BinaryReader 'to read pdf file Dim sudoku(80, 2, 2, 2, 2, 2, 2) As Byte '0 1 2 and 81 steps 'this is suduku(step,x large,y large,x small,y small,num X,num Y) Dim sustep As Byte Dim xl As Byte Dim yl As Byte 'the large square Dim xs As Byte Dim ys As Byte 'the small square Dim nx As Byte Dim ny As Byte ' numbers 1 to 9 in each small square Dim randomgenerator As New Random Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load _assembly = [Assembly].GetExecutingAssembly() Dim sunum As Byte = 1 'set initial suddoku For n = 1 To 3 For m = 1 To 3 'set 1 to 9 in each number box For l = 1 To 3 For k = 1 To 3 For j = 1 To 3 For i = 1 To 3 sudoku(sustep, m - 1, n - 1, k - 1, l - 1, i - 1, j - 1) = sunum sunum = sunum + 1 Next i Next j sunum = 1 'set number back to 1 for next box Next k Next l Next m Next n End Sub Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick Dim xmouse As Integer = MousePosition.X - 400 Dim ymouse As Integer = MousePosition.Y - 120 'is MouseButtons in sudoku nx = 0 xs = 0 xl = 0 'reset matrix counters to find selected position ny = 0 ys = 0 yl = 0 If xmouse > 0 And xmouse < 540 And ymouse > 0 And ymouse < 540 Then 'find x position for selected number Do While xmouse > 0 xmouse = xmouse - 20 If xmouse > 0 Then nx = nx + 1 If nx = 3 Then nx = 0 xs = xs + 1 If xs = 3 Then xs = 0 xl = xl + 1 End If End If End If Loop 'find y position for selected number Do While ymouse > 0 ymouse = ymouse - 20 If ymouse > 0 Then ny = ny + 1 If ny = 3 Then ny = 0 ys = ys + 1 If ys = 3 Then ys = 0 yl = yl + 1 End If End If End If Loop numberselected() 'incorporate number in sudoku checkselection() 'display new sudoku Form1_Paint(Nothing, Nothing) End If End Sub Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim g As Graphics = CreateGraphics() g.FillRectangle(Brushes.White, 400, 100, 540, 540) Dim blackpen As New Pen(Color.Black, 3) For i = 0 To 3 ' large squaresdraw g.DrawLine(blackpen, 400, i * 180 + 100, 940, i * 180 + 100) g.DrawLine(blackpen, i * 180 + 400, 100, i * 180 + 400, 640) Next For i = 0 To 9 'draw small squares g.DrawLine(Pens.Black, 400, i * 60 + 100, 940, i * 60 + 100) g.DrawLine(Pens.Black, i * 60 + 400, 100, i * 60 + 400, 640) Next Dim bigfont As New System.Drawing.Font("arial", 42, FontStyle.Regular) Dim textfont As New System.Drawing.Font("arial", 16, FontStyle.Regular) Dim textbrush As New System.Drawing.SolidBrush(System.Drawing.Color.Black) Dim bignum As Byte 'start with 0 for big number display Label2.Text = "This is step " & sustep For n = 1 To 3 For m = 1 To 3 For l = 1 To 3 For k = 1 To 3 Dim numselect As Byte = 0 'determine how many zeros in number box For j = 0 To 2 For i = 0 To 2 If sudoku(sustep, m - 1, n - 1, k - 1, l - 1, i, j) = 0 Then numselect = numselect + 1 Else : bignum = sudoku(sustep, m - 1, n - 1, k - 1, l - 1, i, j) End If Next Next If numselect <> 8 Then 'number not selected show all possibles For j = 1 To 3 For i = 1 To 3 Dim nums As String = sudoku(sustep, m - 1, n - 1, k - 1, l - 1, i - 1, j - 1) If nums = "0" Then nums = " " g.DrawString(nums, textfont, textbrush, 400 + (i - 1) * 20 + (k - 1) * 60 + (m - 1) * 180, 100 + (j - 1) * 20 + (l - 1) * 60 + (n - 1) * 180) Next i Next j Else : If bignum > 9 Then bignum = bignum - 10 'number has been through selection so has 10 added g.DrawString(bignum, bigfont, textbrush, 400 + (k - 1) * 60 + (m - 1) * 180, 100 + (l - 1) * 60 + (n - 1) * 180) 'one number only in box so display that number as big End If Next k Next l Next m Next n g.Dispose() End Sub Private Sub numberselected() If sudoku(sustep, xl, yl, xs, ys, nx, ny) <> 0 And sudoku(sustep, xl, yl, xs, ys, nx, ny) < 10 Then ' if zero cant select if >9 already selected If sustep < 80 Then sustep = sustep + 1 'number selected go to next step but < 80 For n = 1 To 3 For m = 1 To 3 'copy all to the next step For l = 1 To 3 For k = 1 To 3 For j = 1 To 3 For i = 1 To 3 sudoku(sustep, m - 1, n - 1, k - 1, l - 1, i - 1, j - 1) = sudoku(sustep - 1, m - 1, n - 1, k - 1, l - 1, i - 1, j - 1) Next i Next j Next k Next l Next m Next n 'add 10 to selected number to show its selected sudoku(sustep, xl, yl, xs, ys, nx, ny) = sudoku(sustep, xl, yl, xs, ys, nx, ny) + 10 'set all to zero except selected number in the number box For j = 0 To 2 For i = 0 To 2 If Not (i = nx And j = ny) Then sudoku(sustep, xl, yl, xs, ys, i, j) = 0 End If Next i Next j 'horizontal row cannot have this number For j = 0 To 2 For i = 0 To 2 'horizontal row cannot have this number If Not (i = xs And j = xl) Then sudoku(sustep, j, yl, i, ys, nx, ny) = 0 End If Next i Next j 'vertical row cannot have this number For j = 0 To 2 For i = 0 To 2 'vertical row cannot have this number If Not (i = ys And j = yl) Then sudoku(sustep, xl, j, xs, i, nx, ny) = 0 End If Next i Next j ' box of 9 numbers(large) cannot have this number For j = 0 To 2 For i = 0 To 2 'box of 9 cannot have this number If Not (i = ys And j = xs) Then sudoku(sustep, xl, yl, j, i, nx, ny) = 0 End If Next i Next j End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim TempFile As String = Path.GetTempFileName() TempFile = TempFile & "sudoku.pdf" r = New BinaryReader(_assembly.GetManifestResourceStream("Windowsform2.sudoku.pdf")) 'gets file data into r to write to tempory file My.Computer.FileSystem.WriteAllBytes(TempFile, r.ReadBytes(100000000), True) 'for some reason cant use direct into process start to run adobe Dim ProcessStart As New ProcessStartInfo(TempFile) Process.Start(ProcessStart) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If sustep <> 0 Then sustep = sustep - 1 Form1_Paint(Nothing, Nothing) End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click ' Create the document and attach an event handler. Dim MyDoc As New PrintDocument() AddHandler MyDoc.PrintPage, AddressOf MyDoc_PrintPage MyDoc.Print() 'just print direct without user input End Sub Private Sub MyDoc_PrintPage(ByVal sender As Object, _ ByVal e As PrintPageEventArgs) ' Draw the sudoku image on screen Dim suprint As New Bitmap(543, 543) Dim subound As New Rectangle(403, 122, 543, 543) 'bit different because of frame Dim graph As Graphics = Graphics.FromImage(suprint) 'to print image must have in image format drawing to screen does not count graph.CopyFromScreen(subound.Location, New Point(0, 0), subound.Size) e.Graphics.DrawImage(suprint, 100, 100) End Sub Private Sub checkselection() Dim bignum As Byte Dim nonselect As Byte While nonselect = 0 For k = 0 To 2 For l = 0 To 2 For m = 0 To 2 For n = 0 To 2 Dim numselect As Byte = 0 'determine how many zeros in number box For j = 0 To 2 For i = 0 To 2 If sudoku(sustep, k, l, m, n, j, i) = 0 Then numselect = numselect + 1 Else : bignum = sudoku(sustep, k, l, m, n, j, i) xl = k yl = l xs = m ys = n nx = j ny = i 'the position of the number End If Next Next If numselect = 8 And bignum < 10 Then numberselected() nonselect = 0 ' found one so try again Else : nonselect = 1 End If Next Next Next Next End While 'should now have done all non user selected numbers End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click While sustep <> 80 'try until have full sudoku generatesudoku() End While sustep = sustep - 1 Form1_Paint(Nothing, Nothing) End Sub Private Sub generatesudoku() sustep = 0 Dim blank As Byte = 0 While blank = 0 And sustep < 80 xl = randomgenerator.Next(0, 3) yl = randomgenerator.Next(0, 3) xs = randomgenerator.Next(0, 3) ys = randomgenerator.Next(0, 3) nx = randomgenerator.Next(0, 3) ny = randomgenerator.Next(0, 3) 'totally random number numberselected() checkselection() For k = 0 To 2 For l = 0 To 2 For m = 0 To 2 For n = 0 To 2 Dim numselect As Byte = 0 'determine how many zeros in number box For j = 0 To 2 For i = 0 To 2 If sudoku(sustep, k, l, m, n, j, i) = 0 Then numselect = numselect + 1 End If Next Next If numselect = 9 Then blank = 1 Next ' sudoku cannot be solved Next Next Next End While End Sub End Class