1) Evaluation of true / false
When I applied the C code as is in C#, Visual Studio would show errors:
In C, zero is false; so you could use positive values of scalars or expressions to test for true in conditional (if) and iterative statements (do, while, for).
So code like
becomesif ((x%4 == 0) && (y%4 == 0))
Some more examples:
becomesif ((box[i, j] == 0) && (poss[i, j] == 0))
2) The dreaded exit
In the C program, there is the following code:
if FIRST_SOLUTION_OK is true, exit is called which stops the program. Function try_obvious is called from function solve_it. solve_it calls it recursively, and when the above code is hit, irrespective of the depth of recursion, the program just stops.
What are the constraints in C# ? Firstly, there is no exit. At least I did not find it. Secondly, MH-Sudoku Solver is a visual program, so the exit has to be to the gui. Thirdly, the C functions were translated to methods in the C# class.
First, I tried the return statement; the execution returned to the calling method (solve_it), but then the recursive logic continues. I had to return to the method above solve_it i.e. method process_input that calls solve_it, whose return goes to the application GUI.
gui --> process_input --> solve_it --> calls try_obvious and then itself recursively
I tried setting a few variables and checking for them, but since the method was in recursion, it did not work.
Next, I considered goto, however, goto transfers control only within the lexical scope (same method), but not to a label in another method.
As I was working, at one point it flashed to me that I should be using exceptions which allows us to transfer execution control to the catch block. The C# code I came up with is:
if (Nsolved == 81)
This is not an exception (an error occured), but a success condition and the exception handling mechanism is used to get out of the recursion and exit to the gui. Thus, exception handling is used as a glorified goto.