Sudoku solver and generator

Sudoku solver and generator

Released 7 years ago , Last update 7 years ago

An easy to use SuDoku solver and generator meant to be extensible and practical.

This is a simple yet efficient sudoku solver and generator with many updates to come. It solves sudokus using human logic and is capable of listing the steps needed to solve the given sudoku. It can also generate sudokus of varying difficulty in reasonable time (0 sec -5 sec) for most difficulties.

What sets it apart from other generators and solvers you find around the internet is that it guarantees uniquely solvable sudokus in reasonable times with steps used to solve it. It is designed to be easily incorporated in bigger projects and used as it is. Most code around the internet is either to specific and not easily incorporated in other projects, not efficient or not humanly readable or understandable and doesn't provide steps. Other than that support and updates will be available unlike other sources which makes it worth for long term use. The code saves you over 1000 lines of code which has been tested extensively and works out of the box.

Many updates are to come to extend the solver logic and make speed improvements.

Example usage:

Sudoku su;//Declaration

su.reset();//Resets the data in the sudoku

int data[]={0,6,0,8,7,0,1,0,0,2,0,0,0,0,6,7,9,8,8,0,7,2,0,0,4,0,0,9,1,8,3,2,0,0,0,6,0,4,0,0,0,7,8,0,9,0,0,0,0,0,0,0,0,3,0,8,0,0,0,2,0,0,4,0,0,3,7,6,0,9,0,0,0,2,9,4,3,0,6,8,0};
su.set_matrix(data); //Sets a sudoku to the data provided row by row where 0 denotes an empty space
//This is the faster method

string data="060870100200006798807200400918320006040007809000000003080002004003760900029430680";
su.set_matrix(data);//Sets a sudoku to the data provided row by row where 1-9 are valid values 0 and . represent empty cells and everything else gest skipped.
//It's the preffered method as it's the most flexible

su.get_possble(5,3);//Returns an integer whose binary representation represents the possible candidates in that cell (row,column)
//eg. 12 in base 2 is 1100 so the possible candidates are 3 and 4 (1 being the rightmost and 9 being the leftmost bit)

su.update_possible();//Updates the possible candidates for each cell in case that some changes are made

su.solve(true,3);//Solves the current matrix. The 1st argument is a bool that determines wether to save the steps that were made , the 2nd argument determines the difficulty of the matrix 1-3
//The function returns a bool true meaning the sudoku was solved, false meaning it wasn't

su.generate(2,40);//A function that generates new sudokus from solved ones. The 1st argument is the difficulty of the supposed matrix the 2nd one is the number of emty cells that are to be made
//If success it returs true otherwise false
//Maximum depth for now is 50 since any more takes too long for a matrix to generate

su.generate_simple();//Sets the matrix to a simple solved sudoku, usefull for generating new sudokus if you don't have your own solved ones

su.generate_until_found(1,45,20000);//This function attempts to generate a sudoku from the provided matrix. The 1st arguments is the wanted difficulty, the 2nd is the wanted depth and the 3rd one is the number of attempts

su.generate_until_found_simple(2,43,25000)//Same as the previous function but uses a simple generated sudoku

su.shuffle();//Shuffles the sudoku matrix in a valid manner so additional sudokus can be produced from existing ones

su.print_matrix();//Prints the matrix in a command line fashion

su.print_matrix_with_possible();//Prints the sudoku with the possibilities for every cell in a command line fashion

su.return_matrix_as_string();//Returns the matrix in form of a string 1-9 being cells with values and 0 being cells that are empty

su.return_steps();//Returns a vector that contains the steps in chronological order with location and step

//The generating and solving functions have default values that are used if you don't provide your own
//There are lots of others internal functions that can be used but are not mentioned
//Future updates will contain speed and difficulty improvements
  • 5 0
  • 4 1
  • 3 0
  • 2 0
  • 1 0
1 Reviews Read Reviews


14 day 14-day money-back guarantee


Developer License

  • Perpetual license

  • Unlimited projects

  • Can distribute code and binary products

  • Commercial use

1 license From » $9.99 View Licenses

Get A Quote

What do you need?
  • Custom development
  • Integration
  • Customization / Reskinning
  • Consultation
When do you need it?
  • Soon
  • Next week
  • Next month
  • Anytime

Thanks for getting in touch!

Your quote details have been received and we'll get back to you soon.

Or enter your name and Email
  • C camille almeida 3 years ago
    Hi, I would like to know if the bug mentioned in "support" was fixed? I'm interested in the app. Thanks
  • ES Eric Snider License holderDeveloper License
    Purchased on Jan 14, 2013
    5 years ago
    Does the generator guarantee there is only one unique solution to each puzzle?
    • AA Asmir Avdicevic Publisher 5 years ago
      I wasn't sure since I've written the code a long time ago, so I've checked again, and yes it does generate uniquely solvable puzzles.
  • ES Eric Snider License holderDeveloper License
    Purchased on Jan 14, 2013
    5 years ago
    How well does this work at generating sudoku puzzles of different difficulties without any puzzles to start with? I'd like to offer users randomly generated puzzles of the requested difficulty on demand instead of creating puzzles in advance. Also... what does "depth" mean in the creation function?
    • AA Asmir Avdicevic Publisher 5 years ago
      If no pre-made matrix is given it generates a basic one and then works it way in randomizing it to a sufficient level. Generally these matrices are good enough for most games/users with more than enough variety. Depth means how many different permutations does it look at before deciding to backtrack and try a totally different combination to generate a valid matrix with given requirements.
    • ES Eric Snider 5 years ago
      Thanks for the info! I haven't run the code yet, but it looks good. I've never licensed code on binpress before. With that said, how do you feel about adding support for X-Wings and Swordfish? Is that something you might add? Could I hire you to add those?
    • AA Asmir Avdicevic Publisher 5 years ago
      Yes, I know about those techniques and thought about adding, but didn't find much time to work further on this since I have other projects I'm currently involved in. I'll think about it and contact you in the next couple days.