Sudoku in VB

Hallo liebe User,
Also ich habe nun meinen Abschluss als ITA absolviert und auch somit mein Fachabi in der Informationstechnik. Da Programmieren aber zu meinen Hobby auch wurde, dachte ich mal ich schreibe ein kleines Sudoku Programm, da ich einfach mal wissen wollte wie man so ein Feld schnell erstellen kann.
Dazu schrieb ich nun erstmal folgende Funktion:

Noch wichtige Daten:
PC : 1,6GHz ; 1GB Ram ; Windows 7
Sprache: VB


Code:
Dim FELD(0 To 80)

Private Sub Command1_Click()
Label3.Caption = Time
    Dim FORT
    FORT = 0
    'Zeile1 füllen
    Dim SPZeile(0 To 8)
    'Dim Zeile(0 To 8)
    Dim Spalte(0 To 8)
    Dim SPSpalte(0 To 8)
    
    For n = 0 To 8
        SPZeile(n) = 99 'Irgendeinen Wert außerhalb von 0-8 einstellen
        Spalte(n) = 99
        SPSpalte(n) = 99
    Next n

'-------------------------------------------------------------------------------------
'----------Die 3 Linken Blöcke mit einer 2 Füllen-------------------------------------
'-------------------------------------------------------------------------------------

Dim ZAEHL
For ZAEHL = 1 To 9
    'Block1
    Do
        Spalte(0) = Int(3 * Rnd()) 'Welche Spalte im Block soll belegt werden
        MULTI = Int(3 * Rnd()) * 9 'Welche Zeile im Block soll belegt werden
    Loop While (FELD(Spalte(0) + MULTI) > "")
    FELD(Spalte(0) + MULTI) = ZAEHL 'Ins entsprechende Textfeld die 1 schreiben
    SPSpalte(0) = Spalte(0) 'Speichern der beschriebenen spalte
    SPZeile(0) = MULTI 'Speichern der beschriebenen zeile
    
        FORT = FORT + 1.25
    Text2.Text = FORT
    'Block4
    Do
        Do
            Spalte(1) = Int(3 * Rnd())
        Loop While Spalte(1) = SPSpalte(0) 'Berechne solange eine Spalte bis es nicht die aus Block1 ist
        MULTI = Int(3 * Rnd()) * 9 'Wähle wieder eine Zeile
    Loop While (FELD(Spalte(1) + MULTI + 27) > "")
    FELD(Spalte(1) + 27 + MULTI) = ZAEHL
    SPSpalte(1) = Spalte(1)
    SPZeile(1) = MULTI
    
    
        FORT = FORT + 1.25
    Text2.Text = FORT
    'Block7
    If ((SPSpalte(0) = 0 And SPSpalte(1) = 1) Or (SPSpalte(0) = 1 And SPSpalte(1) = 0)) Then Spalte(2) = 2
    If ((SPSpalte(0) = 1 And SPSpalte(1) = 2) Or (SPSpalte(0) = 2 And SPSpalte(1) = 1)) Then Spalte(2) = 0
    If ((SPSpalte(0) = 0 And SPSpalte(1) = 2) Or (SPSpalte(0) = 2 And SPSpalte(1) = 0)) Then Spalte(2) = 1

    Do
   '     Do
  '          Spalte(2) = Int(3 * Rnd())
 '       Loop While ((Spalte(2) = SPSpalte(1)) Or (Spalte(2) = SPSpalte(0)))
        MULTI = Int(3 * Rnd()) * 9
    Loop While (FELD(Spalte(2) + MULTI + 54) > "")
    FELD(Spalte(2) + 54 + MULTI) = ZAEHL
    SPSpalte(2) = Spalte(2)
    SPZeile(2) = MULTI
    
        FORT = FORT + 1.25
    Text2.Text = FORT
