Using Solver Foundation to generate Sudoku puzzles, Part III (final)

This is the final post on the Sudoku puzzle generator. We have seen how to generate a Sudoku puzzle by using Microsoft Solver Foundation to repeatedly solve the Sudoku model with different board configurations. The data binding feature in Solver Foundation Services makes the process elegant in the sense that only the data source needs to be updated each time and model does not need to be changed.

In this post, we will integrate the puzzle generation into Excel (we assume Excel 2007 and Visual Studio 2008 are installed) via a new add-in. This new add-in creates a new Ribbon tab in Excel. We will not going into the details of the code this time as it is purely mechanical work of building an Excel add-in.

The main procedure is the event handler that handles the button click event on Generate button.

Private Sub GenerateSudokuButton_Click( _

  ByVal sender As System.Object, _

  ByVal e As _

  Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) _

  Handles GenerateSudokuButton.Click

  Dim sheet As Excel.Worksheet = GetSudokuSheet()

  With sheet

.Cells(1, 1) = _

 "Sudoku For Excel, Powered by Microsoft Solver Foundation"

    .Cells(_offset + 10, _offset).Clear()

    .Cells(_offset + 20, _offset).Clear()

.Range(.Cells(_offset, _offset), _

       .Cells(_offset + 8, _offset + 8)).Clear()

    For i = 0 To 2

      For j = 0 To 2

        .Range(.Cells(_offset + 3 * i, _offset + 3 * j), _

              .Cells(_offset + 3 * i + 2, _

                      _offset + 3 * j + _

        2)).BorderAround(Excel.XlLineStyle.xlContinuous, _

  Excel.XlBorderWeight.xlMedium, _

         Excel.XlColorIndex.xlColorIndexAutomatic)

      Next

    Next

.Range(.Cells(_offset, _offset), _

       .Cells(_offset + 8, _offset + _

        8)).BorderAround(Excel.XlLineStyle.xlDouble, _

       Excel.XlBorderWeight.xlThick, _

       Excel.XlColorIndex.xlColorIndexAutomatic)

.Range(.Cells(_offset, _offset), _

       .Cells(_offset + 8, _offset + 8)).ColumnWidth = 5

  Dim board(,) As Sudoku.SudokuBoardCell = _

    Globals.Sudoku.GeneratePuzzle()

  If (board Is Nothing) Then

  MsgBox("Cannot generate a puzzle. Please try again.")

  Return

  End If

  For i = 0 To 8

  For j = 0 To 8

  If board(i, j).PresetValue <> 0 Then

  .Cells(_offset + i, _offset + j) = _

        board(i, j).PresetValue

        .Range(.Cells(_offset + i, _offset + j), _

          .Cells(_offset + i, _offset + _

            j)).Interior.Color = _

          Excel.XlRgbColor.rgbCornflowerBlue

      End If

    Next

  Next

  End With

  Me.FillInZerosToggleButton.Checked = False

End Sub

 

The highlighted places call the puzzle generator we have created to get a new puzzle and fill in the cells in a specific sheet called “Sudoku” with preset values in the puzzle.

This is the end of this sample. I hope you had fun reading through the posts. The complete sample solution can be downloaded from the download page of Microsoft Solver Foundation website. Please feel free to contact me regarding any questions or feedback on this sample. Thank you in advance.

In the next series of posts, we will take a look at the camera depot sample shipped in Microsoft Solver Foundation V1. That sample embeds Solver Foundation in an ASP.NET web service to recommend an optimal camera configuration based on user’s choices and preferences. On the client side, user can specify his/her choices and preferences using a Silverlight UI. This next sample is written in C#.

- Lengning Liu

Comments