Skip to main content

python sudoku generator and solver

Project description

A Sudoku generator and solver

This is a Sudoku game written in python, default to use Wave Function Collapse method.

A puzzle can be generated by following function with a mask rate.

Install

pip install sudokum

Generation

A sudoku can be generated with a mask_rate. A full solution is generated when passing mask_rate=0.

import sudokum

g = sudokum.generate(mask_rate=0.7)
print(g)
"""
[[1 0 0 5 0 0 0 0 3]
 [0 3 7 0 0 1 0 0 4]
 [0 0 0 0 0 2 0 0 0]
 [0 0 0 0 9 4 0 0 0]
 [0 0 4 0 0 0 0 3 2]
 [0 0 0 3 2 5 0 0 0]
 [0 0 0 0 0 0 2 0 0]
 [7 4 0 0 0 0 0 0 0]
 [8 0 0 0 0 3 1 0 0]]
"""

To visualize the generating procedure, I have made a GUI for animate Wave Function Collapse in sudoku generation.

import sudokum
sudokum.display_wfc()

auto

Or you can get you hand on it by running following code.

import sudokum
sudokum.display_wfc(human=True)

human

Solving

By using solver() function, it tries to solve the puzzle.

import sudokum

g = sudokum.generate(mask_rate=0.7)
s = sudokum.solve(g)
print(s)
"""
[[1 2 8 5 4 7 6 9 3]
 [6 3 7 9 8 1 5 2 4]
 [4 9 5 6 3 2 7 1 8]
 [2 1 3 7 9 4 8 5 6]
 [5 7 4 1 6 8 9 3 2]
 [9 8 6 3 2 5 4 7 1]
 [3 5 1 4 7 6 2 8 9]
 [7 4 2 8 1 9 3 6 5]
 [8 6 9 2 5 3 1 4 7]]
"""

A solving max try number can set as following:

import sudokum

s = sudokum.solve(g, max_try=3)

Manually check solution

If you have your own solution, you can validate it by following function:

import sudokum

ok, position_of_problems = sudokum.check(g)

More Example

More use cases can be found in my test files.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sudokum-0.0.1.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

sudokum-0.0.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file sudokum-0.0.1.tar.gz.

File metadata

  • Download URL: sudokum-0.0.1.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.13

File hashes

Hashes for sudokum-0.0.1.tar.gz
Algorithm Hash digest
SHA256 7f0dddf63a1f96f480fb5b07dd4573fb9d44a3fab3f1385a1f1e5a3b84df9530
MD5 50afdecd5109a02a11168e84db0af7ae
BLAKE2b-256 cefc0ebab15245ccf0d695cf7bd7eda620348cba3391d401bb5c72f60a1f7c6b

See more details on using hashes here.

File details

Details for the file sudokum-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: sudokum-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.13

File hashes

Hashes for sudokum-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 82c6ec4db986521585084a69a4b1ee6fb601a17fa11eb45aeb5370e9dcb49dc4
MD5 4de78e357953a8dbff0b1dc174d6960f
BLAKE2b-256 97f60be829800a5afcb79ffad35f981a3aed1884e750f7052d6c910f3cf7d643

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page