I’m currently writing a graphical sudoku solver program in Java using Swing/Awt. So I decided I wanted to shade the boxes (marked with thicker lines) on the board the way these are (with 3×3 and 2×3 boxes):

(source: zitowolf.net)

(source: zitowolf.net)
A 6×6 board and a 8×8 board
A board in the program is basically represented as an int[dimension][dimension] array and the the program accepts boards with dimensions from 4×4 -> 16×16.
The problem is to make sure no boxes next to each other in a row or column has the same shading. The GUI shading process itself is not the problem, but finding out whether a square should be shaded as part of a shaded box.
I’ve tried for hours to implement a simple algorithm for this problem going through the array with a double for-loop, but I haven’t gotten any reasonable pattern yet and I’m out of ideas. My algorithm so far is based on the assumption that every other box should be colored from top left to right bottom. This doesn’t work for the 6×6 variant because there you have the sequence: colored box->white box->white box.
Could anyone here suggest an algorithm or another approach? I’ll provide more info if needed. Much appreciated.
1 Answer