Saturday, February 22, 2014

Solving KenKen (or MathDoku) Puzzle Programmatically.


Since for last couple of days, solving KenKen had completely occupied my mind. To find a solution out to solve this game was not an easy task. Here you find the Algorithm I used in order to get the puzzle solved.
My code is a little messed up. 
May be once I clean it, will upload to Git.


Algorithm

Step 01) 
Create an Array (allPossibleValues[]) corresponding to each cell. Assign all the possible values that the Cell can hold in that array (allPossibleValues[]).

Step 02) 
Identify all possible Rules.

Step 03) 
Based on each of the Rules and the allPossibleValues[] Identify and assign the values each cell can hold in another array (allClueValues[]).


Step 04) 
If the array allClueValues[] is not modified, then exit loop.

Step 05) 
Do an intersection on allPossibleValues[] and allClueValues[].

Step 06) 
If the array allPossibleValues[] is not updated, then exit loop.

Step 07)
Loop through the cells to find the Cell containing only one value in allPossibleValues[] array.

Step 08)
Assign corresponding clue value to that Cell (thisCell) and empty allPossibleValues[].

Step 09)
Remove the assigned value from the corresponding Row and Colum of that Cell (remove from allPossibleValues[]).
Step 10)
Go to Step 04.

Step 11)
Go to Step 03.

Step 12)
If there are still unasigned Cells, then use Brute Force or Depth-First Search.

Useful Links: