On this blog I regularly publish articles with tips and tricks for the programming language C# .Net.
C# is a modern, object-oriented programming language, which fully ultilizes the possibilites of the .Net framework. I also write about app programming for Android via C#, as well as PHP and Matlab. The difficulty of this blog is supposed to be variable, I hope for beginners and experts there is always something to look for.
If you have questions or suggestions, I am happy about your emails.

Monday, November 10, 2014

I now have uploaded the complete program regarding the topic Sudoku, which consists of the code parts of the previous posts, on GitHub you can find the source code, and here an installation file, with which the program can also be installed and run without Visual Studio.

Monday, November 3, 2014

In this post I want to explain the creation of Sudokus of arbitrary difficulty with C#. It is very recommended to read the previous posts regarding these topics.
The class SudokuCreator is renponsible for generating Sudokus, the function CreateRandomSudoku() does this. It expects 2 parameters, the first describes the desired difficulty of the Sudoku, the second the number of fields to be deleted.
First a random completely filled Sudoku is created, here the same principle as when solving a Sudoku by trying out is used.
Then in a loop always one random field is deleted and it is checked, whether the Sudoku is still uniquely solvable (for this the Sudoku is solved using brute force and the number of possibilites is counted) and at most as hard as the desired difficulty. If yes, we continue with the new Sudoku, otherwise with the old one. This is repeated as long as not the desired number of fields is deleted, afterwards the Sudoku is returned.
But of course it can be the case, that the fields are deleted very unforunately, and the future modification of the current Sudoku is hard. Therefor the number of tries to delete a field is counted, if the counter gets too high, we restart with the completely filled Sudoku.
But we also count this event, as it can also be the case, that the created Sudoku is especially unfortunate. If we have to restart too often, we create a new Sudoku.
And here the code:

Sudoku Cloned = NewSudoku.Copy();int difficulty = Difficulty;// take out one field and check if solutions still is unique and not harder than desiredif (DeleteField(Cloned, ref difficulty))
{
NewSudoku = Cloned;
Deleted++;