February 11, 2009

New Sudoku Algorithm - now with working puzzles!

Filed under: php, sudoku — grant @ 7:19 pm

I first developed my Sudoku game system back in 2005. Over the years thousands of people have played it and many have sent positive feedback. However, the one recurring issue since launch has been the quality of the computer generated grids - the quality was variable and often it would generate grids which had more than one possible solution (a bad thing in Sudoku circles).

It turns out that good, difficult Sudoku puzzles take a while to generate, solve and grade.  So whereas the old system spat out grids on-the-fly, the new system has to generate puzzles in advance.  Here’s roughly how it works…

  1. The system first generates a thousand or so puzzles with no particular restrictions.  These are quick to generate (approx 30 puzzles per second) and tend to be fairly easy puzzles to solve.
  2. It then generates a thousand puzzles that require at least one advanced technique (beyond the usual naked and hidden pairs).  These take much longer to generate (approx 4 puzzles per minute).
  3. Next, the system attempts to solve the puzzles using ‘human’ techniques (rather than brute force random guessing).  This gives each puzzle a grade.
  4. Finally, the solutions are de-duplicated and everything is stuffed into a MySQL database where they can be ordered by the grade they’ve been assigned.  This step is important, as some of the puzzles with no restrictions actually turn out to be really difficult, while some advanced technique puzzles actually turn out to be quite tame.

The database currently contains nearly 2000 unique puzzles, split into categories of Easy, Medium, Hard and Evil, based on their computed grade.

Please have a play here and let me know what you think.   Have I got the grading about right?   Are there any problems with the system?   Feedback is always appreciated.

February 23, 2008

Calling all PHP programming Sudoku fans!

Filed under: flash, php — grant @ 10:09 am

I’ve been running a free Sudoku game for a couple of years at www.grantgibson.co.uk/sudoku.  It’s available to play in desktop and mobile formats as well as on widget sites like Netvibes.com. 

The Flash part is really my area of expertise, but I also had to hack together a PHP script to generate the puzzles.  I found a script that does the right sort of thing, and modified it to output in my required format.  However, over the years I’ve had a few people complain that it produces puzzles with multiple solutions (and then only accepts one of those solutions as the ‘correct’ answer).

I don’t know enough mathematics or PHP to correct the problem, so I’m putting out an appeal.  If anyone wants to have a go at correcting the problem — or rewriting the script — please do so.  You’ll be helping me and thousands of other Sudoku fans around the world.

Requirements

Input parameters (via GET URL):

  • Difficulty [currently 1, 2 or 3, but can be tweaked if necessary]
  • Random [currently a six digit integer, but can be tweaked if necessary]

Output format (plain text):

puzzle=<TITLE>
Daily Random Sudoku: Medium
<AUTHOR>
Your Name Here
<COPYRIGHT>
Your Name Here and Grant Gibson
<DIFFICULTY>
2
<GRID>
.ooo..o..
o.o..oooo
o.o…ooo
o.o..oooo
oo.ooo.oo
oooo..o.o
ooo…o.o
oooo..o.o
..o..ooo.
<ANSWER>
528631974
163974285
497285631
235716849
671849352
984352716
859123467
312467598
746598123

… where answer is the unique grid solution, and grid represents the intial state of the puzzle - dot for blank, o for a displayed number.

I’ve attached the Current Grid Script here.  If you’d like to suggest any revisions, or post up an alternative version of the code please do so — either as a comment here, or directly to me by email.  See my contact page for email details.

Thanks, and good luck!

Listening to now

Categories

Archives

Meta

Blog Tools

Add to Technorati Favorites

Powered by WordPress