• Latest Code...

    Featured Post

    Implementing Hilt in a Kotlin Android Jetpack Compose Project with MVVM Architecture

     In modern Android development, maintaining a scalable codebase can be challenging, especially when it comes to dependency management. Hilt,...

    Sudoku Puzzle Image with Jetpack Compose

     To create a Sudoku puzzle image programmatically in Kotlin, you can use libraries such as Android Canvas or Jetpack Compose. Below is an example using Jetpack Compose, which is a modern toolkit for building Android UI.


    Sudoku Puzzle Image with Jetpack Compose

    This code draws a 9x9 Sudoku grid with some numbers pre-filled.

    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.compose.foundation.Canvas
    import androidx.compose.foundation.layout.*
    import androidx.compose.runtime.Composable
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.graphics.Color
    import androidx.compose.ui.graphics.Paint
    import androidx.compose.ui.text.TextStyle
    import androidx.compose.ui.unit.dp
    import androidx.compose.ui.unit.sp
    import androidx.compose.foundation.text.BasicText
    
    class SudokuActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                SudokuGrid()
            }
        }
    }
    
    @Composable
    fun SudokuGrid() {
        val puzzle = arrayOf(
            charArrayOf('5', '3', '.', '.', '7', '.', '.', '.', '.'),
            charArrayOf('6', '.', '.', '1', '9', '5', '.', '.', '.'),
            charArrayOf('.', '9', '8', '.', '.', '.', '.', '6', '.'),
            charArrayOf('8', '.', '.', '.', '6', '.', '.', '.', '3'),
            charArrayOf('4', '.', '.', '8', '.', '3', '.', '.', '1'),
            charArrayOf('7', '.', '.', '.', '2', '.', '.', '.', '6'),
            charArrayOf('.', '6', '.', '.', '.', '.', '2', '8', '.'),
            charArrayOf('.', '.', '.', '4', '1', '9', '.', '.', '5'),
            charArrayOf('.', '.', '.', '.', '8', '.', '.', '7', '9')
        )
    
        Box(Modifier.fillMaxSize().padding(16.dp)) {
            Canvas(modifier = Modifier.fillMaxSize()) {
                val gridSize = size.width
                val cellSize = gridSize / 9f
    
                val paint = Paint().apply { color = Color.Black }
    
                // Draw the grid lines
                for (i in 0..9) {
                    val lineWidth = if (i % 3 == 0) 6f else 2f
                    drawLine(
                        start = androidx.compose.ui.geometry.Offset(i * cellSize, 0f),
                        end = androidx.compose.ui.geometry.Offset(i * cellSize, gridSize),
                        paint.apply { strokeWidth = lineWidth }
                    )
                    drawLine(
                        start = androidx.compose.ui.geometry.Offset(0f, i * cellSize),
                        end = androidx.compose.ui.geometry.Offset(gridSize, i * cellSize),
                        paint.apply { strokeWidth = lineWidth }
                    )
                }
            }
    
            // Fill the grid with numbers
            for (row in puzzle.indices) {
                for (col in puzzle[row].indices) {
                    val number = puzzle[row][col]
                    if (number != '.') {
                        BasicText(
                            text = number.toString(),
                            style = TextStyle(color = Color.Black, fontSize = 20.sp),
                            modifier = Modifier
                                .offset(x = (col * (size / 9f)).dp, y = (row * (size / 9f)).dp)
                        )
                    }
                }
            }
        }
    }

    How It Works:

    1. Grid Drawing:
      The Canvas composable is used to draw the Sudoku grid. Thicker lines are drawn every three cells to demarcate the 3x3 sub-grids.

    2. Number Placement:
      A nested loop iterates over the puzzle array and places numbers using BasicText at the correct cell coordinates.

    3. Customizations:

      • Grid Size: Automatically adapts to the screen.
      • Font Size and Style: Easily customizable for better visuals.

    Output:

    • A 9x9 Sudoku grid is displayed, with the given puzzle pre-filled.
    • The grid uses clear lines and neatly places numbers in their respective cells.


    This example provides a dynamic, scalable, and modern approach for creating Sudoku images directly in an Android app.

    Contact Form

    Name

    Email *

    Message *