Did you ever want to pass the time or to stretch your brains while awaiting important news event or monitoring open position in MetaTrader? It's easy with Sudoku puzzle. This nontrading expert can do many things for Sudoku: generate boards, load existing boards, check difficulty, let you play and solve Sudokus right in MetaTrader chart. This is the classical 9x9 Sudoku with 9 blocks, and digits from 1 to 9 to fill empty cells.
After its started the program shows a dialog with a puzzle. If input parameters are left default, this is a special introductory position provided with the source codes (you may find some details below).
You can adjust input parameters to get random sudokus with different characteristics. Please, note, that new board is not generated right after the start, but upon clicking the New button in the dialog. Other buttons are available in the dialog: Hint shows automatically calculated candidates for empty cells (normally, a fair player should deduce candidates himself/herself, but this option helps you learn Sudoku a bit, if you're a novice); Undo reverts performed moves/changes on the board.
Here is an example of log when you generate new sudoku:
Shuffling... Permutations done: 137 of 200 Generating composition... Done at nesting level of speculations/guesses: 1 / Backtrack count: 0 Clues: 53 / Integral difficulty: 1.08067 Not a unique solution Done at nesting level of speculations/guesses: 1 / Backtrack count: 0 Clues: 50 / Integral difficulty: 1.14551 Not a unique solution ... Done at nesting level of speculations/guesses: 13 / Backtrack count: 0 Clues: 21 / Integral difficulty: 7.21605 Not a unique solution Done at nesting level of speculations/guesses: 21 / Backtrack count: 0 Clues: 21 / Integral difficulty: 9.0458 Not a unique solution Done at nesting level of speculations/guesses: 13 / Backtrack count: 0 Clues: 21 / Integral difficulty: 7.21605 Done at nesting level of speculations/guesses: 17 / Backtrack count: 0 Clues: 20 / Integral difficulty: 8.59135 Not a unique solution Ready, number of clues: 21 ......... .742...3. 8.....749 .8.1...25 5........ ..7.....3 .5....... ..1...... 24.5.8... Generated soduku is saved in file rnd25473.txt
The last line mentions the name of file with the new board. You can use it in the input parameter Sudoku to continue the same puzzle next time (in the next MetaTrader session). For every puzzle, the program remembers current position in a special file (with the same name as Sudoku, but with additional extension ".log"). Also all edits on the board is logged in the global variables and can be reverted by Undo button.
The number of clues and estimated difficulty are also shown in a comment on the chart. There is no a standard scale of Sudoku difficulties, so you'll get accustomed with suggested rating after some time. The value below 2 means easy level, 2 and a bit higher - medium level, above 3 - difficult, it requires to speculate/make guesses multiple times, and backtrack in case of wrong guessing (encountering a collision). Unfortunately it's not possible to generate sudoku of specific difficulty upon request. If you get a puzzle with undesirable difficulty, try to generate another random one.
You can enter a single digit in a cell (fill the edit field), or mark it with several digits - so called candidates, possible values. Candidates are displayed in smaller font in green. During the play you should eliminate candidates one by one and finaly left a single digit (displayed in blue).
Be cautious: clicking the Hint button will overwrite your manually entered candidates (if any). Clicking the Hint button second time will hide candidates completely. In other words, the Hint button works as a switch for automatic help.
The program will check if the puzzle is solved only when all cells are filled with single digits. Of course, the board is considered solved if it complies with Sudoku rules.
The introductory sudoku supplied with the program and shown by default is interesting in many aspects.
Here is how the puzzle is presented in the text file:
000700000 100000000 000430200 000000006 000509000 000000418 000081000 002000050 040000300
It could be as well
...7..... 1........ ...43.2.. ........6 ...5.9... ......418 ....81... ..2....5. .4....3..
To build the program, unpack the attached res.zip with bmp-resources (it should create MQL5\Include\Sudoku\Layouts\res\ folder with bmp-files), and then comment out the line #define CODEBASE_CHECKUP in the source code SudokuUI.mq5. Without this some button images will be unavailable. This is a workaround for the strange limitation of the codebase, which does not allow BMP-files, which are mainly used for graphic resources in MQL.