Hey,
I'm trying to write my own Sudoku game. So far, I've written almost everything concerning the game itself. Problem is, the function that creates the board itself, meaning, the generator.
Some cases it worked. Most cases, it stuck in an endless loop.
If you could guide me or give me some points it would be great.
Basically I'm using TLP as my board and created a special class called "textfield" for hosting some extra variables.
Thanks for the help :)
I'm trying to write my own Sudoku game. So far, I've written almost everything concerning the game itself. Problem is, the function that creates the board itself, meaning, the generator.
Some cases it worked. Most cases, it stuck in an endless loop.
If you could guide me or give me some points it would be great.
Basically I'm using TLP as my board and created a special class called "textfield" for hosting some extra variables.
Code:
Public Class Form1
Dim level As Integer
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim box As textfield
Dim mone, sec As Integer
mone = 0
sec = 1
For i As Integer = 0 To 80
box = New textfield
box.MaxLength = 1
box.Anchor = AnchorStyles.Bottom
box.Anchor = AnchorStyles.Left
box.Anchor = AnchorStyles.Right
box.Anchor = AnchorStyles.Top
box.TextAlign = HorizontalAlignment.Center
box.Font = New Font("Arial", 20)
box.Dock = DockStyle.Fill
box.BorderStyle = BorderStyle.None
AddHandler box.KeyUp, AddressOf check
board.Controls.Add(box)
box.loca.X = board.GetPositionFromControl(box).Row
box.loca.Y = board.GetPositionFromControl(box).Column
getsection(box)
box.Tag = i
Next
'sortme()
End Sub
Public Sub check(sender As System.Object, e As System.EventArgs)
Dim boxclk = DirectCast(sender, textfield)
If boxclk.Text = "" Then
Exit Sub
End If
If Asc(boxclk.Text) > 48 And Asc(boxclk.Text) < 58 Then 'Checks if is a number
If checkme(boxclk) = 1 Then
MsgBox("Can't write the same number on the same row")
boxclk.Text = ""
End If
If checkme(boxclk) = 2 Then
MsgBox("Can't write the same number on the same column")
boxclk.Text = ""
End If
If checkme(boxclk) = 3 Then
MsgBox("Same section not allowed")
boxclk.Text = ""
End If
Else
Beep()
boxclk.Text = ""
Exit Sub
End If
End Sub
Public Sub sort()
Dim mone As Integer = 0
Dim err As Integer = 0
Dim rnd As System.Random
Dim txt As textfield
Dim tmp As Integer = 0
Dim st As String = ""
Dim i As Integer = 0
rnd = New Random
Randomize()
For Each txt In board.Controls
tmp = rnd.Next(1, 10)
While Matched(txt, tmp) = 1
tmp = rnd.Next(1, 10)
txt.used(txt.moneused) = tmp
End While
txt.Text = tmp
While checkme(txt) <> 0
tmp = rnd.Next(1, 10)
txt.Text = tmp
End While
If txt.moneused > txt.used.Length Then
txt.moneused = txt.used.Length
Else
txt.moneused += 1
End If
mone += 1
If mone > 45 Then
Exit Sub
End If
Next
End Sub
Public Function Matched(ByVal txt As textfield, ByVal tmp As Integer)
For i = 0 To txt.moneused
If txt.Text = txt.used(i).ToString Then
Return 1
End If
Next
Return 0
End Function
Public Function checkme(ByVal box As textfield)
Dim txt As textfield
For i = 0 To 8
txt = board.GetControlFromPosition(i, box.loca.X)
If txt.Text = box.Text And i <> box.loca.Y Then ' ROW
Return 1
End If
Next
For i = 0 To 8
txt = board.GetControlFromPosition(box.loca.Y, i)
If txt.Text = box.Text And i <> box.loca.X Then ' COLUMN
Return 2
End If
Next
Select Case box.section
Case 1 : For i = 0 To 2
For j As Integer = 0 To 2
If box.Text = board.GetControlFromPosition(i, j).Text And i <> box.loca.Y And j <> box.loca.X Then
Return 3
End If
Next
Next
Case 2 : For i = 3 To 5
For j As Integer = 0 To 2
If box.Text = board.GetControlFromPosition(i, j).Text And i <> box.loca.Y And j <> box.loca.X Then
Return 3
End If
Next
Next
Case 3 : For i = 6 To 8
For j As Integer = 0 To 2
If box.Text = board.GetControlFromPosition(i, j).Text And i <> box.loca.Y And j <> box.loca.X Then
Return 3
End If
Next
Next
Case 4 : For i = 0 To 2
For j As Integer = 3 To 5
If box.Text = board.GetControlFromPosition(i, j).Text And i <> box.loca.Y And j <> box.loca.X Then
Return 3
End If
Next
Next
Case 5 : For i = 3 To 5
For j As Integer = 3 To 5
If box.Text = board.GetControlFromPosition(i, j).Text And i <> box.loca.Y And j <> box.loca.X Then
Return 3
End If
Next
Next
Case 6 : For i = 6 To 8
For j As Integer = 3 To 5
If box.Text = board.GetControlFromPosition(i, j).Text And i <> box.loca.Y And j <> box.loca.X Then
Return 3
End If
Next
Next
Case 7 : For i = 0 To 2
For j As Integer = 6 To 8
If box.Text = board.GetControlFromPosition(i, j).Text And i <> box.loca.Y And j <> box.loca.X Then
Return 3
End If
Next
Next
Case 8 : For i = 3 To 5
For j As Integer = 6 To 8
If box.Text = board.GetControlFromPosition(i, j).Text And i <> box.loca.Y And j <> box.loca.X Then
Return 3
End If
Next
Next
Case 9 : For i = 6 To 8
For j As Integer = 6 To 8
If box.Text = board.GetControlFromPosition(i, j).Text And i <> box.loca.Y And j <> box.loca.X Then
Return 3
End If
Next
Next
End Select
Return 0
End Function
Public Sub getsection(ByVal box As textfield)
Dim ro As Integer = box.loca.X
Dim co As Integer = box.loca.Y
If ro >= 0 And ro < 3 And co >= 0 And co < 3 Then
box.section = 1
End If
If ro >= 0 And ro < 3 And co > 2 And co < 6 Then
box.section = 2
End If
If ro >= 0 And ro < 3 And co > 5 And co <= 8 Then
box.section = 3
End If
If ro >= 3 And ro < 6 And co >= 0 And co < 3 Then
box.section = 4
End If
If ro >= 3 And ro < 6 And co > 2 And co < 6 Then
box.section = 5
End If
If ro >= 3 And ro < 6 And co > 5 And co <= 8 Then
box.section = 6
End If
If ro >= 6 And ro <= 8 And co >= 0 And co < 3 Then
box.section = 7
End If
If ro >= 6 And ro <= 8 And co > 2 And co < 6 Then
box.section = 8
End If
If ro >= 6 And ro <= 8 And co > 5 And co <= 8 Then
box.section = 9
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
sort()
End Sub
End Class
Public Class textfield
Inherits TextBox
Public loca As Point
Public section As Integer
Public moneused As Integer = 0
Public used(82) As Integer
End Class