77import os
88import re
99from picross .grid import Grid
10- from picross .solver import show , solve_puzzle , derive_constraints , read_image_from_file
10+ from picross .solver import show , solve_puzzle , derive_constraints , read_image_from_file , is_valid_puzzle
1111
1212
1313def _parse_constraints (string ):
@@ -93,7 +93,15 @@ def _extract_puzzle_index(filename):
9393 return int (match .group (1 ))
9494
9595
96- def _add_puzzle (archive , author , solution ):
96+ def _add_puzzle (archive , author , solution , verify = True ):
97+ if verify :
98+ print ('Verifying puzzle... (can take a couple of seconds)' )
99+ if not is_valid_puzzle (solution ):
100+ print (f'{ show (solution )} \n \n is not a valid puzzle' )
101+ sys .exit (- 1 )
102+ else :
103+ print ('Puzzle deemed valid!' )
104+
97105 width = solution .width
98106 height = solution .height
99107
@@ -123,8 +131,9 @@ def _add_puzzle_from_solution(args):
123131 archive = args .archive
124132 author = args .author
125133 solution_file = args .solution_file
134+ verify = args .verify
126135 solution = read_image_from_file (solution_file )
127- _add_puzzle (archive , author , solution )
136+ _add_puzzle (archive , author , solution , verify = verify )
128137
129138
130139def _process_command_line_arguments ():
@@ -143,6 +152,7 @@ def create_archive_parsers(subparsers):
143152 subparser .add_argument ('archive' , help = 'archive' , action = 'store' )
144153 subparser .add_argument ('author' , help = 'author' , action = 'store' )
145154 subparser .add_argument ('solution_file' , help = 'file containing solution' , action = 'store' )
155+ subparser .add_argument ('--no-verify' , help = 'do not check puzzle validity' , action = 'store_false' , dest = 'verify' , default = True )
146156 subparser .set_defaults (func = _add_puzzle_from_solution )
147157
148158 subparser = subparsers .add_parser ('add-from-constraints' , help = 'adds puzzle to archive; puzzle specified by constraints' )
0 commit comments