Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ language: python
python:
- "2.6"
- "2.7"
- "3.2"
- "3.3"
- "3.4"
- "3.5"
install:
- "pip install -r requirements.txt"
- "python setup.py install"
script: py.test
email: false
email: false
46 changes: 46 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: example.json",
"type": "python",
"request": "launch",
"program": "example.py",
"console": "integratedTerminal",
"args": [
"json",
"--json", "example.json"
]
},
{
"name": "Python: example csv with svg",
"type": "python",
"request": "launch",
"program": "example.py",
"console": "integratedTerminal",
"args": [
"--svgout", "example.svg",
"csv",
"--columncsv", "example.columns.csv",
"--rowcsv", "example.rows.csv"
]
},
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
}
]
}
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"python.unitTest.pyTestArgs": [
"tests"
],
"python.unitTest.unittestEnabled": false,
"python.unitTest.nosetestsEnabled": false,
"python.unitTest.pyTestEnabled": true,
"restructuredtext.confPath": ""
}
19 changes: 17 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,29 @@ This solver can be used to create nonogram puzzles given a successful final solu
:target: https://travis-ci.org/mprat/nonogram-solver
:alt: Build status

Example Usage
-----------------------
See example.py for sample use of the library.

.. code:: bash

python3 example.py json --json example.json
python3 example.py --svgout example.svg csv --columncsv example.columns.csv --rowcsv example.rows.csv

SVG Example:

.. image:: example.svg
:height: 420
:width: 305


Installation
--------
-----------------------
To install, run `pip install nonogram-solver`.


Project Website
---------
-----------------------
PyPI: `https://pypi.python.org/pypi/nonogram-solver <https://pypi.python.org/pypi/nonogram-solver>`_

Github: `https://github.com/mprat/nonogram-solver <https://github.com/mprat/nonogram-solver>`_
8 changes: 8 additions & 0 deletions example.columns.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

9
9
2,2
2,2
4
4

25 changes: 25 additions & 0 deletions example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"rows":[
[],
[4],
[6],
[2,2],
[2,2],
[6],
[4],
[2],
[2],
[2],
[]
],
"columns":[
[],
[9],
[9],
[2,2],
[2,2],
[4],
[4],
[]
]
}
93 changes: 93 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import numpy as np
from nonogram_solver import solver
from nonogram_solver.nonogram import Nonogram
import argparse

parser = argparse.ArgumentParser(description="Nonogram solver")
parser.add_argument("--verbose",action='store_true')
parser.add_argument("--addcontraints",action='store_true',help='add_puzzle_constraints arg to solver')
parser.add_argument("--svgout",help='svgoutfile',type=str)
subparsers=parser.add_subparsers(title='subcommands',dest='subparser_name')
jsongroup = subparsers.add_parser('json',help='JSON file input')
jsongroup.add_argument("--json", help="JSON File Path", required=True, type=str)
csvgroup = subparsers.add_parser('csv',help='CSV file input')
csvgroup.add_argument("--columncsv", help="Column Constraint File Path", required=True, type=str)
csvgroup.add_argument("--rowcsv", help="Row Constraint File Path", required=True, type=str)

args = parser.parse_args()

if not args.subparser_name:
parser.print_help()
exit(2)

nonogram = Nonogram()
nonogram.rows_constraints = []
nonogram.cols_constraints = []
nonogram.solution_list=[]

if args.subparser_name == 'json':
import json
with open(args.json) as jsonfile:
puzzle = json.load(jsonfile)
if not puzzle['rows'] or not puzzle['columns']:
print("No column or row info")
exit(1)
nonogram.n_cols=len(puzzle['columns'])
nonogram.n_rows=len(puzzle['rows'])
for c in puzzle['columns']:
nonogram.cols_constraints.append(c)
for r in puzzle['rows']:
nonogram.rows_constraints.append(r)

if args.subparser_name == 'csv':
import csv
with open(args.columncsv, newline='') as columncsv:
columns = csv.reader(columncsv, quoting=csv.QUOTE_NONNUMERIC)
for row in columns:
nonogram.cols_constraints.append(list(map(int, row)))
nonogram.n_cols=len(nonogram.cols_constraints)
with open(args.rowcsv, newline='') as rowcsv:
rows = csv.reader(rowcsv, quoting=csv.QUOTE_NONNUMERIC)
for row in rows:
nonogram.rows_constraints.append(list(map(int, row)))
nonogram.n_rows=len(nonogram.rows_constraints)

import io
from contextlib import redirect_stdout

verbose = io.StringIO()
with redirect_stdout(verbose):
solveable, nonogram_solver = solver.solve(nonogram, add_puzzle_constraints=args.addcontraints)

if args.verbose:
print(verbose.getvalue())
if solveable:
print("The puzzle was solvable")
else:
print("The puzzle was not solvable")

#import pprint
#pp = pprint.PrettyPrinter(indent=4)
#pp.pprint(nonogram_solver.puzzle_state)

for r in nonogram_solver.puzzle_state:
print(*list(map(lambda x: '█' if x==1 else ' ', r)),sep='')


if args.svgout:
import svgwrite
from svgwrite import cm
dwg = svgwrite.Drawing(args.svgout, profile='tiny')
shapes = dwg.add(dwg.g(id='shapes', fill='white'))

rc=0
for r in nonogram_solver.puzzle_state:
cc=0
for c in r:
if c == 1:
shapes.add(dwg.rect(insert=(cc*cm, rc*cm), size=(1*cm, 1*cm), fill='black', stroke='black', stroke_width=1))
else:
shapes.add(dwg.rect(insert=(cc*cm, rc*cm), size=(1*cm, 1*cm), fill='white', stroke='black', stroke_width=1))
cc=cc+1
rc=rc+1
dwg.save()
11 changes: 11 additions & 0 deletions example.rows.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

4
6
2,2
2,2
6
4
2
2
2

2 changes: 2 additions & 0 deletions example.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
numpy
numpy
svgwrite
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5'
Expand Down