GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - eastyou/nonogram: 数织谜题生成器:唯一解校验、行列提示与可打印题面

BranchesTags
Open more actions menu

Latest commit

 

History

2 Commits
2 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
examples
examples
 
 
nonogram
nonogram
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
pyproject.toml
pyproject.toml
 
 

Repository files navigation

nonogram · 数织谜题生成器

CI License: MIT Python 3.9+

面向教师课堂与学生作品集的数织(Nonogram / Picross)谜题生成器。输入尺寸与随机种子,即可得到唯一解的数织谜题,并导出可直接打印的 A4 题面与答案页。

  • 纯 Python 标准库实现,零第三方依赖
  • 自动校验谜题唯一解,避免课堂出现“多解”歧义
  • 输出 A4 HTML:题面留白供涂色,答案页黑格填充
  • 每 5 格加粗分隔线,行列提示自动排版
  • 命令行与 Python API 双入口
  • 完整参数校验与中文错误提示

安装

要求 Python 3.9 及以上。

python3 -m pip install -e .

也可以不安装,直接在仓库根目录运行:

python3 -m nonogram --help

命令行用法

nonogram --size 10 --seed 5 --title "课堂练习" -o classroom.html

参数说明:

参数 说明 默认值
-o, --output 输出 HTML 文件路径 nonogram.html
--size 谜题尺寸,范围 5–15 10
--density 黑格密度,范围 0.3–0.75 0.55
--seed 随机种子,相同种子结果一致 随机
--no-answers 只输出题面,不输出答案页 关闭
--title 题目标题 数织
-h, --help 显示帮助 —

退出码:

  • 0:生成成功
  • 1:文件写入失败(路径无权限、目标是目录等)
  • 2:参数非法或无法生成唯一解谜题(错误信息输出到 stderr)

示例:

# 生成 10×10、种子 5 的谜题,附带答案页
nonogram --size 10 --seed 5 -o examples/nonogram.html

# 学生版:只保留题面
nonogram --size 8 --seed 42 --no-answers -o student.html

Python API

from nonogram.model import generate_puzzle, row_clues, col_clues
from nonogram.render import render_document

puzzle = generate_puzzle(size=10, seed=5)
html = render_document(puzzle, "课堂练习", show_answers=True)

with open("puzzle.html", "w", encoding="utf-8") as handle:
    handle.write(html)

主要接口:

  • generate_puzzle(size, seed=None, density=0.55) -> Puzzle
  • row_clues(puzzle) -> list[tuple[int, ...]]
  • col_clues(puzzle) -> list[tuple[int, ...]]
  • count_solutions(clues_rows, clues_cols, size, limit=2) -> int
  • render_puzzle(puzzle, title="数织") -> str
  • render_answers(puzzle) -> str
  • render_document(puzzle, title="数织", show_answers=True) -> str
  • is_solved(puzzle, grid) -> bool

所有非法参数都会抛出 NonogramError(ValueError 的子类)。

生成算法

  1. 按给定密度随机生成黑白棋盘;
  2. 计算每行、每列的连续黑格提示;
  3. 使用行列提示驱动的回溯求解器统计解的数量,最多找到 2 个解即提前停止;
  4. 只有恰好 1 个解时才接受该棋盘;
  5. 同一密度最多尝试 200 次,之后自动提高或降低密度重试,仍失败则抛出中文异常。

求解器按行推进,并为每一列维护提示自动机状态,利用“剩余格子是否足够完成提示”进行剪枝,因此在 5–15 尺寸下响应迅速。

项目结构

nonogram/
├── nonogram/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py       # 命令行解析与退出码
│   ├── model.py     # 谜题模型、提示、求解与生成
│   └── render.py    # A4 HTML 渲染
├── tests/
│   ├── test_cli.py
│   ├── test_model.py
│   └── test_render.py
├── examples/
├── .github/workflows/ci.yml
├── pyproject.toml
└── README.md

开发与测试

python3 -m unittest discover -s tests -v

测试覆盖提示计算、唯一解校验、生成确定性、边界校验、HTML 转义、A4 排版与命令行退出码。

许可证

MIT License,Copyright (c) 2026 eastyou。

About

数织谜题生成器:唯一解校验、行列提示与可打印题面

Topics

clieducationlogic-puzzlenonogrampicrossprintablepython

Resources

Readme
MIT license
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.