private void SolveButton(object sender, TappedRoutedEventArgs e) { for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { TextBlock textBlock = (TextBlock)this.FindName("Block" + (row + 1).ToString() + (col + 1).ToString()); if (textBlock.FontSize == 12) { textBlock.Text = ""; textBlock.FontSize = 24; } } } sudoku.Solve(sudoku.Board); FillBoard(); if (sudoku.IsSolved()) { StatusMessage.Text = "Generated solution."; } else { StatusMessage.Text = "Block(s) incorrectly filled. Not currently solvable."; } }
public void Start() { PrintFrame(); sudoku = new Sudoku(PrintDigit, SaveAnswer); do { Console.ForegroundColor = ConsoleColor.Gray; GenerateRandom(difficulty); //LoadSudoku("sudoku.txt"); Console.ForegroundColor = ConsoleColor.Green; }while (!sudoku.Solve()); //PrintDigit(1, 2, 3); }
static void Main(string[] args) { try { File.Delete("./output.txt"); Log.Logger = new LoggerConfiguration() .WriteTo.File("./output.txt", outputTemplate: "{Message:lj}{NewLine}") .MinimumLevel.Debug() .CreateLogger(); Log.Information($"Starting Sudoku {DateTime.Now}"); Log.Information("===================================================="); Log.Information(" "); var puzzle = new Sudoku(); string jsonFile = null; if (args != null && args.Length > 0) { jsonFile = args[0]; } int iterations = 0; if (args != null && args.Length > 1) { int.TryParse(args[1], out iterations); } string outfile = null; if (args != null && args.Length > 2) { } //LoadGrid(puzzle, jsonFile); if (LoadFromGridJson(puzzle, jsonFile) == true) { puzzle.Solve(iterations); } } catch (Exception ex) { Log.Information(ex.Message); Log.Information(ex.StackTrace); } finally { Log.CloseAndFlush(); } }
static void Main(string[] args) { Sudoku s = new Sudoku("083020000041003825702000000020085036050301080430290050000000308896100540000040160"); Sudoku s2 = new Sudoku("400030000000600800000000001000050090080000600070200000000102700503000040900000000"); Sudoku s3 = new Sudoku("040010200000009070010000000000430600800000050000200000705008000000600300900000000"); Console.WriteLine(s); Console.WriteLine(s.Solve()); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine($"id sudoku: {sudokuNum}"); for (int i = 0; i < runs; i++) { Sudoku sudoku = new Sudoku(sudokuNum, filePath, searchType, true); List <Solution <byte> > solutions = sudoku.Solve(variableHeuristicType, valueHeuristicType); if (i == 0) { Sudoku.PrintSolutions(solutions); } Console.WriteLine(); } Console.ReadKey(); }
public void SolveAFile(string filePath) { string line; System.IO.StreamReader file = new System.IO.StreamReader(filePath); int currentLineIndex = 0; while ((line = file.ReadLine()) != null) { string[] sudokulineElements = line.Split(','); string inputProblem = sudokulineElements[0]; string expectedSolution = sudokulineElements[1]; Sudoku.Sudoku sudoku = new Sudoku.Sudoku(inputProblem); //RenderNewEntries(sudoku, Color.Black); //MessageBox.Show($"Problem {counter} loaded from DB. Click to Solve "); bool isSovled = sudoku.Solve(); string solution = sudoku.ToString(); if (isSovled) { if (!solution.Equals(expectedSolution)) { throw new Exception($"Sudoku solution does not match input solution excped: {expectedSolution}.\n Actual {solution}"); } Console.WriteLine("Solved a Problem. Rating=" + sudoku.DifficultyRating); } else { throw new Exception($"Sudoku could not be solved. Incompelete solution is {solution} expected solution was {expectedSolution}"); } currentLineIndex++; } file.Close(); }
static void Go(string puzzle) { var sudoku = new Sudoku(puzzle); var sw = Stopwatch.StartNew(); var solved = sudoku.Solve(); sw.Stop(); if (solved) { Console.WriteLine("{0} solved in {1:f2}ms", puzzle, sw.Elapsed.TotalMilliseconds); Console.WriteLine(sudoku.ToString()); } else { Console.WriteLine("No solution found."); } }
static void Main(string[] args) { Sudoku sudoku = new Sudoku(new int[, ] { { 8, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 3, 6, 0, 0, 0, 0, 0 }, { 0, 7, 0, 0, 9, 0, 2, 0, 0 }, { 0, 5, 0, 0, 0, 7, 0, 0, 0 }, { 0, 0, 0, 0, 4, 5, 7, 0, 0 }, { 0, 0, 0, 1, 0, 0, 0, 3, 0 }, { 0, 0, 1, 0, 0, 0, 0, 6, 8 }, { 0, 0, 8, 5, 0, 0, 0, 1, 0 }, { 0, 9, 0, 0, 0, 0, 4, 0, 0 } }); Console.WriteLine(sudoku); Console.WriteLine(sudoku.Solve()); }
private void button4_Click(object sender, EventArgs e) { FillGrid(sudokuGrid, Sudoku.Make()); FillGrid(sudokuGrid, Sudoku.Solve(cucc)); int[,] test = new int[9, 9]; int i = 0; for (int r = 0; r < 9; r++) { for (int c = 0; c < 9; c++) { test[r, c] = i; i++; } } // FillGrid(sudokuGrid, test); }
private void solveButton_Click(object sender, EventArgs e) { int[] board = new int[81]; for (int i = 0; i < 81; i++) { if (number_boxes[i].Text != "") { board[i] = int.Parse(number_boxes[i].Text); } } Sudoku sudoku = new Sudoku(board); sudoku.Solve(); board = sudoku.Board; for (int i = 0; i < 81; i++) { number_boxes[i].Text = board[i] == 0 ? "" : board[i].ToString(); } }
static void Main(string[] args) { /* * Sudoku game = new Sudoku("037060000205000800006908000" + * "000600024001503600650009000" + * "000302700009000402000050360"); */ string easy1 = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"; string easy2 = "619030040270061008000047621486302079000014580031009060005720806320106057160400030"; string medium1 = "037060000205000800006908000000600024001503600650009000000302700009000402000050360"; string diabolic1 = "000000000000003085001020000000507000004000100090000000500000073002010000000040009"; string diabolic2 = "900040000000010200370000005000000090001000400000705000000020100580300000000000000"; string unsolvable1 = "009028700806004005003000004600000000020713450000000002300000500900400807001250300"; string unsolvable2 = "090300001000080046000000800405060030003275600060010904001000000580020000200007060"; string unsolvable3 = "000041000060000020002000000320600000000050041700000002000000230048000000501002000"; string unsolvable4 = "900100004014030800003000090000708001800003000000000030021000070009040500500016003"; string unsolvable5 = "040100350000000000000205000000408900260000012050300007004000160600007000010080020"; string nakedSingles = "305420810487901506029056374850793041613208957074065280241309065508670192096512408"; string hiddenSingles = "002030008000008000031020000060050270010000050204060031000080605000000013005310400"; string unsolvableBox = "090300001000080046000000800405060030003275600060010904001000000580020000200007060"; Sudoku game = new Sudoku(unsolvableBox); Console.WriteLine(game.BoardAsText); game.Solve(); if (game.Solved) { Console.WriteLine(game.BoardAsText); } else { game.RecursiveSolveStarter(); } if (!game.Solved) { Console.WriteLine("Jag hittade tyvärr ingen lösning"); } }
static void Main(string[] args) { if (args.Length != 2) { Console.Write("There must be 2 arguments to the program. First 0 or 1 (0=debug version and 1=non debug version) and then file name full path to sudoku to be solved."); return; } else if (args[0] != "0" && args[0] != "1") { Console.Write("First argument to the program must be 0 (debug version) or 1 (non debug version)."); return; } if (args[0] == "0") { SudokuDebugVersion.Sudoku.Solve(new string[] { args[1] }); } else { Sudoku.Solve(new string[] { args[1] }); } }
/** * Exercise 9 */ public Sudoku Solve() { //lav en ny sudoku for hver mulig løsning og løs dem indtil den retunere null / ikke kan løses. int optimalX; int optimalY; int numOfPossibilities; if (!IsSolved()) { numOfPossibilities = OptimalPosition(out optimalX, out optimalY); for (int i = 0; i < numOfPossibilities; i++) { Sudoku s = Clone(); s.board[optimalX + optimalY * 9] = PossibleNumbers(optimalX, optimalY)[i]; Console.WriteLine(s); return(s.Solve()); } } return(null); }
static void Main(string[] args) { string Easy = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"; string Medium = "037060000205000800006908000000600024001503600650009000000302700009000402000050360"; string Evil = "000009000004030000001000348605007000010304050000200803592000600000070200000100000"; string nogo = "000099000004030000001000348605007000010304050000200803592000600000070200000100000"; string test1 = "305420810487901506029056374850793041613208957074065280241309065508670192096512408"; //Naked Singles string test2 = "002030008000008000031020000060050270010000050204060031000080605000000013005310400"; //Hidden Singles string test3 = "090300001000080046000000800405060030003275600060010904001000000580020000200007060"; //No solution bool alla = false; while (true) { Console.WriteLine("Vill du lösa ALLA pussel eller endast ENKLA?"); string svar = Console.ReadLine().ToUpper(); if (svar == "ALLA") { alla = true; break; } else if (svar == "ENKLA") { alla = false; break; } else { Console.Clear(); } Console.WriteLine("Felaktigt svar. försök igen."); } var game = new Sudoku(test3); game.Solve(alla); Console.ReadKey(); }
static void test(string Path, int Size) { Sudoku sd = new Sudoku(Size); sd.ReadFromFile(Path); sd.PrintBoard(); if (sd.Solve()) { Console.WriteLine(); Console.WriteLine(); sd.PrintBoard(); Console.WriteLine(); Console.WriteLine($"Iterations count {sd.Counter}"); Console.WriteLine(); } else { Console.WriteLine(); Console.WriteLine(); Console.WriteLine("No answers"); Console.WriteLine(); Console.WriteLine(); } }
public static int[][,] Create(int fieldsLeft) { int zeile, spalte, n, solved, counter, selectedIndependentBlocks; int[,] OutputField = new int[9, 9]; int[,] SolvedGame = new int[9, 9]; int[,] tmp = new int[9, 9]; int[,] independentBlocks = new int[6, 3] { { 0, 30, 60 }, { 0, 33, 57 }, { 3, 27, 60 }, { 3, 33, 54 }, { 6, 30, 54 }, { 6, 27, 57 } }; int[] ShuffeledArray; selectedIndependentBlocks = rnd.Next(5); counter = 0; solved = 0; for (int currentBlock = 0; currentBlock < 3; currentBlock++) { ShuffeledArray = Sudoku.Shuffle(); zeile = independentBlocks[selectedIndependentBlocks, currentBlock] / 9; spalte = independentBlocks[selectedIndependentBlocks, currentBlock] % 9; n = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { OutputField[spalte - (spalte % 3) + i, zeile - (zeile % 3) + j] = ShuffeledArray[n++]; SolvedGame[spalte - (spalte % 3) + i, zeile - (zeile % 3) + j] = OutputField[spalte - (spalte % 3) + i, zeile - (zeile % 3) + j]; } } } Sudoku.Solve(0, 0, 0, OutputField, ref counter, ref solved, 1); OutputField = solution[0]; n = 81 - fieldsLeft; Array.Copy(OutputField, tmp, OutputField.Length); do { zeile = rnd.Next(9); spalte = rnd.Next(9); solution.Clear(); tmp[zeile, spalte] = 0; counter = 0; solved = 0; Sudoku.Solve(0, 0, 0, tmp, ref counter, ref solved, 0); if (solved == 1) { Array.Copy(tmp, OutputField, OutputField.Length); n--; } else { Array.Copy(OutputField, tmp, OutputField.Length); } } while (n > 0); SolvedGame = solution[0]; Array.Copy(SolvedGame, tmp, SolvedGame.Length); zeile = rnd.Next(9); spalte = rnd.Next(9); solution.Clear(); tmp[zeile, spalte] = 0; counter = 0; solved = 0; Sudoku.Solve(0, 0, 0, tmp, ref counter, ref solved, 0); Array.Copy(SolvedGame, tmp, SolvedGame.Length); int[][,] SendBack = new int[2][, ]; SendBack[0] = OutputField; SendBack[1] = SolvedGame; return(SendBack); }
private void button1_Click(object sender, EventArgs e) { Sudoku.Solve(); }
public void NewGame() { int level; // Level if (rb_easy.IsChecked == true) { level = 55; } else if (rb_medium.IsChecked == true) { level = 30; } else if (rb_hard.IsChecked == true) { level = 0; } else { level = 30; } input = Sudoku.Create(level); int counter = 0; int solved = 0; Sudoku.Solve(0, 0, 0, input[0], ref counter, ref solved, 0); List <TextBox> TextBox = new List <TextBox>(); TextBox.Add(tb_1); TextBox.Add(tb_2); TextBox.Add(tb_3); TextBox.Add(tb_4); TextBox.Add(tb_5); TextBox.Add(tb_6); TextBox.Add(tb_7); TextBox.Add(tb_8); TextBox.Add(tb_9); TextBox.Add(tb_10); TextBox.Add(tb_11); TextBox.Add(tb_12); TextBox.Add(tb_13); TextBox.Add(tb_14); TextBox.Add(tb_15); TextBox.Add(tb_16); TextBox.Add(tb_17); TextBox.Add(tb_18); TextBox.Add(tb_19); TextBox.Add(tb_20); TextBox.Add(tb_21); TextBox.Add(tb_22); TextBox.Add(tb_23); TextBox.Add(tb_24); TextBox.Add(tb_25); TextBox.Add(tb_26); TextBox.Add(tb_27); TextBox.Add(tb_28); TextBox.Add(tb_29); TextBox.Add(tb_30); TextBox.Add(tb_31); TextBox.Add(tb_32); TextBox.Add(tb_33); TextBox.Add(tb_34); TextBox.Add(tb_35); TextBox.Add(tb_36); TextBox.Add(tb_37); TextBox.Add(tb_38); TextBox.Add(tb_39); TextBox.Add(tb_40); TextBox.Add(tb_41); TextBox.Add(tb_42); TextBox.Add(tb_43); TextBox.Add(tb_44); TextBox.Add(tb_45); TextBox.Add(tb_46); TextBox.Add(tb_47); TextBox.Add(tb_48); TextBox.Add(tb_49); TextBox.Add(tb_50); TextBox.Add(tb_51); TextBox.Add(tb_52); TextBox.Add(tb_53); TextBox.Add(tb_54); TextBox.Add(tb_55); TextBox.Add(tb_56); TextBox.Add(tb_57); TextBox.Add(tb_58); TextBox.Add(tb_59); TextBox.Add(tb_60); TextBox.Add(tb_61); TextBox.Add(tb_62); TextBox.Add(tb_63); TextBox.Add(tb_64); TextBox.Add(tb_65); TextBox.Add(tb_66); TextBox.Add(tb_67); TextBox.Add(tb_68); TextBox.Add(tb_69); TextBox.Add(tb_70); TextBox.Add(tb_71); TextBox.Add(tb_72); TextBox.Add(tb_73); TextBox.Add(tb_74); TextBox.Add(tb_75); TextBox.Add(tb_76); TextBox.Add(tb_77); TextBox.Add(tb_78); TextBox.Add(tb_79); TextBox.Add(tb_80); TextBox.Add(tb_81); int k = 0; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (input[0][i, j] == 0) { TextBox[k].Text = Convert.ToString(""); TextBox[k].Background = Brushes.Transparent; k++; } else { TextBox[k].Text = Convert.ToString(input[0][i, j]); TextBox[k].Background = Brushes.Transparent; k++; } } } }
private bool SolveByTryingPossibilities() { if (!IsAValidState()) { return(false); } int unsolvedCells = CountUnsolvedCells(); if (unsolvedCells == 0) { return(Solved); } SudokuCell[] linearArray = new SudokuCell[81]; for (int rowIndex = 0; rowIndex <= 8; rowIndex++) { for (int colIndex = 0; colIndex <= 8; colIndex++) { linearArray[rowIndex * 9 + colIndex] = sudokuCells[rowIndex][colIndex]; } } //Sorted array by remaining possibilities. Array.Sort(linearArray); foreach (var cell in linearArray) { int rowIndex = cell.RowIndex; int colIndex = cell.ColIndex; if (cell.Value == 0) { var remainingDigitsOnThisCell = cell.RemainingPossibilities; if (remainingDigitsOnThisCell.Count == 0) { Logger.WriteLine("Invalid situation. remainingDigitsOnThisCell.Count == 0 ", _cloningDepth); return(false); } //if (cloningDepth == 0) { Logger.WriteLine($"Evaluating Cell@{cell} ", _cloningDepth); Logger.WriteLine(ToStringFormatted(), _cloningDepth); } foreach (var possibility in remainingDigitsOnThisCell) { //Set this possibility and try to solve. Sudoku clone = new Sudoku(this.ToString()); clone._cloningDepth = this._cloningDepth + 1; clone.sudokuCells[rowIndex][colIndex].Value = possibility; if (clone.Solve()) { //if (cloningDepth == 0) { Logger.WriteLine($"Valid Solution found for Cell@{cell} new Value {possibility}", _cloningDepth); Logger.WriteLine($"\n{clone.ToStringFormatted()}", _cloningDepth); } cell.SolvingDifficulty = SolvingDifficulty.MaxDifficulty; cell.Value = possibility; CopyUnSolvedCellsFromSolution(clone); return(Solved); } } if (_cloningDepth == 0) { throw new Exception("No possibility worked. Root problem is not right"); } Logger.WriteLine($"No possibility worked for Cell@{cell}. Impossible situation. Should be backtraced.", _cloningDepth); return(Notsolved); } } return(Notsolved); }