コード例 #1
0
        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]);
            }
        }
コード例 #2
0
        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());
        }
コード例 #3
0
        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);
        }