• Skip to main content
  • Skip to primary sidebar

Phelela

  • Phelela
  • Engineer Path
  • My Project
  • Home Office
  • Day Off
  • My GitHub

How I Handle Puzzle Solving and Validation: Generating a Sudoku Grid in Kotlin

24. June 2025

It has been some time since I wrote about my project on this blog. A few days ago, I was working on features for another project, and then I caught the flu. I was so tired and sick that I spent some time watching the Supernatural series. It was good.

This week, I’m back on track and feeling better—not great, but better. I am finishing my Android Sudoku application, so I plan a series of articles about Sudoku and my project. Last time, I wrote about the backtracking algorithm. Today, this article will focus on Sudoku solving and validation. So—stay tuned!

Overview

When working on logic-based games, Sudoku stands out as a great puzzle to challenge the player and test my algorithmic thinking. In this post, I’ll walk you through how I approached generating a complete Sudoku puzzle using Kotlin, focusing on the structure, validation, and recursive solving strategy behind it.

The Goal: Generate a Complete Sudoku Grid

My goal is simple and clear: to generate a Sudoku grid that satisfies these rules:

  1. Each row must contain numbers 1–9 without repetition.
  2. Each column must contain numbers 1–9 without repetition.
  3. Each 3×3 box must contain numbers 1–9 without repetition.

To do this, I used backtracking, a classic technique for solving constraint satisfaction problems. I discussed backtracking in my previous article, but today I’ll add some code examples.

Generating the Sudoku Grid

The code creates a simple, empty 9×9 matrix, then calls the fillSudokuGrid(), which uses recursive backtracking to fill the grid.

For each empty cell (initialized as 0), I attempt to place a number between 1 and 9, shuffled randomly to add variability. If the number is valid at that position, we recursively attempt to fill the rest of the grid. If we hit a dead end, we backtrack and try the next number. This process is essentially a depth-first search through the solution space.

Validation Logic

This was my favorite part. A number is only placed if it’s valid, ensured by a function that delegates to three checks:

  • Row check
  • Column check
  • 3×3 box check

These modular checks make validation easy to test and maintain.

Why This Approach Works

The recursive backtracking approach is powerful for puzzles like Sudoku because it:

  • Explores all possible configurations
  • Prunes invalid paths early (thanks to validation)
  • Guarantees a solution if one exists

It does come with performance costs on larger puzzles or if extended for puzzle difficulty tuning, but for generating valid full boards, it’s efficient enough.

First Step in My Sudoku Logic

This is just the first step in the Sudoku logic. It’s one of the UseCases in my Sudoku application for Android, which you can find here: Neurodoku on GitHub.

Conclusion

By using recursive backtracking and modular validation, I built a Sudoku puzzle generator that guarantees correctness. The algorithm is clear, testable, and surprisingly elegant in its logic.

If you’re working on puzzles, constraint satisfaction, or logic games, this structure might be useful for your own projects.

in My Project

Reader Interactions

you may also like
Backtracking in Action – Think Like an Engineer
Why is Sudoku Good for me? – 7 Benefits for Brain Health

Trackbacks

  1. 5 Productivity Apps That Keep Me Focused as a Remote Mobile App Engineer says:
    26. June 2025 at 16:34

    […] If you enjoyed this roundup, you might like my recent post on How I Handle Puzzle Solving and Validation: Generating a Sudoku Grid in Kotlin. […]

    Reply

Leave a Reply to 5 Productivity Apps That Keep Me Focused as a Remote Mobile App Engineer Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Katarína Kováčová

Hi, I’m Katarina. I'm a backend software engineer. My specialization is Android & Backend development. I started this blog as a space to share my journey in tech. It also serves as a portfolio of my work and a place for exciting topics.

When I’m not coding, I get lost in the fascinating world of Sudoku. Welcome to my corner of the internet.

My life in pictures

This year will be mine. I belive I will beat my obesity.
My first work with GCP – I was on a Cloud 9.
I really adore old cars. They are elegant and beautiful.
Need some rest and a good book in Spanish. Maybe Harry Potter?
I started with regular running and I feel incredible.
2025 © Phelela
theme by soleilflare