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.
-Dino
Comments
- Anonymous
February 15, 2006
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. - Anonymous
February 15, 2006
Have you come across any code that solves a Sudoku puzzle? - Anonymous
February 15, 2006
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 9x9. 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. - Anonymous
February 15, 2006
The comment has been removed - Anonymous
February 21, 2006
The comment has been removed - Anonymous
March 07, 2006
I love Sudoku ! - Anonymous
March 08, 2006
How do you determine the difficulty rating of the puzzles? - Anonymous
March 08, 2006
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! - Anonymous
March 16, 2006
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. - Anonymous
March 24, 2006
Dino,
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,
Tuan
tuanmd@scn.org - Anonymous
April 03, 2006
Can I use binary JARs 'as is' or do they need to be recompiled in VJ#? - Anonymous
April 03, 2006
Pat, no, you cannot directly reference Java archives (JARs) from J#. You can: (a) convert them with jbimp.exe (see http://msdn2.microsoft.com/en-us/library/y9teabc2(VS.80).aspx); (b) re-compile the Java classes with vjc.exe; or (c) bridge via something like jnbridge (see www.jnbridge.com).
-Dino - Anonymous
April 03, 2006
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 www.sysinternals.com. 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.
-Dino - Anonymous
April 03, 2006
Thanks, Dino. (a) and (b) look like they might work for me. - Anonymous
December 17, 2008
The comment has been removed