Sunday, May 1, 2011

DDTe2 hours 4 to 6 - Hiding Tiles

This is a continuation of my series of articles on the creation of Dozen Days of Tiles Episode 2: Sudoku Generator. There is a sneak preview of the game on BlazingGames.com for those who are interested. The final version of the game is scheduled for release on May 20th.


Taking an existing sudoku solution and turning it into a puzzle may at first glance sound like it would be something tricky, it actually is very simple. When I was creating a puzzle by hand, the method that immediately came to mind was to start with a near blank puzzle and add bits of the final solution to it. Try to solve until the puzzle is solved or until you reach a point where all remaining tiles have more than one possibility. At this point, reveal one of the unsolved tiles and continue solving.

When working with a lot of data manipulation, it is nice to be able to see what is going on. Using a debugger could work but it is much more efficient to dump the data to a log file. While FireBug supports a console, I figured it would be easier and more compatible to create my own console. This is simply done by using a text area.

function dumpPuzzle(data)
{
   var log = document.getElementById('log');
   log.value ="Puzzle:\n";

   var temp;
   for (var cntrRow = 1; cntrRow < 10; ++cntrRow) {
       for (var cntrCol = 1; cntrCol < 10; ++cntrCol) {
           temp = data.getPuzzleValue(cntrRow, cntrCol);
           if (temp > 10)
               log.value = log.value + (temp - 10);
           else
               log.value += "-";
       }
       log.value += " ===> ";
       for (var cntrCol = 1; cntrCol < 10; ++cntrCol) {
           temp = data.getValue(cntrRow, cntrCol);
               log.value = log.value + temp;
       }
       log.value += '\n';
   }
}


With the ability to dump the puzzle as it is being generated solved, the next issue is revealing tiles in random order. While this could be done purely randomly, every time the randomly chosen tile is already revealed you will need to choose another tile. As the board nears completion you will have the problem that you are going to have a lot of filled-tile hits causing you to be stuck in potentially long random selection loops. My solution is to simply create a row and a column array, shuffle those arrays, then simply use that random order when trying to find the next tile to reveal.

The method I use for trying to solve the puzzle is to simply look at what possible numbers can be placed in the tile. This is done the same way  the hint feature was created in the Sudoku player. If there is only one possibility, we treat that tile as known. When all tiles have been uncovered or marked as known then we are done.

Next, we get to the hard part. Creating potential sudoku solutions.

No comments: