GithubHelp home page GithubHelp logo

sudoku's Introduction

Sudoku Solver AI

This project aims to create and solve Sudoku as a Constraint Satisfaction Problem.

Sudoku is a logic-based, combinatorial number-placement puzzle. The objective is to fill a 9ร—9 grid with digits so that each column, each row, and each of the nine 3ร—3 subgrids that compose the grid (also called "boxes", "blocks", or "regions") contain all of the digits from 1 to 9. The puzzle setter provides a partially completed grid, which for a well-posed puzzle has a single solution.

๐ŸŒŸ About the Project

๐Ÿ“ท Screenshots

image image image

๐Ÿงฐ Getting Started

โ€ผ๏ธ Prerequisites

๐Ÿ“œ Code of Conduct

In this project, there are two different algorithms used to solve the Sudoku: Simple Backtracking and Backtracking with Forward-Checking.

Here's a breakdown of the code function by function:

generate_sudoku: This function generates a Sudoku puzzle. It first fills the diagonal boxes of the grid with random numbers, then solves the grid, and finally removes a random number of elements from the grid to create the puzzle. The sudoku is generated and ready to be solved.

fill_diagonal: This function fills the diagonal boxes of the Sudoku grid.

fill_box: This function fills a 3x3 box with random values.

is_safe: This function checks if it's safe to place a number in a particular position. It checks if the number is already used in the same row, column, or 3x3 box.

used_in_row: This function checks if a number is used in a row.

used_in_column: This function checks if a number is used in a column.

used_in_box: This function checks if a number is used in a 3x3 box.

find_unassigned_location: This function finds an unassigned location in the grid.

remove_elements: This function removes a certain number of elements from the grid.

solve_sudoku: This function solves the Sudoku puzzle using backtracking. It finds an unassigned location in the grid, then for each value from 1 to 9, it checks if it's safe to place the value at that location. If it is, it places the value and recursively tries to solve the rest of the grid. If it can't solve the grid with that value, it backtracks and tries the next value.

display_grid: This function displays the Sudoku grid.

solve_sudoku_forward_checking: This function solves the Sudoku puzzle using forward checking that is a common filtering. It first creates domains for each cell in the grid, which are the possible values that can be placed in that cell. Then it finds an unassigned location in the grid and for each value in the domain of that location, it checks if it's a valid assignment. If it is, it assigns the value and recursively tries to solve the rest of the grid. If it can't solve the grid with that value, it removes the value from the domain and tries the next value.

initializing_grid: This function generates and displays the initial Sudoku grid.

backtracking_answer: This function solves the Sudoku grid using backtracking and displays the result.

backtracking_forward_checking_answer: This function solves the Sudoku grid using backtracking with forward checking and displays the result.

Main: The main part of the script generates a Sudoku puzzle, makes two copies of it, and then solves it using both simple backtracking and backtracking with forward checking. It prints the initial Sudoku grid, the solved grid, and the number of steps taken to solve the grid for both methods.

โš ๏ธ License

Distributed under the MIT License.

This code is provided by ErfanXH and all rights are reserved.

Project Link: https://github.com/ErfanXH/Sudoku-AI

sudoku's People

Contributors

erfanxh avatar

Watchers

 avatar

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.