Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to fill a 3x3 grid to be a valid Sudoku grid in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

' To fill a 3x3 grid to be a valid Sudoku grid, you must ensure that each row, 
' column, and the 3x3 grid contains the numbers 1 through 9 without repetition.

Module SudokuGridGenerator
    Private Const SIZE As Integer = 3

    ' Function to shuffle an array
    Private Sub Shuffle(ByRef numbers As List(Of Integer))
        Dim rnd As New Random()
		
        For i As Integer = numbers.Count - 1 To 1 Step -1
            Dim j As Integer = rnd.Next(i + 1)
            Dim temp As Integer = numbers(i)
            numbers(i) = numbers(j)
            numbers(j) = temp
        Next
    End Sub

    ' Function to fill the Sudoku grid
    Private Sub FillSudokuGrid(ByRef grid As Integer(,))
        Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9}

        ' Shuffle the numbers randomly
        Shuffle(numbers)

        ' Fill the 3x3 grid row by row
        Dim index As Integer = 0
        For i As Integer = 0 To SIZE - 1
            For j As Integer = 0 To SIZE - 1
                grid(i, j) = numbers(index)
                index += 1
            Next
        Next
    End Sub

    ' Function to print the grid
    Private Sub PrintGrid(ByVal grid As Integer(,))
        For i As Integer = 0 To SIZE - 1
            For j As Integer = 0 To SIZE - 1
                Console.Write(grid(i, j) & " ")
            Next
            Console.WriteLine()
        Next
    End Sub

    Sub Main()
        ' Initialize an empty 3x3 grid
        Dim grid(SIZE - 1, SIZE - 1) As Integer

        ' Fill the grid with a valid Sudoku configuration
        FillSudokuGrid(grid)

        ' Print the grid
        Console.WriteLine("Generated 3x3 Sudoku Grid:")
        PrintGrid(grid)
    End Sub
End Module


 
' run
'
' Generated 3x3 Sudoku Grid:
' 5 3 7 
' 8 6 2 
' 4 9 1 
'

 



answered Jun 1, 2025 by avibootz
edited Jun 1, 2025 by avibootz

Related questions

...