'-------------------------------------------------------------------------------------
'----------Die 3 Linken Blöcke mit einer 1 Füllen Klappt schonmal :)------------------
'-------------------------------------------------------------------------------------
'-------------------------------------------------------------------------------------
'----------Die 3 Mittleren Blöcke mit einer 1 Füllen Klappt schonmal :)---------------
'-------------------------------------------------------------------------------------
    
    'Block2
    Do
        Spalte(3) = Int(3 * Rnd())
        Do
            MULTI = Int(3 * Rnd()) * 9
        Loop While MULTI = SPZeile(0) 'Wähle solange zeile solange es nicht die im Block1 ist
    Loop While (FELD(Spalte(3) + MULTI + 3) > "")
    FELD(Spalte(3) + MULTI + 3) = ZAEHL
    SPSpalte(3) = Spalte(3)
    SPZeile(3) = MULTI
    
    
        FORT = FORT + 1.25
    Text2.Text = FORT
    'Block5
    Do
        Do
            Spalte(4) = Int(3 * Rnd())
        Loop While Spalte(4) = SPSpalte(3)
        Do
            MULTI = Int(3 * Rnd()) * 9
        Loop While MULTI = SPZeile(1)
    Loop While (FELD(Spalte(4) + 27 + MULTI + 3) > "")
    FELD(Spalte(4) + 27 + MULTI + 3) = ZAEHL
    SPSpalte(4) = Spalte(4)
    SPZeile(4) = MULTI
    
    
        FORT = FORT + 1.25
    Text2.Text = FORT
    'Block8
    Do
        If ((SPSpalte(3) = 0 And SPSpalte(4) = 1) Or (SPSpalte(3) = 1 And SPSpalte(4) = 0)) Then Spalte(5) = 2
        If ((SPSpalte(3) = 1 And SPSpalte(4) = 2) Or (SPSpalte(3) = 2 And SPSpalte(4) = 1)) Then Spalte(5) = 0
        If ((SPSpalte(3) = 0 And SPSpalte(4) = 2) Or (SPSpalte(3) = 2 And SPSpalte(4) = 0)) Then Spalte(5) = 1
  
       ' Do
      '      Spalte(5) = Int(3 * Rnd())
     '   Loop While ((Spalte(5) = SPSpalte(4)) Or (Spalte(5) = SPSpalte(3)))
        Do
            MULTI = Int(3 * Rnd()) * 9
        Loop While MULTI = SPZeile(2)
    Loop While (FELD(Spalte(5) + 54 + MULTI + 3) > "")
    FELD(Spalte(5) + 54 + MULTI + 3) = ZAEHL
    SPSpalte(5) = Spalte(5)
    SPZeile(5) = MULTI
    
    
        FORT = FORT + 1.25
    Text2.Text = FORT
'-------------------------------------------------------------------------------------
'----------Die 3 Mittleren Blöcke mit einer 1 Füllen Klappt schonmal :)---------------
'-------------------------------------------------------------------------------------
'-------------------------------------------------------------------------------------
'----------Die 3 Rechten Blöcke mit einer 1 Füllen Klappt schonmal :)-----------------
'-------------------------------------------------------------------------------------
    'Block3
    Do
        Spalte(6) = Int(3 * Rnd())
        If ((SPZeile(0) = 0 And SPZeile(3) = 9) Or (SPZeile(0) = 9 And SPZeile(3) = 0)) Then MULTI = 18
        If ((SPZeile(0) = 9 And SPZeile(3) = 18) Or (SPZeile(0) = 18 And SPZeile(3) = 9)) Then MULTI = 0
        If ((SPZeile(0) = 0 And SPZeile(3) = 18) Or (SPZeile(0) = 18 And SPZeile(3) = 0)) Then MULTI = 9
        'Do
         '   MULTI = Int(3 * Rnd()) * 9
        'Loop While ((MULTI = SPZeile(0)) Or (MULTI = SPZeile(3)))
    Loop While (FELD(Spalte(6) + MULTI + 6) > "")
    FELD(Spalte(6) + MULTI + 6) = ZAEHL
    SPSpalte(6) = Spalte(6)
    SPZeile(6) = MULTI
    
    
    
        FORT = FORT + 1.25
    Text2.Text = FORT
    'Block6
    Do
        Do
            Spalte(7) = Int(3 * Rnd())
        Loop While Spalte(7) = SPSpalte(6)
        If ((SPZeile(1) = 0 And SPZeile(4) = 9) Or (SPZeile(1) = 9 And SPZeile(4) = 0)) Then MULTI = 18
        If ((SPZeile(1) = 9 And SPZeile(4) = 18) Or (SPZeile(1) = 18 And SPZeile(4) = 9)) Then MULTI = 0
        If ((SPZeile(1) = 0 And SPZeile(4) = 18) Or (SPZeile(1) = 18 And SPZeile(4) = 0)) Then MULTI = 9

