Readme for !Nonogram

A nonogram is a type of Japanese puzzle with rows and columns of numbers at
two sides of a grid; these numbers indicate the length of consecutive series
of black squares, taht together form a row or a column.
Together, these series of pixels form a picture.
The challenge is, by means of logical reasoning, to deduct this picture from
the numbers.
Between the black dots there are white dots; the length of these series of
white dot sare omitted.
There must be at least one white pixel between series of black pixels; at
the end and the beginning of a row/column, a white pixel is optional but not
required.
The situation resembles a compression method called runlength encoding (RLE);
only, in RLE the length of series of similar pixels is given for black and 
for white pixels alike, and only in one direction; in nonograms, both rows
and columns are given.
This puzzle is known under a wide variety of names, e.g. griddler,japanese 
picture puzzle,paint by number; you'll discover soon enough when you try 
the Internet. 
The program in its current state is not yet very intelligent, e.g. there are
nonograms that it cannot solve yet. The wait is for an algorithm for these
difficult cases to ripen. Also, it cannot handle colour yet and it has no 
redo/undo.


The program consists of these files:
!Boot
!Run
!RunImage (!Runimage' is its compressed form, maybe somewhat faster)
!Sprites
bitmaps
!Templates
this file,!Help
maybe in the future: Messages for interactive help
possibly a directory Puzzles.

How to operate the program:
Unzip it and place it somewhere convenient on your computer.
Doubleclick on its application icon to install it on the iconbar.
There are many methods for entering a puzzle:
(1)
Click on the iconbar icon, a setup window opens, you can choose the
dimensions, if you're satisfied click OK. A grid should open now with
narrow borders left and upper; press Menu over these; if you now select
'EnterByClick' you can click with Adjust and Select to enter values for 
rows and columns. Alternatively, choose 'EnterARow' or 'EnterAColumn' to
enter values directly.
(2) 
Click on the grid itself to paint pixels black or white. There is even
a primitive Rectanglefill for black,white and undetermined: Make a Drag
on the main Grid and see what happens. If all is to your liking, choose
Menu over the main grid and choose RenewBorder (this only works if all
pixels are filled-in, not grey)
(3) 
Drag a suitable textfile on the iconbar icon or the open MainGrid.
Two formats are supported:
(a) First line, two numbers separated by a comma, first the height, then the
width. E.g. 40,30 is a picture of 40 high,30 wide. Empty line; then lines
equalling the number of rows, filled with '-' for a white pixel,X for a black
pixel.
(b) First line, two numbers separated by commas, height and width.
Then the runlengths in the rows, followed by the runlengths in the columns.
(4) 
Drag a suitable sprite(2bit-per-pixel upto 32 bpp) on iconbar icon or
maingrid. A good way to generate these sprites is, get the solved picture
from the PDF file that contains unsolved and/or solved puzzle; export to
!Paint, or make a screengrab with !Paint, from this picture inside the PDF.
Another method is to scan a picture from a book or magazine and, in !Paint,
convert the Jpeg into a sprite.
Make the screengrab as precise as possible. Drag the sprite you made, to
!Nonogram; it opens the Setup window again as !nonogram does not know the 
dimensions. Fill in the dimensions and choose OK. If the sprite does not
convert happily, click on the Sprite icon, (right-upper corner) in the
setupwindow; again you see the sprite. Choose Menu to apply a grid, see if
this grid matches well with the sprite. If not, you can distort the grid
by dragging its 4 red corners; if ready, choose Process, the sprite itself 
will distort. Follow the Level menu entry, most probably you will see some
values in the black region and some in the white region and hopefully one or
more entries with zero. Choose (ie.click) on such an entry with zero-value
to determine the separation-level. If all is well, you can now
click 'Setup Wdw" to go on with the setup window. This more or less elaborate
method is more or less usable for scans and photographs of the solution, as
it can also rotate the picture a bit.
The puzzle is now inside; all you have to do is, to solve it.
A menu entry on the main window lets you solve it directly.
If the picture is still there, it can be discarded of by choosing 
'Clear grid'. A submenu from Solve leads you to a choice how often you
wish to see the intermediate results. While solving, it can be stopped
with StopSolving; most often the result you see contains errors.
To accept intermediate results, or hints, choose Give a hint/ Apply hint
from the menus on upper- and left border; or make sure that the grid has
input focus and operate the arrow keys and the Spacebar on the keyboard. 
To generate hints for the rows or the columns separately, use a menu
entry on upper/leftborder menus; or use the up/down/left/right arrow keys
on the keyboard while the grid has input focus.
Finally you can save out textfiles or sprites using the Save dialogue window,
the type of which depend on the state of the program, ie. finished: --XX file
or sprite; unfinished: CSV-type textfile or sprite of the border.
And of course, you can choose the grid to be big or tiny using the Scale
view submenu on the main grid.

How does it work:
The data are collected in a structure, consisting of words (ie. 4 bytes).
Using these data, a grid of black/white pixels is used to compare these data with.
In the structure 'bytes%', rows or columns are separated by 
a word -1 (&FFFFFFFF); there are (vertical+horizontal) of these 
substructures. Each substructure is a word and represents one element of 
black dots. 5 bits for colour,9 for length,9 for the first position that 
an element can have,9 bits for the last position. At the start, the 
elements have as their first position 0 and their last: (row/columnlength
 minus elementlength). At setup, these positions are calculated better:
first elements startingposition remains 0, the next has 
startingposition+length of previous plus one. 
Likewise, the last-possible positions are calculated from the lastest 
element to the first. If the first-possible position is equal to the 
last-possible position, the element is fixed and can be coloured all
black. If (first-possible-position+length) is equal or greater than 
last-possible-position, the overlapping pixels can also be coloured black.
Pixels that cannot be filled in by any of the black elements, are coloured 
white. This way, you can prepare all the rows. Arriving at the columns, some
gridpixels are already white or black, so that the elements in the columns
have a little less playroom: the first-possible locations shift up, the last-
possible locations shift down.
To fit, all the gridlocations for the element must be either black or 
indeterminate, and the location just below must be either white or 
indeterminate, and the location just on top also white or indeterminate.
IF an element shifts up (lowest-possible location increases) it can leave
back a black pixel; this then must be part of the previous element so that 
the algorithm must go back one step and pull up the previous element.
If there is a black pixel between the last element and the end of the row, 
this last element must increase its first-possible-location to include this
last black pixel. If that implies that another black pixel is left behind, we
must go back to the previous element to include this pixel, etc.
Do the same for the last-possible locations, working from the last element 
to the first down.
To conclude, scan the first/last locations and see which pixels are always
covered by black(-->black pixel) and which are unreachable (-->white pixel)
Repeat this until no more changes occur in the element first/last positions.

If the grid cannot be filled the program selects one pixel with a high
probability of being black. It copies the structure and adds this
black pixel. If it fails (if an error results), the black pixel is made
white and another try is done. The disadvantage of this method is, that
if a wrong guess is made in the beginning, the program must first traverse
the whole recursive tree before the error can be corrected.

Recommended websites: 
(just select one of the URL's, drag the selection to !Netsurf while setting its
filetype to URL, and you're there)

http://jsimlo.sk/griddlers/      including a beautiful program (Windows) 
AND a load of nonograms (sgriddlers) included in the program download!

http://www.turkzeka.com .. lots of nonograms

http://www.janko.at/Raetsel/Nonograms/ 

http://www.conceptispuzzles.com/products/picapix/puzzle_samples.htm

http://en.wikipedia.org/wiki/Nonogram

And there are various magazines dedicated to this kind of puzzle.

Known problems:
No colour-nonograms
No interactive help
No redo/undo
Can spend a long time solving a puzzle (recursion!).


Enjoy

jandeboer@telfort.nl











