Today I want to show you a sudoku solver I just wrote in C++. It is a software pretty simple, but that helps you to exercise using of the language.
Note: it can solve only simple sudoku
The idea is simple: for every cell, the software considers all the possibilities, from 1 to 9. Then, for every cell, it discards the impossible alternative: and so on, processing every cell, until the puzzle is solved.
The software takes input from the text file ‘sudoku.txt’, that contains the sudoku formatted as shown in the following picture:
Every cell is separated by each other by a comma, every row by a new line;
x’s represent empty cells that the software will fill.
This software is really fast: it solved every sudoku I gave as input in less than one second.
The code should be easy to read. In the
main() function, while reading the input file, there is an instruction inserted under the directive
#ifdef _WIN32. When you use Windows, in fact, “new line” is achieved with the couple CRLF (that is ‘\r\n’), while on Unix with the only ‘\n’. This instruction removes the character ‘\r’, so the software can go on.