'        Do
 '           MULTI = Int(3 * Rnd()) * 9
  '      Loop While ((MULTI = SPZeile(1)) Or (MULTI = SPZeile(4)))
    Loop While (FELD(Spalte(7) + 27 + MULTI + 6) > "")
    FELD(Spalte(7) + 27 + MULTI + 6) = ZAEHL
    SPSpalte(7) = Spalte(7)
    SPZeile(7) = MULTI
    
    
        FORT = FORT + 1.25
    Text2.Text = FORT
    'Block8
    If ((SPSpalte(6) = 0 And SPSpalte(7) = 1) Or (SPSpalte(6) = 1 And SPSpalte(7) = 0)) Then Spalte(8) = 2
    If ((SPSpalte(6) = 1 And SPSpalte(7) = 2) Or (SPSpalte(6) = 2 And SPSpalte(7) = 1)) Then Spalte(8) = 0
    If ((SPSpalte(6) = 0 And SPSpalte(7) = 2) Or (SPSpalte(6) = 2 And SPSpalte(7) = 0)) Then Spalte(8) = 1
    
    If ((SPZeile(2) = 0 And SPZeile(5) = 9) Or (SPZeile(2) = 9 And SPZeile(5) = 0)) Then MULTI = 18
    If ((SPZeile(2) = 9 And SPZeile(5) = 18) Or (SPZeile(2) = 18 And SPZeile(5) = 9)) Then MULTI = 0
    If ((SPZeile(2) = 0 And SPZeile(5) = 18) Or (SPZeile(2) = 18 And SPZeile(5) = 0)) Then MULTI = 9
    'Multi - Werte:
    '0   ;    9    ;    18
    'Do
    '    Do 'ok
    '        MULTI = Int(3 * Rnd()) * 9
    '    Loop While ((MULTI = SPZeile(2)) Or (MULTI = SPZeile(5)))
    '    Do
    '            Spalte(8) = Int(3 * Rnd())
    '    Loop While ((Spalte(8) = SPSpalte(6)) Or (Spalte(8) = SPSpalte(7)))
    'Loop While Text1(Spalte(8) + 54 + MULTI + 6).Text > ""
    
    FELD(Spalte(8) + 54 + MULTI + 6) = ZAEHL
    SPSpalte(8) = Spalte(8)
    SPZeile(8) = MULTI
    
        FORT = FORT + 1.25
    Text2.Text = FORT
Next ZAEHL
AUSGABE
'-------------------------------------------------------------------------------------
'----------Die 3 Rechten Blöcke mit einer 1 Füllen Klappt schonmal :)-----------------
'-------------------------------------------------------------------------------------
Label4.Caption = Time
End Sub

Sub AUSGABE()
    Dim WELCHE
    For WELCHE = 0 To 80
        Text1(WELCHE) = FELD(WELCHE)
    Next WELCHE
End Sub

Private Sub Form_Initialize()
    Randomize   'Dafür sorgen das nicht immer zu Anfang die selben Zufallszahlen kommen
    For laufi = 0 To 80
        FELD(laufi) = ""
    Next laufi

End Sub

So wie ich das sehe und auch Test zeigen das, dass ganze auch klappt. Nur kommt nach dem das Programm eine bestimmte Zahl zum eintragen erreicht hat. Die Meldung das, dass Programm sich aufhängt und nicht komplett durchläuft. Daher denke ich kann man es noch weiter optimieren damit ich dann auch alle 81 Felder des Spiels gefüllt bekomme. Was habt ihr nun für Vorschläge was ich besser machen kann?

Testete gerade nochmal:
Läuft die For Schleife nur bis 5 klappt alles sofort , stelle ich diese aber auf 6 da kommt das Problem schon wieder auf.

Gruß Piatch
 
Zuletzt bearbeitet:
Zurück
Oben