Got more questions? Find advice on: SQL | XML | Regular Expressions | Windows
in Search
Welcome to AspAdvice Sign in | Join | Help

C# Nuggets

Silverlight Sudoku with LINQ

A couple of months ago I came across a very clever Sudoku solver written by Peter Norvig. Peter had written the solver using Python. The solver was special as it solved by constraint propagation using list comprehensions and generator expressions making it extremely fast and very memory frugal. It turns out that the new LINQ feature of C# v3 is basically list comprehensions and generator expressions by another name.

Brendan Eich, the creator of JavaScript had ported the original Python implementation to JavaScript and I thought it would be a good idea to try to port it to C# v3. You can check out the code here.

 
I always had a vague plan to wrap the solver in a Silverlight front-end for added WOW factor, but it never really materialized. Fast forward two months and David Anson posts a Silverlight-based Sudoku game on his blog. This gave me the perfect excuse to integrate the LINQ-based solver into his codebase. I extended David's code to respond to the Escape key and complete the board with the first solution found. You can play with it online here, or download the code here. Have fun!

 

 To solve a game, give the Silverlight board the input focus and hit the escape key.

 

Sponsor
Published Thursday, August 23, 2007 8:30 AM by rbirkby
Attachment(s): Solver.cs.txt

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

[translation] solve all the problems (sudoku) said:

June 12, 2011 1:14 PM
 

RichB said:

Code now at https://github.com/PKRoma/LinqSudokuSolver
October 12, 2012 8:07 AM
 

Gizmo said:

This one will not solve. Not sure why.... var hard = "..54.3.9.7.892.4...4.....724..8.6.25.5..4..1.63.5.2..437.....6...4.392.7.1.7.58.."; The answer is: var Solved = "125473698768921453943658172497816325852347916631592784379284561584169237216735849";
June 6, 2014 9:49 PM

Leave a Comment

(required) 
(optional)
(required) 
Enter the code you see below

Submit