I use this blog as a soap box to preach (ahem... to talk :-) about subjects that interest me.

Wednesday, July 13, 2011

Java - Formatting a Sudoku for the Web

I just wrote a small method in Java that somebody might find useful. It converts a Sudoku string into HTML.
That is, it converts something like this:

074000610695802743310764089730040025560000034049000870007406300000179000000050000

into something like this:


Here is the code of the method:



I am sorry, but I can only display the source code as an image, because blogspot gets confused with the HTML tags.  This shouldn't happen, because I am pasting the HTML code in compose mode.  Their application should be able to escape the critical characters, but it doesn't. Very annoying.  I wasted at least half an hour trying to escape the greather-than signs manually, but it doesn't let me do it...

In any case, the method creates an HTML file that conforms to W3C’s XHTML 1.0 and CSS level 2.1.

To do the formatting of the output, it defines a different style class for each cell of a box, from top-left (c0, with thick top and left borders) to bottom right (c8, with thick right and bottom borders). It then determines the correct style class by calculating the position of each cell (from 0 to 8) with the formula (kR % 3 * 3 + kC % 3).

I know: it is nothing special for somebody who is familiar with stylesheets and Java. Still...

No comments:

Post a Comment