Ajax/PHP-powered Sudoku Helper


This Ajax/PHP-powered Sudoku Helper is a little project written for the ‘Internet Technologies’ course at KaHo Sint-Lieven. It’s an attempt to explore the world of object-oriented programming and the use of WebServices in PHP5, as well as a way to get to know the Prototype / Script.aculo.us Javascript frameworks.
And then some … Ajax (buzz buzz!), valid xHTML, CSS Design and a mySQL database.

This article is written for my fellow students wanting to know more about this experiment.

The Game

A Sudoku is a 9*9 grid, made up of 3*3 subgrids called “regions”. Some cells already contain numbers. The goal is to fill in the empty cells, one number in each, so that each column, row, and region contains the numbers 1-9 exactly once. Each number in the solution therefore occurs only once in each of three “directions”. – From Wikipedia

The application tries to help solving a Sudoku by offering suggestions and/or trying to solve it automatically.

Some extra info about the tools I’ve used

  • mySQL & phpMyAdmin: phpMyAdmin is a popular PHP-front end to administer a mySQL-database.
  • Javascript / Ajax: Prototype and Script.aculo.us were used to explore the power of Javascript. “Prototype is a JavaScript framework that aims to ease development of dynamic web applications with a toolkit for class-driven development and an Ajax-library.” Prototype includes some very handy functions like the ‘$’-function, AJAX-wrappers, Event-listeners, etc. (Explore Prototype / Prototype Tutorial) The Script.aculo.us-library builds on Prototype and adds some great effects and possibilities. Check out some cool effects or the sortable lists. Prototype and Script.aculo.us are both used on eg. www.digg.com and several people wrote handy scripts using Prototype and Script.aculo.us. Both Prototype and Script.aculo.us are quite heavy libs, check out moo.fx if you like your code lightweight. Other Javascript / Ajax-frameworks include Dojo, Ajax.net, Spry and Atlas.
  • PHP5: This new version of the popular scripting language offers better support for OOP. PHP5 allows for the following concepts to be used:
    • public / private
    • constructors
    • getters and setters
    • toString
    • Exception Handling

    See the code below for examples.

The Implementation

How it works

The Sudoku helper is a xHTML-page with 81 input boxes for all numbers in a Sudoku (client side). Javascript adds behaviour to these input boxes by calculating suggestions, showing errors, trying to auto-fill in numbers, etc. To speed things up, the maths are passed on to a server-side function which returns the necessary information (Ajax right there). Examples of Sudoku’s to solve are stored in a database and loaded into the input boxes, asynchronous-stylee of course.

The Algorithm

This was no attempt to solve every Sudoku around and it’s not about maths either, so the algorithm used to “solve” Sudoku’s is actually quite simple; there are nine options for each box, the algorithm will see which one of those are still possible by checking the row, columns and region for numbers that are already there.


So what do we need? A simple mySQL-table that stores examples of a Sudokus and their difficulty level, php-scripts (a database connection class, a class representing a sudoku and the script that handles the Ajax requests), the html-file with the input boxes and some extra features, the Javascript-libraries and the Javascript that adds all the fun to the html.

The Demo

See a demo of this project. It’s tested in Firefox 1.5, but I’m told it’s working ok in Safari too.

The WebService

One has gotta keep on learning, so I also tried to write my first real WebService using PHP and the new SOAP classes. The SOAPClient-class was used to open up the “give-me-a-sudoku-example”-function for the world. It takes one argument, a difficulty level from 1 to 3 and returns a string which represents a Sudoku. Maybe not very useful, but at least now I know how to publish and consume WebServices, right?

The Files

The Presentation

Some very basic slides, made with AJAX-S

Update (17/05): Digg This | PHP Developer News


  1. Jurriaan, dat hebt ge netjes gedaan. Nu nog een beetje alles fijntunen als pauzering tijdens de examens en het wordt nog perfect

    ps: spijtig dat uw site zo traag gaat

  2. powweb.com heeft vandaag inderdaad al wat raar gedaan; off- en online. ze zijn bezig met verhuis, vandaar waarschijnlijk. ik hoop dat het niet te veel van dattum is. die pauzes tijdens examens zijn idd befaamd ;).

  3. Knap werk Jurriaan alweer!
    De algoritmes zijn inderdaad niet de kern van de zaak, die kunnen gemakkelijk aangepast worden. Chique om te zien wat zoal mogelijk is hier met je demonstratie. Hopelijk steken niet alleen de studenten daar iets van op ;-) !

  4. Hi,
    I’ve made an online Sudoku MULTIPLAYER mode (2 players). http://www.SudokuCombat.com provides puzzles in easy, medium and hard levels. SudokuCombat is free and fully web 2.0. No subscription required, no crappy java applets, no advertising. Just fun!