public void AssertCanSolvePuzzle003() { SudokuPuzzle puzzle = SudokuFactory.Create(new[, ] { { 0, 0, 0, 7, 1, 6, 0, 0, 0 }, { 6, 0, 0, 9, 0, 2, 0, 0, 7 }, { 0, 0, 8, 0, 0, 0, 9, 0, 0 }, { 2, 0, 1, 0, 0, 0, 5, 0, 6 }, { 0, 8, 0, 0, 0, 0, 0, 1, 0 }, { 4, 0, 5, 0, 0, 0, 8, 0, 2 }, { 0, 0, 3, 0, 0, 0, 1, 0, 0 }, { 1, 0, 0, 4, 0, 9, 0, 0, 3 }, { 0, 0, 0, 1, 3, 5, 0, 0, 0 }, }); SudokuPuzzle solution = SudokuFactory.Create(new[, ] { { 5, 3, 9, 7, 1, 6, 2, 4, 8 }, { 6, 1, 4, 9, 8, 2, 3, 5, 7 }, { 7, 2, 8, 5, 4, 3, 9, 6, 1 }, { 2, 7, 1, 8, 9, 4, 5, 3, 6 }, { 3, 8, 6, 2, 5, 7, 4, 1, 9 }, { 4, 9, 5, 3, 6, 1, 8, 7, 2 }, { 9, 4, 3, 6, 7, 8, 1, 2, 5 }, { 1, 5, 7, 4, 2, 9, 6, 8, 3 }, { 8, 6, 2, 1, 3, 5, 7, 9, 4 }, }); AssertIsSolved(puzzle, solution); }
public GameBoard() { this.Rows = SudokuFactory.BuildSingle <Row>().ToList(); this.Columns = SudokuFactory.BuildSingle <Column>().ToList(); this.subGrids = SudokuFactory.BuildDouble <SubGrid>(); this.AlignCollections(); }
private static void Main() { SudokuPuzzle puzzle = SudokuFactory.Create(new[, ] { { 0, 0, 3, 0, 9, 5, 0, 0, 0 }, { 0, 0, 6, 2, 0, 0, 4, 0, 0 }, { 0, 0, 0, 1, 0, 0, 0, 8, 7 }, { 0, 0, 4, 7, 0, 0, 5, 0, 3 }, { 0, 2, 0, 0, 0, 0, 0, 4, 0 }, { 3, 0, 7, 0, 0, 6, 1, 0, 0 }, { 2, 4, 0, 0, 0, 1, 0, 0, 0 }, { 0, 0, 9, 0, 0, 2, 8, 0, 0 }, { 0, 0, 0, 5, 8, 0, 9, 0, 0 }, }); Console.WriteLine(puzzle); Console.WriteLine("Pieces Left: " + puzzle.RemainingPieces); puzzle.Solve(); Console.WriteLine(puzzle); Console.WriteLine("Pieces Left: " + puzzle.RemainingPieces); Console.ReadLine(); }
public void Solve6x6Test() { var puzzle = @"100000 020000 003000 000400 000050 000000 ".Replace(" ", ""); var sudoku = SudokuFactory.CreateFromString(puzzle); Assert.AreEqual(puzzle, sudoku.ToString()); var solver = new SudokuSolver(sudoku); solver.Solve(); var solution = @"134265 526134 243516 615423 461352 352641 ".Replace(" ", ""); Assert.AreEqual(solution, sudoku.ToString()); Assert.IsTrue(sudoku.IsSolved()); }
/// <summary> /// Creates the tiles. /// </summary> private void CreateTiles() { foreach (var position in SudokuFactory.Box(this.tiles.GetLength(0), this.tiles.GetLength(1))) { this.tiles[position.Item1, position.Item2] = new SudokuTile(position.Item1, position.Item2, this.maximumValue, this.language); } }
public void SudukoBoard_Solve_SmallWithSolution() { // Arrange SudokuBoard board = SudokuFactory.SizeAndBoxes(4, 4, 2, 2, new[] { "0003", "0004", "1000", "4000" }); string[] tileDefinitions = new[] { "2413", "3124", "1342", "4231" }; // Act IEnumerable <SudokuBoard> solutions = board.Solve(); // Assert Assert.Single(solutions); Assert.Equal(tileDefinitions, solutions.Single().TileDefinitions); }
/// <summary> /// Initializes a new instance of the <see cref="SudokuBoard"/> class. /// </summary> /// <param name="copy">The copy.</param> /// <param name="language">The language.</param> private SudokuBoard(SudokuBoard copy, ILanguage language) { this.language = language; this.maximumValue = copy.maximumValue; this.tiles = new SudokuTile[copy.Width, copy.Height]; this.CreateTiles(); // Copy the tile values foreach (var position in SudokuFactory.Box(this.Width, this.Height)) { this.tiles[position.Item1, position.Item2] = new SudokuTile(position.Item1, position.Item2, this.maximumValue, this.language) { Value = copy.tiles[position.Item1, position.Item2].Value }; } // Copy the rules foreach (var rule in copy.rules) { var ruleTiles = new HashSet <SudokuTile>(); foreach (var tile in rule) { ruleTiles.Add(this.tiles[tile.X, tile.Y]); } this.rules.Add(new SudokuRule(ruleTiles, rule.Description)); } }
public void SerializationCopyTest2() { // arrange var puzzle = @"100008400 020004900 903256000 600000571 410805062 532000004 000582709 001300040 008100005 ".Replace(" ", ""); var sudoku = SudokuFactory.CreateFromString(puzzle); var solver = new SudokuSolver(sudoku); // act solver.Solve(); var sudokuCopy = sudoku.DeepCopy(); sudokuCopy.SetCellValue(0, 1, 0, "manual"); sudokuCopy.UpdatePossibleValuesInAllRegions(); // assert Assert.IsTrue(sudoku.IsSolved()); Assert.IsFalse(sudokuCopy.IsSolved()); }
public void Solve3Test() { var puzzle = @"293040100 516230740 847156000 354002690 600415000 000900000 000394802 000600005 000521000 ".Replace(" ", ""); var sudoku = SudokuFactory.CreateFromString(puzzle); Assert.AreEqual(puzzle, sudoku.ToString()); var solver = new SudokuSolver(sudoku); solver.Solve(); var solution = @"293748156 516239748 847156239 354872691 629415387 781963524 165394872 432687915 978521463 ".Replace(" ", ""); Assert.AreEqual(solution, sudoku.ToString()); Assert.IsTrue(sudoku.IsSolved()); }
/// <summary> /// Gets generated sudoku from cache. /// Generates and caches if missing. /// </summary> private ISudokuGrid GetGeneratedSudoku(int seed) { if (!GeneratedSolutions.TryGetValue(seed, out ISudokuGrid grid)) { GeneratedSolutions[seed] = grid = SudokuFactory.GenerateSolved(_algorithm, seed); } return(grid); }
public static SudokuFactory GetInstance() { if (Sudoku == null) { Sudoku = new SudokuFactory(); Sudoku.GetSudoku(3); } return(Sudoku); }
/// <summary> /// Adds the boxes count. /// </summary> /// <param name="boxesX">The boxes X value.</param> /// <param name="boxesY">The boxes Y value.</param> internal void AddBoxesCount(int boxesX, int boxesY) { var sizeX = this.Width / boxesX; var sizeY = this.Height / boxesY; var boxes = SudokuFactory.Box(sizeX, sizeY); foreach (var position in boxes) { var boxTiles = this.TileBox(position.Item1 * sizeX, position.Item2 * sizeY, sizeX, sizeY); this.CreateRule(this.language.GetWord("BoxAt") + position.Item1 + ", " + position.Item2 + ")", boxTiles); } }
public void SolveEmpty4x4Test() { var puzzle = @"0000 0000 0000 0000 ".Replace(" ", ""); var sudoku = SudokuFactory.CreateFromString(puzzle); var solver = new SudokuSolver(sudoku); solver.Solve(); Assert.IsTrue(sudoku.IsSolved()); }
public void GenerateSolved_CheckSpecificSeed() { var validSudoku = SudokuGenerator.Original.SudokuFactory.GenerateSolved((SudokuGenerator.Original.Data.Enum.GenerationAlgorithm)_algorithm, 0); var testSudoku = SudokuFactory.GenerateSolved(_algorithm, 0); for (int index = 0; index < 81; index++) { int row = index / 9; int col = index % 9; byte value1 = validSudoku[row, col]; byte value2 = testSudoku[row, col]; Assert.AreEqual(value1, value2, "Generated sudoku doesn't match original."); } }
public void GenerateSolved_CheckSameSeed() { var sudoku1 = SudokuFactory.GenerateSolved(_algorithm, 0); var sudoku2 = SudokuFactory.GenerateSolved(_algorithm, 0); for (int index = 0; index < 81; index++) { int row = index / 9; int col = index % 9; byte value1 = sudoku1[row, col]; byte value2 = sudoku2[row, col]; Assert.AreEqual(value1, value2, "Same seed generated different sudokus."); } }
public void SudokoBoard_Solve_HyperWithSolution() { // Arrange // http://en.wikipedia.org/wiki/File:A_nonomino_sudoku.svg string[] areas = new string[] { "111233333", "111222333", "144442223", "114555522", "444456666", "775555688", "977766668", "999777888", "999997888" }; SudokuBoard board = SudokuFactory.ClassicWithSpecialBoxes(areas, new[] { "3.......4", "..2.6.1..", ".1.9.8.2.", "..5...6..", ".2.....1.", "..9...8..", ".8.3.4.6.", "..4.1.9..", "5.......7" }); string[] tileDefinitions = new[] { "358196274", "492567138", "613978425", "175842693", "826453719", "249731856", "987324561", "734615982", "561289347" }; // Act IEnumerable <SudokuBoard> solutions = board.Solve(); // Assert Assert.Single(solutions); Assert.Equal(tileDefinitions, solutions.First().TileDefinitions); }
public void SudokuBoard_Solve_NoSolutionFound() { // Arrange SudokuBoard board = SudokuFactory.SizeAndBoxes(4, 4, 2, 2, new[] { "0003", "0204", // the 2 must be a 1 on this row to be solvable "1000", "4000" }); // Act IEnumerable <SudokuBoard> solutions = board.Solve(); // Assert Assert.False(solutions.Any()); }
public List <StringBuilder> Print(Row row) { var lines = SudokuFactory.BuildSingle <StringBuilder>(5).ToList(); lines = this.CellPrinter.Print(row[0], lines); lines = this.CellPrinter.Print(row[1], lines); lines = this.CellPrinter.Print(row[2], lines); lines = this.SeparatorPrinter.Print(lines); lines = this.CellPrinter.Print(row[3], lines); lines = this.CellPrinter.Print(row[4], lines); lines = this.CellPrinter.Print(row[5], lines); lines = this.SeparatorPrinter.Print(lines); lines = this.CellPrinter.Print(row[6], lines); lines = this.CellPrinter.Print(row[7], lines); lines = this.CellPrinter.Print(row[8], lines); return(lines); }
private void SolveButton_Click(object sender, EventArgs e) { var groupedByRow = NumericUpDownControls .Select(n => TilesTableLayoutPanel.GetCellPosition(n)) .OrderBy(p => p.Row) .ThenBy(p => p.Column) .GroupBy(p => p.Row); string[] tileDefinitions = groupedByRow.Select(g => new string( g.Select(p => { NumericUpDown n = (NumericUpDown)TilesTableLayoutPanel.GetControlFromPosition(p.Column, p.Row); return(n.Value == 0 ? '.' : n.Value.ToString().Single()); }).ToArray() )).ToArray(); SudokuBoard board = SudokuFactory.ClassicWith3x3Boxes(tileDefinitions); // taking only 2 solutions for optimization IEnumerable <SudokuBoard> solutions = board.Solve().Take(2); if (!solutions.Any()) { MessageBox.Show("No solution available!"); return; } if (solutions.Count() > 1) { MessageBox.Show("Multiple solutions available!"); return; } string[] tileDefinion = solutions.Single().TileDefinitions; foreach (NumericUpDown numericUpDown in NumericUpDownControls) { var position = TilesTableLayoutPanel.GetCellPosition(numericUpDown); numericUpDown.Value = (int)char.GetNumericValue(tileDefinion[position.Row][position.Column]); } }
private void MenuOpenSingle_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "sudoku files (*.sud)|*.sud"; if (openFileDialog.ShowDialog() == true) { string sudokuString = File.ReadAllText(openFileDialog.FileName); Console.WriteLine(sudokuString); try { LoadSudoku(SudokuFactory.CreateSudoku(sudokuString.Trim()), openFileDialog.FileName); } catch (Exception) { MessageBox.Show("There was a problem opening the Sudoku.\nPlease ensure the Sudoku is solveable, and that the file is properly formatted.", "Failed opening Sudoku", MessageBoxButton.OK, MessageBoxImage.Error); statusLeft.Content = "Failed to open Sudoku. Please ensure the Sudoku is solveable, and the file is properly formatted."; } } }
void GetSukuInfor() { if (SudokuFactory.GetInstance().listSudoku.Count <= 0) { return; } _SudokuInfor = SudokuFactory.GetInstance().listSudoku[0]; _OldSudokuInfor = new int[_SudokuLength, _SudokuLength]; for (int i = 0; i < _SudokuLength; i++) { for (int j = 0; j < _SudokuLength; j++) { _OldSudokuInfor[i, j] = _SudokuInfor[i, j].Num; } } //int tempLevel = _Level[Random.Range(0, _Level.Length)]; int tempLevel = MagicStaticValue.GetInstance()._Level; Debug.Log("困难等级:" + tempLevel); for (int i = 0; i < tempLevel; i++) { int tempX = Random.Range(0, _SudokuLength); int tempY = Random.Range(0, _SudokuLength); if (_SudokuInfor[tempX, tempY].Num != 0) { _SudokuInfor[tempX, tempY].Num = 0; } else { i--; } } SudokuFactory.GetInstance().listSudoku.RemoveAt(0); SudokuFactory.GetInstance().GetSudoku(3); }
public void SudokoBoard_Solve_ClassicWithMultipleSolutions() { // Arrange SudokuBoard board = SudokuFactory.ClassicWith3x3Boxes(new[] { "...84...9", "..1.....5", "8...2.46.", // Removed a "1" on this line "7.8....9.", ".........", ".5....3.1", ".2491...7", "9.....5..", "3...84..." }); // Act IEnumerable <SudokuBoard> solutions = board.Solve(); // Assert Assert.Equal(20, solutions.Count()); }
public void SudokoBoard_Solve_ExtraZonesWithSolution() { // Arrange // http://en.wikipedia.org/wiki/File:Oceans_Hypersudoku18_Puzzle.svg SudokuBoard board = SudokuFactory.ClassicWith3x3BoxesAndHyperRegions(new[] { ".......1.", "..2....34", "....51...", ".....65..", ".7.3...8.", "..3......", "....8....", "58....9..", "69......." }); string[] tileDefinitions = new[] { "946832715", "152697834", "738451296", "819726543", "475319682", "263548179", "327985461", "584163927", "691274358" }; // Act IEnumerable <SudokuBoard> solutions = board.Solve(); // Assert Assert.Single(solutions); Assert.Equal(tileDefinitions, solutions.First().TileDefinitions); }
public void SudokuBoard_Solve_ClassicWithSolution() { // Arrange SudokuBoard board = SudokuFactory.ClassicWith3x3Boxes(new[] { "...84...9", "..1.....5", "8...2146.", "7.8....9.", ".........", ".5....3.1", ".2491...7", "9.....5..", "3...84..." }); string[] tileDefinitions = new[] { "632845179", "471369285", "895721463", "748153692", "163492758", "259678341", "524916837", "986237514", "317584926", }; // Act IEnumerable <SudokuBoard> solutions = board.Solve(); // Assert Assert.Single(solutions); Assert.Equal(tileDefinitions, solutions.First().TileDefinitions); }
public void SerializationCopyTest() { // arrange var puzzle = @"100008400 020004900 903256000 600000571 410805062 532000004 000582709 001300040 008100005 ".Replace(" ", ""); var sudoku = SudokuFactory.CreateFromString(puzzle); // act var sudokuCopy = sudoku.DeepCopy(); sudoku.SetCellValue(0, 1, 7, "manual"); // assert Assert.AreEqual(7, sudoku.GetCellValue(0, 1)); Assert.AreEqual(0, sudokuCopy.GetCellValue(0, 1)); }
public void Solve12x12Test() { var puzzle = @"5, 0, 0,10, 0,12, 0, 0, 1, 7, 0,11 0, 3, 2, 0, 0, 0, 0, 6, 0,10, 4, 0 1, 6, 0, 0, 0, 0, 0, 0, 0, 0,12, 0 3, 0, 0, 0,11, 0, 6, 0, 0, 0, 0, 8 0, 4, 0, 0,12, 1, 0, 7,11, 0, 0, 0 0, 0, 0, 6, 0,10, 4, 8, 0, 0, 0,12 2, 0, 0, 0,10, 4, 8, 0, 6, 0, 0, 0 0, 0, 0, 4, 6, 0, 7,11, 0, 0, 1, 0 11, 0, 0, 0, 0, 3, 0, 2, 0, 0, 0,10 0,10, 0, 0, 0, 0, 0, 0, 0, 0, 8, 4 0, 7, 8, 0, 9, 0, 0, 0, 0, 2,11, 0 6, 0, 9, 5, 0, 0,11, 0,10, 0, 0, 7 ".Replace(" ", ""); var sudoku = SudokuFactory.CreateFromString(puzzle); Assert.AreEqual(puzzle, sudoku.ToString().Replace(" ", "")); var solver = new SudokuSolver(sudoku); solver.Solve(); Assert.IsTrue(sudoku.IsSolved()); }
public void Solve4x4Test() { var puzzle = @"1000 0200 0030 0004 ".Replace(" ", ""); var sudoku = SudokuFactory.CreateFromString(puzzle); Assert.AreEqual(puzzle, sudoku.ToString()); var solver = new SudokuSolver(sudoku); solver.Solve(); var solution = @"1342 4213 2431 3124 ".Replace(" ", ""); Assert.AreEqual(solution, sudoku.ToString()); Assert.IsTrue(sudoku.IsSolved()); }
static void Main(string[] args) { /* * ReadFileFromTXT rt = new ReadFileFromTXT(); * rt.ReadFile(); * rt.PrintToConsole(); * Console.ReadKey(); */ // Lots of Sudoku here: // https://github.com/ralli/sudoku/blob/master/data/top1465.txt // https://www.telegraph.co.uk/news/science/science-news/9359579/Worlds-hardest-sudoku-can-you-crack-it.html // http://lipas.uwasa.fi/~timan/sudoku/ // 17-cell Sudoku (No valid Sudoku has fewer cells filled) // http://theconversation.com/good-at-sudoku-heres-some-youll-never-complete-5234 // Loading a Sudoku. This Sudoku uses '0' as indication of unfilled cell // You must translate to this format your self var s = SudokuFactory.CreateSudoku("042000005000632080080040200000000000715068340908350761091006000000020190006100050"); Console.WriteLine(s); // Print how many cells are filled/unfilled Console.WriteLine("Filled Cells : " + s.NumberOfFilledCells()); Console.WriteLine("Open Cells : " + s.NumberOfOpenCells()); Console.WriteLine(); // Do we have any obvious conflicts? // column, row, and local block conflicts? Console.WriteLine("s.HasConflicts() = " + s.HasConflicts()); // What numbers can be placed at position 0,0 (remember we count from 0 and not from 1) Console.Write("s.PossibleNumbers(0, 0) : "); foreach (var n in s.PossibleNumbers(0, 0)) { Console.Write(n + ", "); } Console.WriteLine(); // At what position are the fewest possible numbers? // (Easiest place to start filling in) int I = 0; int J = 0; int numberCount = s.OptimalPosition(out I, out J); Console.WriteLine($"{numberCount} numbers possible at optimal position {I},{J}"); if (numberCount == 1) { int n = s.PossibleNumbers(I, J)[0]; Console.WriteLine("Only one number possible, so you must fill this cell with the number " + n); // Lets do this, by the way s[I, J] = (byte)n; } Console.WriteLine(); // Solve the Sudoku (if possible) and dump the result var s_clone = s.Clone(); var s_solved = s_clone.Solve(); // You could try to give this delegate to Solve : (txt) => Console.WriteLine(txt) if (s_solved != null) { Console.WriteLine("SOLVED"); Console.WriteLine(s_solved); } else { Console.WriteLine("No solution exists"); } Console.WriteLine("The original Sudoku is still here:"); Console.WriteLine(s); Console.ReadLine(); }
public void SudokoBoard_Solve_SamuraiWithSolution() { // Arrange // http://www.freesamuraisudoku.com/1001HardSamuraiSudokus.aspx?puzzle=42 SudokuBoard board = SudokuFactory.Samurai(new[] { "6..8..9..///.....38..", "...79....///89..2.3..", "..2..64.5///...1...7.", ".57.1.2..///..5....3.", ".....731.///.1.3..2..", "...3...9.///.7..429.5", "4..5..1...5....5.....", "8.1...7...8.2..768...", ".......8.23...4...6..", "//////.12.4..9.//////", "//////......82.//////", "//////.6.....1.//////", ".4...1....76...36..9.", "2.....9..8..5.34...81", ".5.873......9.8..23..", "...2....9///.25.4....", "..3.64...///31.8.....", "..75.8.12///...6.14..", ".......2.///.31...9..", "..17.....///..7......", ".7.6...84///8...7..5." }); string[] tileDefinitions = new[] { "674825931000142673859", "513794862000897425361", "982136475000563189472", "357619248000425916738", "298457316000918357246", "146382597000376842915", "469578123457689534127", "821963754689231768594", "735241689231754291683", "000000512748396000000", "000000497163825000000", "000000368592417000000", "746921835976142368597", "238456971824563497281", "159873246315978152346", "815237469000625749813", "923164758000314825769", "467598312000789631425", "694385127000431586972", "581742693000257914638", "372619584000896273154" }; // Act IEnumerable <SudokuBoard> solutions = board.Solve(); // Assert Assert.Single(solutions); Assert.Equal(tileDefinitions, solutions.First().TileDefinitions); }
static void Main(string[] args) { var sudoku = SudokuFactory.GenerateSolved(0); Console.WriteLine(sudoku); }