Generating Solvable Sudokus with PHP

in #utopian-io7 years ago (edited)

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

Capture.PNG

Commits of the Day

Capture.PNG

New Features

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');

output_b2tROp.gif

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.

image.png

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() returns true

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.
Capture.PNG

Proof-of-work

U5dsbXERZdD1p2x61ySYPDH19UP5wQF_1680x8400.png

Kind regards!



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

Very informative, I like the level of details you went into.

I was not able to build the project as per the README but rather doing a simple composer install.

Screenshot from 2018-01-25 19-51-51.png

You can contact us on Discord.
[utopian-moderator]

Dear helo!

Thanks for your feedback. Actually it was not a bug, it was about the released version. It was not marked as stable. But it has no security issue from my point of view so I marked it as stable. So I want you to clear your composer cache with following code

composer clearcache

then give a try once again

composer require frnxstd/sudoxu

see my terminal window below,
thanks advance!

Capture.PNG

Hey @ikidnapmyself I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x