Hallo,
ich habe ein (vermutlich) JavaScript, das würde ich gerne in VB.NET übersetzen...
Allerdings benötige ich etwas Hilfe...
Mit online-Convertern war ich nicht erfolgreich.
Das JavaScript ist zum Lösen von Sudokus.
Ist es erwünscht dass ich das JavaScript und meinen Versuch in VB.NET poste?
Vielen Dank!
hier das JavaScript:
----------------------------------------------------------------------------------------
hier mein VB.NET-Versuch:
kwon
*Topic verschoben, korrekte Code-Tags eingefügt*
ich habe ein (vermutlich) JavaScript, das würde ich gerne in VB.NET übersetzen...
Allerdings benötige ich etwas Hilfe...
Mit online-Convertern war ich nicht erfolgreich.
Das JavaScript ist zum Lösen von Sudokus.
Ist es erwünscht dass ich das JavaScript und meinen Versuch in VB.NET poste?
Vielen Dank!
hier das JavaScript:
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 sudokuGrid = [
- [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(sudokuGrid));
----------------------------------------------------------------------------------------
hier mein VB.NET-Versuch:
VB.NET-Quellcode
- Public Class Form1
- Dim row As Integer, col As Integer, num As Integer
- Dim checkBlankSpaces As Boolean
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- End Sub
- Function sudokuSolver(matrix As Integer) As Integer
- Dim a As String
- If (solveSudoku(matrix) = True) Then
- Return matrix
- End If
- a = "NO SOLUTION"
- Return a
- End Function
- Function solveSudoku(matrix As Integer)
- row = 0
- col = 0
- checkBlankSpaces = False
- For row = 1 To 9
- For col = 1 To 9
- If (matrix[row][col] = 0) Then
- checkBlankSpaces = True
- break
- End If
- Next
- If (checkBlankSpaces = True) Then
- break
- End If
- Next
- If (checkBlankSpaces = False) Then
- Return True
- End If
- For num = 1 To 9
- If (isSafe(matrix, row, col, num)) Then
- matrix[row][col] = num
- If (solveSudoku(matrix)) Then
- Return True
- End If
- matrix[row][col] = 0
- End If
- Next
- Return False
- End Function
- Function isSafe(matrix, row, col, num)
- Return (!usedInRow(matrix, row, num) & !usedInCol(matrix, col, num) &
- !usedInBox(matrix, row - (row % 3), col - (col % 3), num)
- End Function
- Function usedInRow(matrix, row, num)
- For (col = 0; col < matrix.length; col++) {
- If (matrix[row][col] === num) {
- Return True
- End If
- Next
- Return False
- End Function
- Function usedInCol(matrix, col, num)
- For (row = 0; row < matrix.length; row++
- If (matrix[row][col] === num) {
- Return True;
- End If
- Return False
- End Function
- Function usedInBox(matrix, boxStartRow, boxStartCol, num) {
- For (row = 0; row < 3; row++) {
- For (col = 0; col < 3; col++) {
- If (matrix[row + boxStartRow][col + boxStartCol] === num) {
- Return True
- End If
- Next
- Next
- Return False
- End Function
- Const sudokuGrid = [[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(sudokuGrid))
- End Class
kwon
*Topic verschoben, korrekte Code-Tags eingefügt*
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Marcus Gräfe“ ()