Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

Sudoku creator/solver with PHP

This is an old project for a class held in 2005.

At that time, Sodoku game was very popular in Italy, and I saw quite a lot of Flash games based upon it.

If you don’t know what is a Sudoku, Wikipedia says is a logic-based number-placement puzzle. The objective is to fill a 9×9 grid so that each column, each row, and each of the nine 3×3 boxes (also called blocks or regions) contains the digits from 1 to 9 only one time each. The puzzle setter provides a partially completed grid.

The script consists in a set of functions that emulate the “human way” to solve a Sudoku game, with trials and errors, using backtracking.

The only thing you need to know is how I represent the Sudoku table inside the script.

It’s an array (line 193) with 81 values representing positions from 0 to 80. The value at n-th position is the one you will find on the table at row floor(n/9) and column n%9.

A value from 1 to 9 represents a know number of a Sudoku table, while a zero represents an unknown one.

For this reason, an array filled only with zeros like the one in the script will generate a new, full Sudoku.

This is the script:

And this is an example of a new sudoku generated with the script:

Refresh the iframe to create another one.

This script too is quite easy to port in AS3, if you convert it I’ll post it on the blog.

I should have an old AS2 conversion too somewhere in my hard disks…

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (22 votes, average: 4.91 out of 5)
Loading ... Loading ...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 33 comments

  1. Joel Parkey

    on December 9, 2008 at 5:32 pm

    I started to write a version of this in C# last week!

  2. Nathan

    on December 9, 2008 at 11:42 pm

    Cool. How would you make the 3×3 squares darker?

  3. lamfan

    on December 10, 2008 at 12:26 pm

    really good! I love it!

  4. Colin Diam

    on December 13, 2008 at 11:05 pm

    Can u please post the as2 version emanuele!!??!!

  5. Creare un risolutore di sudoku con php : sastgroup.com

    on December 17, 2008 at 11:39 pm

    [...] web: http://www.emanueleferonato.com/2008/12/09/sudoku-creatorsolver-with-php/ Share and Enjoy: These icons link to social bookmarking sites where readers can share and [...]

  6. Risolvere sudoku con php | Web2Eg

    on December 18, 2008 at 2:17 pm

    [...] web: http://www.emanueleferonato.com/2008/12/09/sudoku-creatorsolver-with-php/ Segnala [...]

  7. Kevin Hoyt

    on December 18, 2008 at 9:44 pm

    I got most of the way through an AS3 port, but got hung up on the data type of “next_move” in the solve() method. What’s that supposed to be?

    The scan_sudoku_for_unique seems to either return a boolean, or an array. Yet the next line in the solve() method checks it for a boolean. The line after that attempts to pop a value off what is initially an empty array and return that value into next_move. The subsequent “if” appears to treat next_move as an array.

    I’m not a PHP pro, so I’m likely missing something here, but if you could shed a little light on the data type/expectations for next_move, I’d really appreciate it.

    Thanks,
    Kevin

  8. Creare un risolutore di sudoku con php

    on December 19, 2008 at 2:13 am

    [...] Sito web: http://www.emanueleferonato.com/2008/12/09/sudoku-creatorsolver-with-php/ [...]

  9. Emanuele Feronato

    on December 19, 2008 at 10:48 am

    next_move determines if I can make another “move” (placing another number or not).

    If I can’t place a number, I have to backtrack the sudoku

  10. A strange way AS3 manages arrays : Emanuele Feronato

    on December 26, 2008 at 11:35 pm

    [...] I was porting my Sudoku creator/solver with PHP in AS3 when I found this strange [...]

  11. Sudoku creator/solver with AS3 : Emanuele Feronato

    on December 27, 2008 at 3:09 pm

    [...] At the beginning of this month I published the Sudoku creator/solver with PHP. [...]

  12. Richard Howarth

    on January 2, 2009 at 5:31 pm

    Thanks for this simple and easy to understand technique.

    It ported fairly easily to the KiXtart scripting language with a few changes where the interpreter does not have the wealth of built-ins of PHP.

    I’ve posted the ported script (with some additional code to create the starting grid) in the KiXtart community board here: http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=191555

    It’s a bit slow in KiXtart (it takes about 23 seconds to complete an empty grid), but watching the guesses is quite soothing.

  13. johnli

    on January 5, 2009 at 12:02 pm

    i have finished the as3 version!

  14. Rodrigo

    on March 31, 2009 at 10:32 pm

    can you post the initial state of the game?
    please?

  15. Subhajit

    on April 1, 2009 at 4:01 am

    It’s really nice!!!!!!!!!!

  16. Phil

    on May 27, 2009 at 9:42 pm

    I’ve written a similar one using PHP – however it doenst really generate puzzels, only solves them. Its avaliable at http://www.idontplaydarts.com/code/sudoku-solver/

    I use a similar method except my code is more OOP

  17. Torvall

    on June 6, 2009 at 5:46 am

    where is it matlab solucion from sudoku????

  18. kr.roland

    on August 25, 2009 at 12:51 pm

    Hy!

    Thx!
    perfect source code!

  19. Ertyno

    on August 31, 2009 at 4:00 pm

    No Sudoku X solver?

  20. nicolasd

    on May 28, 2010 at 11:53 am

    hi, i’m trying your code just for a test because i ve ceded one too, but i ve a non stop loop and nothing works, i dont know what isnt working but u call a variable x on solve method without declare it first, and i think the problem is due to that.
    if you have a correction of your code which is working can you send me by mail.
    i can also show you mine if needed

  21. Jim Nelms

    on December 3, 2010 at 10:00 am

    Hi All,

    I would be pleased to hear from a programmer to write the code for a new type of ‘Sudoku’ puzzle, if you are interested and prepared to sign a NDA please reply at the earlist, the successful candidate will be given shares in the company as a reward. Regards, Jim Nelms

  22. diallo

    on February 9, 2011 at 12:03 pm

    who can send me the sududu(php)

  23. gg

    on February 21, 2011 at 5:10 pm

    chewdoku is better: try http://twasack.tripod.com/index.htm

  24. Rizta Husni Ananda

    on April 8, 2011 at 4:12 pm

    For this particular problem (Platinum Blonde from Wikipedia):
    $sudoku = array(0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,3,0,0,2,3,0,0,4,0,0,0,0,1,8,0,0,0,0,5,0,6,0,0,7,0,8,0,0,0,0,0,0,0,9,0,0,0,0,0,8,5,0,0,0,0,0,9,0,0,0,4,0,5,0,0,4,7,0,0,0,6,0,0,0);
    And its solution:
    839465712
    146782953
    752391486
    391824675
    564173829
    287659341
    628537194
    913248567
    475916238

    My PHP script ran for 6.64 secs in my system. And yours for about 3 times longer.
    But still, your way of thinking is really interesting. Very much different than mine. I myself never use array push pop diff etc.
    Besides, I already spent over 500 lines of code for the basic elimination, trial & error, backtracking function. But yours only took less than 200 LOC for everything.
    Nice and neat. Good one dude!

  25. Rizta Husni Ananda

    on April 8, 2011 at 4:18 pm

    Argh shoot…
    Sorry my comment ruin your page. I didn’t know that it would be displayed like that. You can just delete it if you mind.
    Btw, on line 157 there’s a bit warning. Some kind of undefined variable. Thank you.

  26. David

    on May 4, 2011 at 7:25 pm

    Three bugs:

    The initial array is busted, it is too short.

    FIX:

    $sudoku = array(
    0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0,
    0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0,
    0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0
    );

    In the function “solve”, initialize $x before the loop:

    $x = 0;

    while (!is_solved_sudoku($sudoku)) {
    $x++;

    And the big one, check if a key for the array is valid, before trying to access it:

    function next_random($possible)
    {
    $max = 9;

    for ($x = 0; $x <= 80; $x++) {
    $keyExists = array_key_exists($x, $possible);

    if ($keyExists) {
    if ( (count($possible[$x]) 0) ) {
    $max = count($possible[$x]);
    $min_choices = $x;
    }
    }
    }

    return $min_choices;
    }

    Very nice code, works extremely fast.

  27. David

    on May 4, 2011 at 7:29 pm

    Three bugs to fix.

    1) The initial array is busted, it is too short.

    FIX:

    $sudoku = array(
    0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0,
    0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0,
    0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0
    );

    2) Initialize the variable $x before the loop within the function “solve”.

    $x = 0;

    while (!is_solved_sudoku($sudoku)) {
    $x++;

    3) Check if the array has the key available.

    function next_random($possible)
    {
    $max = 9;

    for ($x = 0; $x <= 80; $x++) {
    $keyExists = array_key_exists($x, $possible);

    if ($keyExists) {
    if ( (count($possible[$x]) 0) ) {
    $max = count($possible[$x]);
    $min_choices = $x;
    }
    }
    }

    return $min_choices;
    }

  28. Max Kessler

    on June 9, 2011 at 8:06 pm

    I don’t see any copyright notice. Did you release this to the public domain? If not, then what are your terms with regard to derivative works?

  29. Max Kessler

    on June 11, 2011 at 7:28 am

    To clarify, I don’t see a copyright notice in the *script*.

  30. Emanuele Feronato

    on June 11, 2011 at 9:15 am

    Use as you want. If you are going to making money out of this, consider a small donation to triqui@libero.it

  31. ??????

    on December 22, 2011 at 2:39 pm

    Thank you for this tutorial and i’ll try to develope this code for a facebook application.

  32. jay patil

    on December 28, 2012 at 11:42 am

    I wanna display a grid of numbers from 1-8..
    I want each number to be unique in its row as well as in its column..
    Can u help me based on ur sodoku algo…

  33. Hristo Hristov

    on July 15, 2013 at 4:11 pm

    The code do not start. There are endless unknown offset errors.
    Can yo tell me az explanation, PLEASE!

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×