FillCell
This method is a recursive method for filling in a Sudoku table. Let's look at the long drop of code:
Code
/// <Summary>
/// Fill the cell
/// </Summary>
/// <Param name = "table"> table </param>
/// <Param name = "index"> index </param>
/// <Returns> return result </returns>
Private bool FillCell (Cell [,] table, int index)
{
RelativeCellMethod removeCandidateMethod = new RelativeCellMethod (RemoveCellCandidate );
RelativeCellMethod recoverCandidateMethod = new RelativeCellMethod (RecoverCellCandidate );
If (index> = 81)
{// If the index is out of the range, it indicates that the Sudoku has been successfully generated and the result is returned directly.
Return true;
}
If (table [index/9, index % 9]. Value! = 0)
{// If the index cell has been assigned a value, Jump directly to the next Index Assignment
Return FillCell (table, index + 1 );
}
Bool flag = true;
List <int> nextCandidates = new List <int> ();
// Pre-Save the candidate number sequence of the modified cells. If no candidate number is successful, the candidate number is restored and then returned.
NextCandidates. AddRange (table [index/9, index % 9]. Candidate );
While (table [index/9, index % 9]. Candidate. Count> 0 & flag)
{// If the number of cell candidates is greater than 0 and the number is marked as true, the number of cyclic testing candidates
SetValue (table [index/9, index % 9]); // assign values to cells
Flag & = DealRelativeCell (removeCandidateMethod, table, index); // remove the number of candidates corresponding to the value of the relevant Cell
If (! Flag)
{// If the number of candidates fails to be removed, the number of candidates will be restored and the next cycle will continue.
DealRelativeCell (recoverCandidateMethod, table, index );
}
Else
{// If the number of candidates is successfully removed, continue testing to fill the next cell
Flag & = FillCell (table, index + 1 );
If (! Flag)
{// If filling the next cell fails, the number of candidates will be restored and the next cycle will continue.
DealRelativeCell (recoverCandidateMethod, table, index );
}
Else
{// If the next cell is successfully filled, return directly (running here will definitely indicate that the entire Sudoku has been successfully generated !)
Return true;
}
}
Flag =! Flag; // reverse the flag and continue the next cycle
}
If (table [index/9, index % 9]. Candidate. Count = 0)
{// If all candidates fail, restore the number of candidates for this cell and return false
Table [index/9, index % 9]. Candidate. AddRange (nextCandidates );
Return false;
}
Return flag;
}
The first two rows define two proxy methods. In the while method, the number of candidates for one test cell is filled in the cell. If the input fails, the number of candidates is restored, continue to try the next number of candidates. If no number of candidates is correctly generated, the while LOOP jumps out and the function returns false, Which is returned to the previous cell, reset the value of the previous cell to 0, and try another number of candidates, and so on. If the index is greater than or equal to 81, all cells are successfully assigned a value, and true is returned.
Next, you only need to write a method to display the sudoku:
Show
As follows:
Code
/// <Summary>
/// Display Sudoku
/// </Summary>
/// <Param name = "table"> table </param>
Private void Show (Cell [,] table)
{
For (int I = 0; I <9; I ++)
{
For (int j = 0; j <9; j ++)
{
Console. Write ("{0, 2}", table [I, j]. Value );
}
Console. WriteLine ();
}
Console. WriteLine ("----------------------------------------------");
}
There is also a method to initialize the sudoku table and then start filling in the sudoku:
StartFillTable
Code
/// <Summary>
/// Start to generate a Sudoku
/// </Summary>
Public void StartFillTable ()
{
Cell [,] table = new Cell [9, 9];
While (true)
{
For (int I = 0; I <9; I ++)
For (int j = 0; j <9; j ++) // initialize a Sudoku table
Table [I, j] = new Cell ();
Bool flag = FillCell (table, 0); // fill the sudoku table
If (flag) // if a Sudoku is successfully generated, this Sudoku is displayed
Show (table );
Console. ReadKey ();
}
}
I wrote it in a console application. It would be nice if I used Silverlight or other presentations !, The following shows the Main method and result display:
Class Program
{
Static void Main (string [] args)
{
CellMethod method = new CellMethod ();
Method. StartFillTable ();
}
}
You only need to press the Enter key to generate a different new Sudoku!
If you are interested in developing a Sudoku game, you can randomly mask a certain number of grids. The higher the difficulty is, the more grids there are, in this way, the sudoku game must have a solution, but the solution is not necessarily unique. To ensure the uniqueness of the solution, we need to use a method similar to this to let the computer first perform the independence after the mask to see if there is only one solution. If not, then re-select the mask area until the computer finds that the solution is unique, and then outputs it to the front end for players to play!
Next is the source code of the entire program.