creating a SuDoKu puzzle generator

major_lost 0 Tallied Votes 476 Views Share

I know that this probably has been done by someone, but I want to learn how to do this using VB 2010 express. I cannot seem to get the numbers to stay in the text boxes that they are dropped in. The numbers always go to the last box (#81). What am I doing wrong. You might have to change some lines to get the numbers to display in the lables assigned for them - that's ok.

Public Class Form2
    Inherits System.Windows.Forms.Form
    Public boxes(8, 8) As TextBox
    Public newbox As TextBox


    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load


        Dim groupBoxes() As GroupBox = Me.Controls.OfType(Of GroupBox).OrderBy(Function(g) g.Name).ToArray
        Dim arrayIndex As Integer = 0
        Dim rowStartIndex As Integer = 0
        Dim rowCounter As Integer = 0
        Dim colCounter As Integer = 0

        For row As Integer = 0 To 8
            For col As Integer = 0 To 8

                'create a new textbox and set its properties
                newbox = New TextBox
                newbox.Size = New Drawing.Size(50, 30)
                newbox.Location = New Point(5 + colCounter * 70, 20 + rowCounter * 20 * 2)
                newbox.TextAlign = HorizontalAlignment.Center
                newbox.Font = New Font("Tahoma", 18, FontStyle.Bold)
                newbox.AllowDrop = True
                newbox.Clear()

                'connect it to a handler, save a reference to the array and add it to the form controls
                'AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
                AddHandler newbox.DragEnter, AddressOf TextBox_DragEnter
                AddHandler newbox.DragDrop, AddressOf TextBox_DragDrop

                boxes(row, col) = newbox

                Dim gb As GroupBox = groupBoxes(arrayIndex)
                gb.Controls.Add(newbox)
                colCounter += 1
                If colCounter = 3 Then colCounter = 0 : arrayIndex += 1
                If col = 8 Then arrayIndex = rowStartIndex

            Next
            rowCounter += 1
            If rowCounter = 3 Then rowCounter = 0 : arrayIndex += 3
            rowStartIndex = arrayIndex
        Next

        Label1.Text = IIf(Display = 1, "1", "A")
        Label2.Text = IIf(Display = 1, "2", "B")
        Label3.Text = IIf(Display = 1, "3", "C")
        Label4.Text = IIf(Display = 1, "4", "D")
        Label5.Text = IIf(Display = 1, "5", "E")
        Label6.Text = IIf(Display = 1, "6", "F")
        Label7.Text = IIf(Display = 1, "7", "G")
        Label8.Text = IIf(Display = 1, "8", "H")
        Label9.Text = IIf(Display = 1, "9", "I")

        Label16.Text = IIf(Autocheck = False, "Off", "On")
        Label16.ForeColor = IIf(Autocheck = False, Color.Black, Color.Red)
        Label17.Text = Microsoft.VisualBasic.Switch(Difficulty = 0, "Easy", Difficulty = 1, "Medium", Difficulty = 2, "Hard")
        Label17.ForeColor = Microsoft.VisualBasic.Switch(Difficulty = 0, Color.Green, Difficulty = 1, Color.Black, Difficulty = 2, Color.Red)
        Label18.Text = Microsoft.VisualBasic.Switch(Display = 0, "Letters", Display = 1, "Numbers")
        Label18.ForeColor = Microsoft.VisualBasic.Switch(Display = 0, Color.Red, Display = 1, Color.Black)
        Label19.Text = IIf(Sounds = False, "Off", "On")
        Label19.ForeColor = IIf(Sounds = False, Color.Black, Color.Red)


    End Sub

    Private Sub TextBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        ' Check the format of the data being dropped.
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            ' Display the copy cursor.
            e.Effect = DragDropEffects.Copy
        Else
            ' Display the no-drop cursor.
            e.Effect = DragDropEffects.None
        End If

    End Sub

    Private Sub TextBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        newbox.Text = e.Data.GetData(DataFormats.Text)
    End Sub

    Private Sub SettingsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SettingsToolStripMenuItem.Click
        Me.Hide()
        SetFrm.ShowDialog()
    End Sub

    Private Sub Label1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
        Label1.DoDragDrop(Label1.Text, DragDropEffects.Copy)
    End Sub

    Private Sub Label2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label2.MouseDown
        Label2.DoDragDrop(Label2.Text, DragDropEffects.Copy)
    End Sub

    Private Sub Label3_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label3.MouseDown
        Label3.DoDragDrop(Label3.Text, DragDropEffects.Copy)
    End Sub

    Private Sub Label4_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label4.MouseDown
        Label4.DoDragDrop(Label4.Text, DragDropEffects.Copy)
    End Sub

    Private Sub Label5_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label5.MouseDown
        Label5.DoDragDrop(Label5.Text, DragDropEffects.Copy)
    End Sub

    Private Sub Label6_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label6.MouseDown
        Label6.DoDragDrop(Label6.Text, DragDropEffects.Copy)
    End Sub

    Private Sub Label7_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label7.MouseDown
        Label7.DoDragDrop(Label7.Text, DragDropEffects.Copy)
    End Sub

    Private Sub Label8_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label8.MouseDown
        Label8.DoDragDrop(Label8.Text, DragDropEffects.Copy)
    End Sub

    Private Sub Label9_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label9.MouseDown
        Label9.DoDragDrop(Label9.Text, DragDropEffects.Copy)
    End Sub
End Class
Reverend Jim 4,780 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I'd try this out locally but there are several undefined "things" like Sounds, Difficulty, etc. Also, there is not enough information to recreate the form, not to mention the lack of any information as to how this is all supposed to work.

major_lost 0 Newbie Poster

REM out all of the other lines (47 - 64). All of those are options I have set up on Form3. Create a form that is 763 x 591. Add 9 Groupboxes size 205 x 139, place the first one at 114, 111 and put them in groups of 3 aligning the lefts and bottoms.
Next add 9 lables (Label1 - Label9) and make the text for them the numbers 1 through 9. Place these lables on the left side of the form vertically. That is all you should need to do besides copy the remaining code. When you run it, you will find that when you attempt to drop and drag and of the label (Lable1-Label9) it goes to the last text box (#81)

Reverend Jim 4,780 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

For the handler that places the dropped text you have

Private Sub TextBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
    newbox.Text = e.Data.GetData(DataFormats.Text)
End Sub

so no matter what box is actually being dropped on, only newbox will be modified. Change it to

Private Sub TextBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
    sender.Text = e.Data.GetData(DataFormats.Text)
End Sub

and see what happens from there. This is just my opinion but since you are using textboxes to hold the dropped digits, wouldn't it be easier to just type a digit into the box? A few years ago I wrote a tool for solving Sudoku puzzles. It doesn't have any smarts for actually solving (where would be the fun in that). What it does is save me the time of writing and erasing. It looks like this on startup

clip-0000.jpg

You start by setting each cell to the given puzzle digits by clicking a digit. At that point all of the other digit buttons are made invisible and the picturebox behind the buttons is set to an image of a large digit from 1-9. You might try something like that for setting cell values. It's easier than typing or drag/drop.

major_lost 0 Newbie Poster

Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
It works great.
I sincerely appreciate your help.
GOD Bless you

major_lost

Reverend Jim 4,780 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

No problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.