private void button1_Click(object sender, EventArgs e) { int[,] nums = new int[9, 9]; for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) nums[i, j] = int.Parse(controls[i, j].Text == string.Empty ? "0" : controls[i, j].Text); var startTime = DateTime.Now; var maze = new SudokuSolver.Maze(nums); try { IList<StepInfo> steps = null; nums = new SudokuSolver.SudokuSolver(maze).SolveAndGetSteps(out steps); if (Logger != null) ShowLog(steps); var timeElapsed = DateTime.Now.Subtract(startTime); updateUI(nums); timeElapsedLbl.Text = timeElapsed.TotalMilliseconds + " ms."; } catch (Exception ex) { MessageBox.Show("logic error :( "); } }
static void Main(string[] args) { int[,] sudoku = new int[, ] { //Ukazkove { 0, 2, 0, 0, 8, 0, 0, 3, 0 }, { 3, 0, 0, 0, 7, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 1, 9, 0 }, { 0, 0, 6, 2, 4, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 1 }, { 2, 0, 8, 0, 3, 6, 0, 0, 0 }, { 4, 0, 0, 0, 6, 0, 0, 0, 0 }, { 5, 0, 2, 4, 0, 0, 0, 0, 0 }, { 0, 6, 0, 0, 9, 7, 8, 0, 4 } }; SudokuSolver solver = new SudokuSolver(sudoku); solver.Solve(); solver.VykresliSudoku(); Console.WriteLine(); solver.VytiskniKandidaty(); Console.ReadLine(); }
private void _solve_Click(object sender, EventArgs e) { try { var size = GetSize(); textBox1.Clear(); var solver = new SudokuSolver.SudokuSolver(); var problem = new Square(size); problem = FillProblem(problem, size); var result = solver.Solve(problem); if (result.Solved) { FillGrid(result); } else { MessageBox.Show("Not solved!!"); } } catch (Exception exception) { MessageBox.Show("Необработанное исключение: " + exception.Message); } }
public void SudokuSolver_SolvesPuzzle() { var puzzle = PuzzleMaker.MakeSolvablePuzzle(); var solver = new SudokuSolver.SudokuSolver(puzzle); var solved = solver.Solve(); Assert.IsTrue(solved); Assert.IsTrue(solver.Puzzle.IsSolved()); }
public void SudokuSolver_ClonesArgumentToAvoidModifyingOriginal() { var puzzle = new SudokuPuzzle(); puzzle.SetValue(1, 4, SudokuValue.NINE); var solver = new SudokuSolver.SudokuSolver(puzzle); puzzle.ClearBoard(); Assert.AreEqual(SudokuValue.NINE, solver.Puzzle.GetValue(1, 4)); }
//[Test] public void SolveTest() { // diabolical var initialCells = "000041000060000200000000000320600000000050041700000000000200300048000000501000000".StringToCells(); var resultCells = new SudokuSolver().Solve(initialCells); //string result = resultCells.CellsToString(); Assert.IsTrue(resultCells.CellsToString() == "872941563169573284453826197324617859986352741715498632697284315248135976531769428"); }
static void Main(string[] args) { Config config = new Config("../../../appsettings.json"); string path = (string)config["SudokuSolver:Path"]; ISudokuAccess sudokuAccess = new NumPySudokuAccess(9); IList <Sudoku> readPuzzles = sudokuAccess.ReadPuzzles(path); IList <Sudoku> readSolutions = sudokuAccess.ReadSolutions(path); IList <string> strategies = (IList <string>)config["SudokuSolver:Strategies"]; List <ISolvingStrategy> solvingStrategies = new List <ISolvingStrategy>(); if (strategies.Count < 1) { solvingStrategies.Add(new HiddenSingleStrategy()); solvingStrategies.Add(new NakedSingleStrategy()); } if (strategies.Contains("NakedSingle")) { solvingStrategies.Add(new NakedSingleStrategy()); } if (strategies.Contains("HiddenSingle")) { solvingStrategies.Add(new HiddenSingleStrategy()); } SudokuSolver solver = new SudokuSolver(solvingStrategies); for (int i = 0; i < readPuzzles.Count; i++) { Sudoku possibleSolution = solver.Solve(readPuzzles[i]); Sudoku actualSolution = readSolutions[i]; bool solved = actualSolution.Equals(possibleSolution); Console.WriteLine("Sudoku " + i + ": "); Console.WriteLine("Calculated Solution:"); Console.WriteLine(possibleSolution.ToString()); Console.WriteLine("Actual Solution:"); Console.WriteLine(actualSolution.ToString()); Console.WriteLine("Result: " + (solved ? "SOLVED" : "FAIL")); Console.WriteLine("-------"); } }
public void ThreadJob(int inputVal, string thrName, CancellationToken token, CancellationTokenSource tokenSource, string inputGrid) { string project_path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())); string threadOut_path = project_path + "\\" + thrName + ".txt"; showStatus("Kisitlari kullanarak cozuyor..."); var myGrid = SudokuSolver.parse_grid(inputGrid, thrName, threadOut_path, token, tokenSource); showStatus("Arama algoritmasi calisiyor..."); Stopwatch stopWatch2 = new Stopwatch(); stopWatch2.Start(); SudokuSolver.search(myGrid, thrName, threadOut_path, token, tokenSource, stopWatch2); showStatus("Cozuldu!!!"); }
static void Main(string[] args) { if (args.Length > 0) { //Any sudoku passed as argument foreach (string sudoku in args) { SudokuSolver s = new SudokuSolver { Board = new StringBuilder(sudoku) }; s.SolveBoard(); s.PrintBoard(); Console.WriteLine(); } } else { //Project euler problem 96, parsing sudoku.txt before string sudokusFile = System.IO.File.ReadAllText("sudokus.txt"); string[] sudokus = Regex.Split(sudokusFile, @"(Grid\s\d{2})\n((?:\d{9}\n?){9})"); double output = 0; var timer = System.Diagnostics.Stopwatch.StartNew(); for (int number = 2; number < sudokus.Length; number += 3) { string sudoku = sudokus[number].Replace("\n", ""); SudokuSolver s = new SudokuSolver { Board = new StringBuilder(sudoku) }; s.SolveBoard(); output += int.Parse(s.Board.ToString().Substring(0, 3)); } timer.Stop(); Console.WriteLine("Solved {0} sudokus in {1} ms and the " + "sum of the 3-digit numbers found at " + "the top left is {2}.", sudokus.Length / 3, timer.ElapsedMilliseconds, output); } Console.ReadLine(); }
public void IsPossibleTest() { var cells = "000580000500000900240079005005040710010000040082010500300120098001000006000036000".StringToCells(); bool isPossible = SudokuSolver.IsPossible(cells, new Cell(0, 0), 1); Assert.IsTrue(isPossible); isPossible = SudokuSolver.IsPossible(cells, new Cell(0, 0), 2); Assert.IsFalse(isPossible); isPossible = SudokuSolver.IsPossible(cells, new Cell(0, 0), 3); Assert.IsFalse(isPossible); isPossible = SudokuSolver.IsPossible(cells, new Cell(5, 5), 5); Assert.IsFalse(isPossible); isPossible = SudokuSolver.IsPossible(cells, new Cell(8, 4), 9); Assert.IsTrue(isPossible); }
private static void SolveSudokuAndDisplayResults(string sudokuPath) { PrintHeader(sudokuPath); Sudoku initial; try { initial = SudokuParser.Parse(sudokuPath); } catch (Exception e) { Console.WriteLine(); Console.WriteLine($"Error: {e.Message}"); Console.WriteLine(); return; } var maxDepth = SudokuInspector.CountEmptyCells(initial); PrintSudoku(initial); Console.WriteLine($"Solving puzzle '{sudokuPath}'..."); var(solution, functionCallCount, elapsedTimeInMs) = SudokuSolver.Solve(initial); if (solution != null) { Console.WriteLine("Done! Solution:"); PrintSudoku(solution); Console.WriteLine($"Puzzle '{sudokuPath}' solved in {elapsedTimeInMs:F3}ms."); Console.WriteLine($"Solution found at depth {maxDepth}, in {functionCallCount} function calls."); Console.WriteLine($"Validity check: {(SudokuValidator.Validate(solution) ? "Passed!" : "Failed.")}"); } else { Console.WriteLine("Puzzle could not be solved. Please make sure the puzzle was defined properly."); } Console.WriteLine(); }
static void Main(string[] args) { int?[,] initialBoard = new int?[, ] { { null, null, null, 2, null, null, null, null, 1 }, { null, null, 3, 8, null, null, null, 9, null }, { 7, null, 4, null, 9, 5, 8, null, null }, { 2, 8, null, null, null, null, null, null, 5 }, { null, null, null, null, null, null, null, null, null }, { 6, null, null, null, null, null, null, 7, 3 }, { null, null, 2, 7, 5, null, 6, null, 9 }, { null, 7, null, null, null, 6, 4, null, null }, { 5, null, null, null, null, 9, null, null, null } }; SudokuBoard board = new SudokuBoard(initialBoard); bool solved = SudokuSolver.Solve(board); Console.WriteLine($"{(solved ? "SOLVED" : "UNSOLVABLE")}"); }
public void GetValuesFromCellsTest() { var cells = "000580000500000900240079005005040710010000040082010500300120098001000006000036000".StringToCells(); List <int> a = SudokuSolver.GetValues(cells, new Cell(0, 0), Orientation.Vertical); Assert.IsTrue(a.Where(i => (new List <int>() { 5, 2, 3 }).Contains(i)).Count() == 3); a = SudokuSolver.GetValues(cells, new Cell(6, 1), Orientation.Horizontal); Assert.IsTrue(a.Where(i => (new List <int>() { 5, 9 }).Contains(i)).Count() == 2); a = SudokuSolver.GetValues(cells, new Cell(4, 3), Orientation.Vertical); Assert.IsTrue(a.Where(i => (new List <int>() { 8, 7, 4, 1, 2, 3 }).Contains(i)).Count() == 6); }
public void SolveMultipleTest() { var solver = new SudokuSolver(); // input, expected output var games = new List <Tuple <string, string> >(); games.Add(Tuple.Create <string, string>("000580000500000900240079005005040710010000040082010500300120098001000006000036000", "193584627578261934246379185635948712719652843482713569367125498821497356954836271")); games.Add(Tuple.Create <string, string>("400010000000309040070005009000060021004070600190050000900400070030608000000030006", "459716382612389745873245169387964521524173698196852437965421873731698254248537916")); // moderate games.Add(Tuple.Create <string, string>("309000400200709000087000000750060230600904008028050041000000590000106007006000104", "369218475215749863487635912754861239631924758928357641173482596542196387896573124")); // tough games.Add(Tuple.Create <string, string>("000704005020010070000080002090006250600070008053200010400090000030060090200407000", "981724365324615879765983142197836254642571938853249716476398521538162497219457683")); // diabolical games.Add(Tuple.Create <string, string>("000041000060000200000000000320600000000050041700000000000200300048000000501000000", "872941563169573284453826197324617859986352741715498632697284315248135976531769428")); foreach (var game in games) { var cells = solver.Solve(game.Item1.StringToCells()); string output = cells.CellsToString(); Console.WriteLine(game.Item1 + " --> " + output); Assert.IsTrue(output == game.Item2); } }
public static void Main(string[] args) { var board = LoadFileToArray("input.txt"); var solver = new SudokuSolver(board); var stopwatch = Stopwatch.StartNew(); var result = solver.Solve(); stopwatch.Stop(); var timeTaken = stopwatch.ElapsedMilliseconds; if (result) { solver.PrintBoard(); Console.WriteLine($"Took {timeTaken}ms."); } else { Console.WriteLine("Cannot solve."); } }
public void GetBlockTest() { var block = SudokuSolver.GetBlock(1, 1); Assert.IsTrue(block == Block.B00); block = SudokuSolver.GetBlock(5, 2); Assert.IsTrue(block == Block.B10); block = SudokuSolver.GetBlock(8, 2); Assert.IsTrue(block == Block.B20); block = SudokuSolver.GetBlock(8, 0); Assert.IsTrue(block == Block.B20); block = SudokuSolver.GetBlock(2, 3); Assert.IsTrue(block == Block.B01); block = SudokuSolver.GetBlock(4, 5); Assert.IsTrue(block == Block.B11); block = SudokuSolver.GetBlock(7, 4); Assert.IsTrue(block == Block.B21); block = SudokuSolver.GetBlock(1, 8); Assert.IsTrue(block == Block.B02); block = SudokuSolver.GetBlock(4, 6); Assert.IsTrue(block == Block.B12); block = SudokuSolver.GetBlock(8, 2); Assert.IsTrue(block == Block.B20); block = SudokuSolver.GetBlock(8, 8); Assert.IsTrue(block == Block.B22); }
public static List <Cell> StringToCells(this string values) { var cells = new List <Cell>(81); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { var cell = new Cell(); cell.X = i; cell.Y = j; cell.Initial = true; int value = Convert.ToInt32(values[(9 * j) + i].ToString()); cell.Value = value == 0 ? (int?)null : value; cell.Block = SudokuSolver.GetBlock(cell.X, cell.Y); cells.Add(cell); } } return(cells); }
public void SudokuSolver_ExpectsPuzzleInConstructor() { var solver = new SudokuSolver.SudokuSolver(null); }
public void GetBlockInvalidValuesTest() { var block = SudokuSolver.GetBlock(1, 9); Assert.IsTrue(block == Block.B00); }
public void SolveMultipleTest() { var solver = new SudokuSolver(); // input, expected output var games = new List<Tuple<string, string>>(); games.Add(Tuple.Create<string, string>("000580000500000900240079005005040710010000040082010500300120098001000006000036000", "193584627578261934246379185635948712719652843482713569367125498821497356954836271")); games.Add(Tuple.Create<string, string>("400010000000309040070005009000060021004070600190050000900400070030608000000030006", "459716382612389745873245169387964521524173698196852437965421873731698254248537916")); // moderate games.Add(Tuple.Create<string, string>("309000400200709000087000000750060230600904008028050041000000590000106007006000104", "369218475215749863487635912754861239631924758928357641173482596542196387896573124")); // tough games.Add(Tuple.Create<string, string>("000704005020010070000080002090006250600070008053200010400090000030060090200407000", "981724365324615879765983142197836254642571938853249716476398521538162497219457683")); // diabolical games.Add(Tuple.Create<string, string>("000041000060000200000000000320600000000050041700000000000200300048000000501000000", "872941563169573284453826197324617859986352741715498632697284315248135976531769428")); foreach (var game in games) { var cells = solver.Solve(game.Item1.StringToCells()); string output = cells.CellsToString(); Console.WriteLine(game.Item1 + " --> " + output); Assert.IsTrue(output == game.Item2); } }
private void SetupSolverCallback(SudokuSolver solver, int delay) { solver.Callback += (solverInstance, args) => { // calculate index int index = args.Row * 9 + args.Column; var value = args.Value == SudokuValue.UNASSIGNED ? string.Empty : ((int)args.Value).ToString(); this.Cells[index].Invoke(new Action(() => { this.Cells[index].Text = value; })); args.Delay = delay; }; }
private void solveButton_Click(object sender, EventArgs e) { var puzzle = this.LoadPuzzleState(); var solver = new SudokuSolver(puzzle); int delay = this.speedSlider.Maximum - this.speedSlider.Value; this.SetupSolverCallback(solver, delay); this.speedSlider.Enabled = false; this.solveButton.Enabled = false; this.ProcessingThread = new Thread(new ThreadStart(() => { // solve the puzzle! if (!solver.Solve()) { MessageBox.Show(NOT_SOLVEABLE_MESSAGE); } this.EnableControl(this.speedSlider); this.EnableControl(this.solveButton); this.clearButton.Invoke(new Action(() => { this.clearButton.Text = CLEAR_LABEL; })); })); this.clearButton.Text = CANCEL_LABEL; this.ProcessingThread.Start(); }