Re-use of existing Java code (Sudoku engine)

Re-use of existing Java code (Sudoku engine)

This isn't so much about systems interop, as it is about code re-use and language interop.

You've seen Sudoku, the "number place" puzzle. Wikipedia says that it was created by a US puzzle magazine, but popularized (and given its recognized name) by the Japanese.

Last year during the summer holiday my wife gave me a book of Sudoku puzzles to do on the beach with my kids. The first one we tried was a real challenge, but gradually we caught on to the technique. Now we do the puzzles that come in the daily paper, regularly. They started to get a little too easy for us, so our thought was, maybe we could generate puzzles ourselves?

I thought - this is a nice little programming project. The next thought was: to generate a valid Sudoku, you have to solve it, to verify there is only one solution. The simple brute force solver method I imagined would be easy to code, but wouldn't be very fast. And I hadn't thought about sudoku generation algorithms. This was getting messy. Maybe somebody has already done this for me?

In short order I found lots of Sudoku sites and an abundance of algorithms and code implementations for generators and solvers. I tried a couple and found one that looked good and fast - implemented in Java by Rolf Sandberg, based on a C implementation by Günter Stertenbrink, which was based on the Dancing Links algorithm described by Donald Knuth.

As a port from C, the Java code is not the most object-oriented or elegant, but I didn't care about that. I wanted a toy to generate Sudoku puzzles. It worked great, but there was one big drawback. The generated puzzles looked like this:

 . . 8 1 . . . . .
. 9 1 . . . . . 7
. . . . 5 . 3 . 2
6 . . 3 . 2 8 . 4
3 . 9 . . . 5 . .
. . . . . 6 . . .
. 1 . . . . . 2 8
. . 6 . . . . . .
. 5 . . 7 . . . .

It was a command line tool, and it generated string output. What I really wanted was a puzzle I could print out, easily, and hand to my sons and say "have at it!"

I had previously fiddled with itextsharp, a library for creating PDFs from within .NET. (This is a port of iText, which is a Java library with the same purpose). PDF generation would seem to be a good fit.

Here Comes the Part About Interop

The cool thing was, I didn't have to do anything to the Java code to get it to work with my C# WinForms code. I just compiled it with VJ#, and BAM! it's ready to go. So no changes were required, but I did actually make a few changes in Sandberg's Java code. I added a DTO class for the puzzle. I added a few helper methods like solve() and generate() and rate(). But I left most everything intact. (re-factoring was tempting but I didn't want to spend the time understanding the code)

So.... I built a Winform app in C#, to wrap around that generator engine. I added in the PDF generation, and added a print button. Boom, a Sudoku generation toy. Its pedigree includes Java source code, C# code, and an activeX control from 2000. All seamlessly connected.

Grab the finished binary (with a Windows Installer) if you want to use the toy. It will run on Windows XP, requires .NET 1.1 and Adobe Acrobat Reader. Grab the VS 2003 Solution if you want to examine or tweak the code.

This is just one small illustration, but it shows: if you have source code repositories in your shop, or even if you have existing Java apps, and you want to update or extend them, you may be able to re-use that code very simply in .NET.


Comments (15)

  1. Prakash says:

    Would it have helped to export/display result as a CSV or Tab delimited file? It would have opened in excel for easy online play.

  2. Prakash says:

    Have you come across any code that solves a Sudoku puzzle?

  3. cheeso says:

    Prakash – interesting idea to export to CSV for Excel.  Once open in Excel you’d ideally want it to look a little like a printed Sudoku, with a grid limited to 9×9.  I don’t know if I can do that in CSV, but I am sure I can do it in Excel-ML.  Will look into it.

    about tools that solve sudokus – this toy solves them, but the problem is, you cannot enter the sudoku.  The toy, in its current guise, only solves sudoku that the toy itself has generated.  Adding that feature is left as an exercise for the reader!

    But there are plenty of tools out there that solve sudoku.  This one is based on the DancingLinksEngine which itself is based on C-code implementations known as suexg and suexk, the former is a generator and the latter a solver.

    But I don’t believe those are GUI tools.

  4. Craig says:

    I’m using VS2005 and grabbed the source code, but the project cannot resolve references to AxPdfLib

    and PdfLib … where can I get these ActiveX components?

    Very cool tool (if I can get it working).

  5. cheeso says:

    To resolve the reference to the Acrobat PDF component, you need to add it to your VS toolbox.  This is how it worked in VS2003:

    Within VS, ensure that the Win forms designer is showing.  Hover over the toolbox.  Be sure the "Windows Forms" tab is open on the

    Left hand side.  Right click, then select "Add Items…"  you get the "Customize Toolbox" dialog.

    Select the COM tab.

    check the box for Adobe Acrobat control for ActiveX


    You have now added the Adobe Acrobat ActiveX control to the VS toolbox. It should appear at the bottom of the list.  From that toolbox, You can now drag an instance of that control onto a Windows Form, as with any other Windows Forms control.

    Since this project already has the activeX control there, you may have to add a reference to the library.  In the solution explorer, explode the references node.  If you don’t see AxPdfLib, you need to add it.  you should be able to implicitly add it by dragging-and-dropping the PDF control from the toolbox to the form.  

    good luck!

  6. scott says:

    How do you determine the difficulty rating of the puzzles?

  7. cheeso says:

    it’s part of the generation and solving algorithm.  I don’t understand Knuth’s DLX algorithm enough to explain it.  but of course you can look at the code!

  8. Any one know seen the java engines that allow you to play online?  I havnet had time to look at it yet but I dont think it would be that hard.  

  9. Tuan Duong says:


    I would love to get this Sudoku Toy to work on my computer. Unfortunately, it requires Acrobat reader 6 which I have uninstalled from my computer. Now Acrobat reader 6 refused to install (newer functional version exist) and I’m having trouble removing Acrobat reader 7 completely.

    Install Acrobat reader 7 will not overwrite a botched installation of Acrobat reader 7 which cannot be uninstalled. I tried to delete every file and folder related to Acrobat, search and delete all the acrobat entries in the registry. Adobe would not give me e-mail support on this predicament.

    I hope somebody can tell me where the Acrobat reader 6 setup program find out that Acrobat reader 7 existed.

    Acrobatically challenged,


  10. Can I use binary JARs ‘as is’ or do they need to be recompiled in VJ#?

  11. cheeso says:

    Pat, no, you cannot directly reference Java archives (JARs) from J#.  You can: (a) convert them with jbimp.exe (see; (b) re-compile the Java classes with vjc.exe; or (c) bridge via something like jnbridge (see  


  12. cheeso says:

    Sorry, I do not know how to do this. Seems like you are on the right path (filesystem and registry sweep).  if you are savvy, try regmon.exe, a free download from   Run regmon, then install Acrobat 7 on a new machine, then regmon will tell you what registry entries have been created.  There is also a companion filemon.exe from the same source.  you can then remove these entries from *your* machine.  That ought to work, but no guarantees.  I would advise trying this only if you are pretty savvy.  And of course you need a new (clean) machine to install Acrobat7 on.  


  13. Thanks, Dino. (a) and (b) look like they might work for me.

  14. Veeravijayarangarajaraghava Reddiar says: is a neat solution for Sudoku solving code.

    It is command line though and well broken.

Skip to main content