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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to fill a 3x3 grid to be a valid Sudoku grid in Kotlin

1 Answer

0 votes
import kotlin.random.Random

// 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.

fun fillSudokuGrid(): Array<Array<Int>> {
    val numbers = (1..9).toList().shuffled()

    // Initialize a 3x3 grid
    val grid = Array(3) { Array(3) { 0 } }
    var index = 0

    for (i in 0 until 3) {
        for (j in 0 until 3) {
            grid[i][j] = numbers[index++]
        }
    }
    return grid
}

fun printGrid(grid: Array<Array<Int>>) {
    grid.forEach { row ->
        println(row.joinToString(" "))
    }
}

fun main() {
    val grid = fillSudokuGrid()
    println("Generated 3x3 Sudoku Grid:")
    printGrid(grid)
}

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

 



answered Jun 1, 2025 by avibootz

Related questions

...