/// <summary> /// Randomizes random # elements in Sudoku /// </summary> /// <param name="sudoku">Sudoku to randomize</param> private static void Randomize(Sudoku sudoku) { Random rn = new Random(); for (int i = 0; i < sudoku.RandomizedCount;) { int row = rn.Next(0, 9); int col = rn.Next(0, 9); if (sudoku.Solution[row][col] != 0) { continue; } else { sudoku.Solution[row][col] = rn.Next(1, 10); } if (DataValidator.RowVal.IsValid(Sudoku.GetRow(sudoku.Solution, row)) && DataValidator.ColumnVal.IsValid(Sudoku.GetColumn(sudoku.Solution, col)) && DataValidator.SquareVal.IsValid(Sudoku.GetSquare(sudoku.Solution, row, col))) { sudoku.Metadata[row][col] = false; i++; } else { sudoku.Solution[row][col] = 0; } } }
/// <summary> /// Checks if sudoku is valid /// </summary> /// <param name="sudoku">Sudoku to check</param> /// <returns>Whether the sudoku is valid</returns> public override bool IsValid(Sudoku sudoku) { bool isCorrect = true; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if ((!DataValidator.RowVal.IsValid(Sudoku.GetRow(sudoku.Data, i)) || !DataValidator.ColumnVal.IsValid(Sudoku.GetColumn(sudoku.Data, j)) || !DataValidator.SquareVal.IsValid(Sudoku.GetSquare(sudoku.Data, i, j)) || UIManager.RichTextBoxes[i, j].Text == "") && !UIManager.RichTextBoxes[i, j].ReadOnly) //if has number and is not ReadOnly { UIManager.RichTextBoxes[i, j].BackColor = Color.Red; isCorrect = false; } } } return(isCorrect); }
/// <summary> /// Generates new sudoku /// </summary> /// <param name="sudoku"></param> public static Sudoku Generate(Sudoku sudoku) { //Whether randomize sudoku (recursion) Randomize(sudoku); DateTime dt = DateTime.Now; bool timeRanOut = false; for (int row = 0; row < 9;) { for (int col = 0; ;) { //If is generating longer than # seconds if (DateTime.Now.CompareTo(dt.AddSeconds(5)) > 0) { timeRanOut = true; break; } //if is editable, else ++ if (!sudoku.Metadata[row][col]) { if (col == 8) { col = 0; row++; break; } else { col++; } continue; } else { sudoku.Solution[row][col]++; } //if is valid, else go back if 9 or uneditable if (DataValidator.RowVal.IsValid(Sudoku.GetRow(sudoku.Solution, row)) && DataValidator.ColumnVal.IsValid(Sudoku.GetColumn(sudoku.Solution, col)) && DataValidator.SquareVal.IsValid(Sudoku.GetSquare(sudoku.Solution, row, col))) { if (col == 8) { col = 0; row++; break; } else { col++; } } else { while (sudoku.Solution[row][col] == 9 || !sudoku.Metadata[row][col]) { if (sudoku.Metadata[row][col]) { sudoku.Solution[row][col] = 0; } if (col == 0) { col = 8; if (row != 0) { row--; } } else { col--; } } } } //If time ran out if (timeRanOut) { break; } } //If time ran out, then Sudoku has no solution if (timeRanOut) { sudoku = Generate(new Sudoku()); } //return valid sudoku return(sudoku); }