The following code creates Sudoku Number Sequences at random. It does not present a game that can be played but the solution of games which can then be transferred to a game program. The algorithm is not elegant nor do I use “best practice” programming techniques, but one important aspect of it is that it works! It will consistently produce sequences of 81 numbers which are unique, original and logically sound Sudoku game solutions. There is no mathematical wizardry involved, no artificial intelligence, nor is there any “fuzzy logic” it just relies on chance: random numbers!
The programs flowchart is reasonably simple: I start by populating a ListBox with the numbers 1 to 9 then I randomly shuffle them. Those numbers are then in turn transferred to the first row of a Sudoku array. (I used a Label control array). Then, in turn, each square is filled with a randomly selected number (1-9) and interrogated to see if it conflicts with the same number in the current row, column or block of 9. If there is no conflict the next square is selected and the process continues. If there is a conflict then the next random number is selected.
If all 9 numbers return a conflict then the process re-starts at the beginning of the current line. This testing for conflict/no conflict continues until all 81 squares are populated, i.e. A sequence of Sudoku numbers have been created. At times there may be no solution to the current sequence of number and the display will show what I call a “Bail Out” event. A Bail Out event is easy to see; the program continuously loops with the same set of numbers over either 1 square or multiple squares.
If this occurs the Bail Out button should be clicked to restart the process from the first row with a different set of random numbers. When the selected number of sequences is created the list of sequences is saved to a text file with each line being a string of 81 characters representing the sequence. This text file (SudokuTest.txt) can then be transported to a Sudoku game and used as the source for the solved games. Testing of the program indicate that, on average, it takes less than 30 seconds to create a Sudoku sequence of numbers with an average Bail Out every 5 generations of sequences I chose to write this short program because I’ve already written 3 versions of the Sudoku game, (numbers, words and pictures), some 3 years ago.
But I had to rely on using published solved games that I “borrowed” from a book of Sudoku puzzles and I realised that there may be copyright issues. It was also a time consuming exercise and prone to errors creating the text file by hand, so the only alternative was to create my own Sudoku puzzles. I solicit all comments and criticisms from people of all levels of programming experience. The only way to judge the merits of the program is to copy and run it for oneself. The Form should have two buttons, two Labels and two ListBoxes positioned similar to the screen capture below. The array of 81 labels is a control array created at run time. The link to the program in my account on SkyDrive is.