A long time ago, when someone mistakenly broke fiber-optic cable of my office and I had to wait there with no connection all day long. After I tried to get rid of playing sudoku game on Ubuntu, I decided to make my own game. I did not expect that it would be that complex. Anyways, it was a simple class until this morning but I decided to modernize and improve it with Scrutinizer-CI. It's also composer friendly now. So I hope you think that, it deserves to be shared.
Before I start
Commits of the Day
New Features
- Composer compatibility added
Packagist link is here. - PHPDocs added
Related commit link is here.
Improvements
- Scrutinizer-CI rank improvement
- Bug fixes to improve Scrutinizer-CI rank
- Sample usage added
Installation
Run the command in your project folder:
composer require frnxstd/sudoxu
And then introduce your class to your code
use Sudoxu\Sudoku;
$Sudoku = new Sudoku();
And run the code!
$json = $Sudoku->generate('json');
$array = $Sudoku->generate('array');
$serialize = $Sudoku->generate('serialize');
Would you like to see it in action? You can check out sudoxu.com and it has jQuery flavor for a better user experience. By the way, it also provides better development experience.
Algorithm
Basically generate() function makes everything in a row. I will explain the steps one-by-one
- It loops until it's solved in while()
- Finds available
items(we use numbers 1 to 9 in this example) to place - In previous step, it finds the items, these are going to be unique in the row, column and in the block
- It tries one of available items
- If it's all okay, pushes into an array and keeps on looping until
Sudoku::is_solved_sudoku()returnstrue
How to contribute?
The code is able to create up to 6^2 sudokus with extra characters rather than numbers. But I do have performance issue here. If anyone could help me to solve this complexity, we could create deeper sudokus.
Proof-of-work
Kind regards!
Posted on Utopian.io - Rewarding Open Source Contributors