Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/morvanzhou/sudoku
A simple Sudoku generator and solver. 数独生成器和解题器
https://github.com/morvanzhou/sudoku
sudoku sudoku-checker sudoku-generator sudoku-solution-finder sudoku-solver
Last synced: 1 day ago
JSON representation
A simple Sudoku generator and solver. 数独生成器和解题器
- Host: GitHub
- URL: https://github.com/morvanzhou/sudoku
- Owner: MorvanZhou
- License: mit
- Created: 2019-01-09T18:28:56.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2023-01-13T03:36:18.000Z (almost 2 years ago)
- Last Synced: 2024-12-25T18:08:35.101Z (9 days ago)
- Topics: sudoku, sudoku-checker, sudoku-generator, sudoku-solution-finder, sudoku-solver
- Language: Python
- Homepage:
- Size: 298 KB
- Stars: 169
- Watchers: 5
- Forks: 26
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# A Sudoku generator and solver
This is a Sudoku game written in python, default to use Wave Function Collapse method.
## Install
```shell
pip install sudokum
```# Generation
A sudoku can be generated with a `mask_rate`. A full solution is generated when passing `mask_rate=0`.
```python
import sudokumg = 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 animating Wave Function Collapse in sudoku generation.
```python
import sudokumsudokum.display_wfc()
```![auto](https://github.com/MorvanZhou/sudoku/raw/main/demo/sudoku_auto.gif)
Or you can get you hand on it by running following code.
```python
import sudokumsudokum.display_wfc(human=True)
```![human](https://github.com/MorvanZhou/sudoku/raw/main/demo/sudoku_human.gif)
# Solving
By using `sudokum.solve()` function, it tries to solve the puzzle.
```python
import sudokumg = 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 (default to 10) can set as following:
```python
import sudokums = sudokum.solve(g, max_try=3)
```## Manually check solution
If you have your own solution, you can validate it by following function:
```python
import sudokumok, position_of_problems = sudokum.check(g)
```# More Example
More use cases can be found in my [test files](https://github.com/MorvanZhou/sudoku/tree/main/tests).