public static SudokuMatrixModel GenerateRandomBoard() { var model = new SudokuMatrixModel(); // add thirty values var count = 0; var r = new Random(); var usedSquares = new HashSet <Index>(); while (true) { if (count == 30) { break; } // the max value for Next is EXCLUSIVE var nextX = r.Next(0, CellCount); var nextY = r.Next(0, CellCount); var nextIx = new Index(nextX, nextY); if (usedSquares.Contains(nextIx)) { continue; } var nextVal = r.Next(1, CellCount + 1); var c = model.GetCell(nextX, nextY); model.UpdateValue(c, nextVal); count++; usedSquares.Add(nextIx); Console.WriteLine($"Set {nextIx} to {nextVal}"); } return(model); }
static void RunSudoku() { var matrix = SudokuMatrixModel.GenerateRandomBoard(); Console.Write(matrix.ToString()); var valid = matrix.Validate(); Console.WriteLine("Matrix is {0}", valid ? "valid" : "invalid!"); var errors = matrix.GetInvalidCells().OrderBy(x => x.Ix.Row).ThenBy(x => x.Ix.Column); foreach (var item in errors) { Console.WriteLine("Cell {0} is invalid", item); } var gridErrors = matrix.GetInvalidGrids().OrderBy(x => x.Ix.Row).ThenBy(x => x.Ix.Column); foreach (var item in gridErrors) { Console.WriteLine("Grid {0} is invalid", item); } }