Giter Club home page Giter Club logo

android-sudoku-solver's Introduction

android-sudoku-solver

A simple sudoku solver for exercise

The board view were taken from here with some modifications for the required UI.

The generator and solver algorithm were taken hrom here with slight modification on generator. On the original generator it needs to check if the whole field is solvable while removing digits one by one, which sometimes it took too long to generate (more than 5 minutes or so). So I modified it by removing the solvable condition and let the user know if the generated board is unsolvable.

You can try the app here.

The program is written in Kotlin and uses Architecture Components.

Generator

The generator algorithm is divided into 3 parts

Filling diagonal boxes

Sudoku puzzle has 3 diagonal 3x3 boxes that share neither common rows nor columns. Because of that, it’s super safe to fill them first with digits from 1 to 9 and to already have 33% of the grid completed.

Filling all remaining cells

Here we are iterating through all empty cells from non-diagonal boxes and taking the random digit from 1 to 9 to check if it’s safe to put it in until we find the safe one.

Removing the desired number of digits

Once we have made a grid with correctly filled digits, we have to remove some of them to complete the Sudoku puzzle. Until the desired number of digits is removed, we take the digit from the random row and column. This is where I made the modification from the original generator to remove solvable checker for every removal of digits.

Solver

The solver uses backtracking algorithm

  • Place the digit "1" in the first cell and check if it is allowed to be there.
  • If there are no violations (checking row, column, and box constraints) then the algorithm advances to the next cell and places a "1" in that cell.
  • When checking for violations, if it is discovered that the "1" is not allowed, the value is advanced to "2".
  • If a cell is discovered where none of the 9 digits is allowed, then the algorithm leaves that cell blank and moves back to the previous cell.
  • The value in that cell is then incremented by one.
  • This is repeated until the allowed value in the last (81st) cell is discovered.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.