Again, we continue my series of posts on the creation of my Sudoku Player. Speaking of which, new puzzles have been added. While the Sudoku Player is technically in a functional state at this point in its development, none of the sidebar buttons work. As most of the functionality that I want is tied to these buttons, it is important to start implementing the features. To me, the most important feature is the mistake system. When on, it color codes the tiles based on if the numbers in the tiles are valid for the current state of the puzzle.
For some reason, most likely being rushed due to the time constraints placed on this project, I refer to the mistakes as hints. the enableHintsMode method in the View class will set the state of the hint system. If mistakes are to be shown, the updateHints method gets called to set the colors otherwise we simply loop through the View’s tiles and set them to the normal color if they are not locked.
Before the updateHints method can be created, some support methods need to be created. These methods are checkRowForValue, checkColForValue, and checkGroupForValue. This is a poor name for these functions as they return a count of how many times the indicated value is found, but I couldn’t think of a more appropriate names at the time. The row and column methods are fairly obvious to write but the group method required a bit of thought. My solution was to use the group number to determine offsets and have a simple pair of loops to check a 3x3 section using the offsets. Here is that function:
Sudoku.SudokuView.prototype.checkGroupForValue = function(g, n)
var cntrRow, cntrCol;
var foundCount = 0;
var rowOffset = Math.floor((g-1) / 3) * 3;
var colOffset = Math.floor((g-1) % 3) * 3;
for (cntrRow = 1; cntrRow < 4; ++cntrRow)
for (cntrCol = 1; cntrCol < 4; ++cntrCol)
if (this.tileGrid[rowOffset+cntrRow][colOffset+cntrCol].getValue() == n)
With the above three methods finished, the updateHints method is very simple to write. It is just a simple loop over all the tiles. For each tile, we add the results of the above three methods when called for the tiles row, column and group using the current value in the tile. If this number is greater than 3 then the number appears more than once in one of the three categories and therefore is not a valid tile.
With this code out of the way, the only thing left to do is to implement the toggle button. Originally I was going to use two buttons (one visible, the other invisible) to switch between the two states, but I quickly realized that the ImageCheckBox class can have button images instead of box images so I combined the two images to create the check box. With that done, the mistakes toggle shows which numbers are valid if the player desires this information. With only a few hours left in the project, it is time to finish things off.