Hallo,
ich habe ein JavaScript, das ich (richtig?) in VB übersetzt habe... Habe es versucht so gut ich konnte...
Auf der Form habe ich ein TableLayoutPanel zum Positionieren der 81 (Multiline) TextBoxen benutzt.
Der Solver gibt natürlich nicht! eine korrekte Lösung in die TextBoxen aus...
Könntet ihr mir Hinweise geben was ich vielleicht falsch übersetzt habe?
Habe versucht zu debuggen...
Könntet ihr mir Hinweise geben wie man so etwas sinnvoll debuggt (irgendwie ist es nicht einfach die Orientierung zu behalten...)?
Hier das JavaScript:
Hier mein Versuch in VB:
Das angefügte Bild der Form soll nur zur Orientierung dienen...
Ich bitte um eure geschätzte Hilfe...
Viele Grüße,
Kwon
ich habe ein JavaScript, das ich (richtig?) in VB übersetzt habe... Habe es versucht so gut ich konnte...
Auf der Form habe ich ein TableLayoutPanel zum Positionieren der 81 (Multiline) TextBoxen benutzt.
Der Solver gibt natürlich nicht! eine korrekte Lösung in die TextBoxen aus...
Könntet ihr mir Hinweise geben was ich vielleicht falsch übersetzt habe?
Habe versucht zu debuggen...
Könntet ihr mir Hinweise geben wie man so etwas sinnvoll debuggt (irgendwie ist es nicht einfach die Orientierung zu behalten...)?
Hier das JavaScript:
Quellcode
- function sudokuSolver(matrix) {
- if (solveSudoku(matrix) === true) {
- return matrix;
- }
- return 'NO SOLUTION';
- }
- const UNASSIGNED = 0;
- function solveSudoku(matrix) {
- let row = 0;
- let col = 0;
- let checkBlankSpaces = false;
- /* verify if sudoku is already solved and if not solved,
- get next "blank" space position */
- for (row = 0; row < matrix.length; row++) {
- for (col = 0; col < matrix[row].length; col++) {
- if (matrix[row][col] === UNASSIGNED) {
- checkBlankSpaces = true;
- break;
- }
- }
- if (checkBlankSpaces === true) {
- break;
- }
- }
- // no more "blank" spaces means the puzzle is solved
- if (checkBlankSpaces === false) {
- return true;
- }
- // try to fill "blank" space with correct num
- for (let num = 1; num <= 9; num++) {
- /* isSafe checks that num isn't already present
- in the row, column, or 3x3 box (see below) */
- if (isSafe(matrix, row, col, num)) {
- matrix[row][col] = num;
- if (solveSudoku(matrix)) {
- return true;
- }
- /* if num is placed in incorrect position,
- mark as "blank" again then backtrack with
- a different num */
- matrix[row][col] = UNASSIGNED;
- }
- }
- return false;
- }
- function isSafe(matrix, row, col, num) {
- return (
- !usedInRow(matrix, row, num) &&
- !usedInCol(matrix, col, num) &&
- !usedInBox(matrix, row - (row % 3), col - (col % 3), num)
- );
- }
- function usedInRow(matrix, row, num) {
- for (let col = 0; col < matrix.length; col++) {
- if (matrix[row][col] === num) {
- return true;
- }
- }
- return false;
- }
- function usedInCol(matrix, col, num) {
- for (let row = 0; row < matrix.length; row++) {
- if (matrix[row][col] === num) {
- return true;
- }
- }
- return false;
- }
- function usedInBox(matrix, boxStartRow, boxStartCol, num) {
- for (let row = 0; row < 3; row++) {
- for (let col = 0; col < 3; col++) {
- if (matrix[row + boxStartRow][col + boxStartCol] === num) {
- return true;
- }
- }
- }
- return false;
- }
- const matrix = [
- [5, 3, 0, 0, 7, 0, 0, 0, 0],
- [6, 0, 0, 1, 9, 5, 0, 0, 0],
- [0, 9, 8, 0, 0, 0, 0, 6, 0],
- [8, 0, 0, 0, 6, 0, 0, 0, 3],
- [4, 0, 0, 8, 0, 3, 0, 0, 1],
- [7, 0, 0, 0, 2, 0, 0, 0, 6],
- [0, 6, 0, 0, 0, 0, 2, 8, 0],
- [0, 0, 0, 4, 1, 9, 0, 0, 5],
- [0, 0, 0, 0, 8, 0, 0, 7, 9]
- ];
- console.log(sudokuSolver(matrix));
Hier mein Versuch in VB:
VB.NET-Quellcode
- Public Class Form1
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- End Sub
- Const UNASSIGNED = 0
- Private Sub loeseButton_Click(sender As Object, e As EventArgs) Handles loeseButton.Click
- Dim i As Integer, j As Integer, n As Integer
- Dim matrix(80, 80) As Integer
- 'Matrix füllen
- n = 1
- For i = 0 to 8
- For j = 0 To 8
- If Me.TableLayoutPanel1.Controls("TextBox" & n).Text <> "" Then
- matrix(i, j) = CInt(Me.TableLayoutPanel1.Controls("TextBox" & n).Text)
- End If
- n += 1
- Next
- Next
- SudokuSolver(matrix)
- End Sub
- Function SudokuSolver(matrix(,) As Integer) As Integer
- Dim n As Integer
- If SolveSudoku(matrix) = True Then
- n = 1
- For i = 0 To 8
- For j = 0 To 8
- Me.TableLayoutPanel1.Controls("TextBox" & n).Text = CStr(matrix(i, j))
- n += 1
- Next
- Next
- MessageBox.Show("Sudoku gelöst.")
- End If
- Return Nothing
- End Function
- Function SolveSudoku(matrix(,) As Integer) As Boolean
- Dim row As Integer, col As Integer, num As Integer
- Dim checkBlankSpaces As Boolean = False
- ' verify if sudoku is already solved and if not solved,
- ' get next "blank" space position
- For row = 0 To 8
- For col = 0 To 8
- If matrix(row, col) = UNASSIGNED Then
- checkBlankSpaces = True
- Exit For
- End If
- Next col
- If checkBlankSpaces = True Then
- Exit For
- End If
- Next row
- ' no more "blank" spaces means the puzzle is solved
- If checkBlankSpaces = False Then
- Return True
- End If
- ' try to fill "blank" space with correct num
- For num = 1 To 9
- 'isSafe checks that num isn't already present
- 'in the row, column, or 3x3 box (see below)
- If IsSafe(matrix, row, col, num) Then
- matrix(row, col) = num
- If SolveSudoku(matrix) = True Then
- Return True
- End If
- 'if num is placed in incorrect position,
- 'mark as "blank" again then backtrack with
- 'a different num
- matrix(row, col) = UNASSIGNED
- End If
- Next num
- Return False
- End Function
- Function IsSafe(matrix(,) As Integer, row As Integer, col As Integer, num As Integer) As Boolean
- Return (Not UsedInRow(matrix, row, num) Or Not UsedInCol(matrix, col, num) Or Not UsedInBox(matrix, row - (row Mod 3), col - (col Mod 3), num))
- End Function
- Function UsedInRow(matrix(,) As Integer, row As Integer, num As Integer) As Boolean
- For col As Integer = 0 To 8
- If matrix(row, col) = num Then
- Return True
- End If
- Next col
- Return False
- End Function
- Function UsedInCol(matrix(,) As Integer, col As Integer, num As Integer) As Boolean
- For row As Integer = 0 To 8
- If matrix(row, col) = num Then
- Return True
- End If
- Next row
- Return False
- End Function
- Function UsedInBox(matrix(,) As Integer, boxStartRow As Integer, boxStartCol As Integer, num As Integer) As Boolean
- For row As Integer = 0 To 2
- For col As Integer = 0 To 2
- If matrix(row + boxStartRow, col + boxStartCol) = num Then
- Return True
- End If
- Next col
- Next row
- Return False
- End Function
- Private Sub löschenButton_Click(sender As Object, e As EventArgs) Handles löschenButton.Click
- Dim ctrl As Control
- For Each ctrl In Me.TableLayoutPanel1.Controls
- If TypeOf ctrl Is TextBox Then
- ctrl.Text = ""
- End If
- Next
- End Sub
- End Class
Das angefügte Bild der Form soll nur zur Orientierung dienen...
Ich bitte um eure geschätzte Hilfe...
Viele Grüße,
Kwon