Пример #1
0
        private void solveButton_Click(object sender, EventArgs e)
        {
            var rowString = "";

            foreach (var box in rowTextBoxs)
            {
                rowString += box.Value.Text + ";";
            }
            var colString = "";

            foreach (var box in rowTextBoxs)
            {
                colString += box.Value.Text + ";";
            }

            var puzzle = PuzzleFactory.Instance().MakePuzzle(numberOfRows, numberOfColumns, rowString, colString);

            PuzzleSolver solver = new PuzzleSolver(puzzle);

            solver.Solve();

            if (solver.Solve())
            {
                UpdateDisplay(puzzle);
            }
            else
            {
                MessageBox.Show(solver.ErrorMessage);
            }
        }
Пример #2
0
        protected void SolveButton_Click(object sender, EventArgs e)
        {
            //try
            //{
            Puzzle puzzle;

            if (!TestPuzzles.Puzzles.TryGetValue(PuzzleDropDown.Text, out puzzle))
            {
                throw new InvalidPuzzleException("Select Valid Puzzle from list");
            }
            else
            {
                PuzzleSolver solver = new PuzzleSolver(puzzle);
                solver.Solve();

                if (solver.Solve())
                {
                    UpdateDisplay(puzzle);
                }
                else
                {
                    //MessageBox.Show(solver.ErrorMessage);
                }
            }
            //}
            //catch (InvalidPuzzleException ex)
            //{
            //     MessageBox.Show(ex.Message);
            //}
        }
Пример #3
0
        public void TestMethod5()
        {
            int input    = 29;
            int solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 4);
        }
Пример #4
0
        public void TestMethod4()
        {
            int input    = 1024;
            int solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 31);
        }
Пример #5
0
        public void Solve_Test2()
        {
            var sb = new StringBuilder();

            sb.AppendLine("0 2 3 0");
            sb.AppendLine("0 0 0 0");
            sb.AppendLine("3 1 2 4");
            sb.AppendLine("0 0 0 0");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var solver = new PuzzleSolver(puzzle);
            var result = solver.Solve();

            Assert.True(result);

            sb.Clear();
            sb.AppendLine("4 2 3 1");
            sb.AppendLine("1 3 4 2");
            sb.AppendLine("3 1 2 4");
            sb.AppendLine("2 4 1 3");
            var expectedSolution = sb.ToString();

            Assert.Equal(expectedSolution, puzzle.ToString());
        }
Пример #6
0
        public void TestMethod4()
        {
            string input    = "91212129";
            int    solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 9);
        }
Пример #7
0
        public void TestMethod3()
        {
            string input    = "1234";
            int    solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 0);
        }
Пример #8
0
        public void TestMethod10()
        {
            int input    = 57;
            int solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 8);
        }
Пример #9
0
        public void Solve_WithPuzzleConstructor_SolvesPuzzle(Puzzle puzzle)
        {
            var solver = new PuzzleSolver(puzzle);

            solver.Solve();
            _AssertPuzzleSolved(puzzle);
        }
Пример #10
0
        public void Solve_WithOneSolutionUsingDiagonals_Works()
        {
            var puzzle = new PuzzleWithPossibleValues(new int?[][] {
                new int?[] { null, null, null, null, null, null, null, 1, 2 },
                new int?[] { null, 5, null, null, 9, null, null, null, null },
                new int?[] { null, null, null, null, null, 8, null, null, null },
                new int?[] { null, 3, null, null, null, null, 5, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, 9, null },
                new int?[] { null, null, null, null, 4, 3, 9, null, null },
                new int?[] { 6, null, 1, null, null, null, 7, null, null },
                new int?[] { 2, null, null, null, null, null, null, null, null },
            });
            var magicSquares = new Box[] {
                new Box(new Coordinate(3, 3), 3),
            };
            var solver = new PuzzleSolver <PuzzleWithPossibleValues>(
                new DynamicRuleKeeper(
                    new IRule[] {
                new RowUniquenessRule(),
                new ColumnUniquenessRule(),
                new BoxUniquenessRule(),
                new MagicSquaresRule(puzzle.Size, magicSquares, includeDiagonals: true),
            }));

            var solution = solver.Solve(puzzle);

            PuzzleTestUtils.AssertStandardPuzzleSolved(solution);
            MagicSquareTests.AssertMagicSquaresSatisfied(
                solution, magicSquares, expectedSum: 15, verifyDiagonals: false);
        }
Пример #11
0
        public void Solve_WithManySolutions_Works()
        {
            var puzzle = new PuzzleWithPossibleValues(new int?[][] {
                new int?[] { null, null, 9, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, 3, 5, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
            });
            var magicSquares = new Box[] {
                new Box(new Coordinate(0, 0), 3),
                new Box(new Coordinate(3, 3), 3),
                new Box(new Coordinate(6, 6), 3),
            };
            var solver = new PuzzleSolver <PuzzleWithPossibleValues>(
                new DynamicRuleKeeper(
                    new IRule[] {
                new RowUniquenessRule(),
                new MagicSquaresRule(puzzle.Size, magicSquares, includeDiagonals: false),
            }));

            var solution = solver.Solve(puzzle);

            Assert.Equal(0, solution.NumEmptySquares);
            MagicSquareTests.AssertMagicSquaresSatisfied(
                solution, magicSquares, expectedSum: 15, verifyDiagonals: false);
        }
Пример #12
0
        static void Main(string[] args)
        {
            var mapResultToEquation = new SortedDictionary <int, List <string> >();

            var puzzleSolver = new PuzzleSolver(new int[] { 1, 2, 3, 4 });

            puzzleSolver.CompletedValue += delegate(object?sender, CompletedValueArgs e)
            {
                if (!mapResultToEquation.ContainsKey(e.Value))
                {
                    mapResultToEquation.Add(e.Value, new List <string>()
                    {
                        e.Equation
                    });

                    if (mapResultToEquation.SolvedAllValues(puzzleSolver.StartValue, puzzleSolver.EndValue))
                    {
                        Console.WriteLine("Completed! :)");
                        puzzleSolver.Cancel();
                    }
                }
                else
                {
                    mapResultToEquation[e.Value].Add(e.Equation);
                }
            };
            puzzleSolver.Solve();

            int j = 0;

            j++;
        }
Пример #13
0
 private void SolvePuzzle(ScreenAnalyzer analyzer)
 {
     try
     {
         var screen   = analyzer.Analyze();
         var solver   = new PuzzleSolver(screen.GetPuzzle());
         var solution = solver.Solve();
         new SolutionRenderer(solution, screen).Render();
     }
     catch (AbortException)
     {
         sm_log.Info("Solution aborted");
     }
     catch (AnalysisException e)
     {
         HandleError("A problem occurred while analyzing the screen. Please ensure a puzzle is open." +
                     Environment.NewLine + Environment.NewLine + "Error message: " + e.Message, e);
     }
     catch (SolverException e)
     {
         HandleError("Unable to solve this puzzle: " + e.Message, e);
     }
     catch (RenderException e)
     {
         HandleError("A problem occurred while rendering the solution: " + e.Message, e);
     }
     catch (Exception e)
     {
         HandleError("Internal error: " + e.ToString(), e);
     }
 }
Пример #14
0
        public void ExampleInput()
        {
            PuzzleSolver ps     = new PuzzleSolver();
            int          result = ps.Solve("input0.txt", 100);

            Assert.AreEqual(1940, result);
        }
Пример #15
0
        public void TestMethod1()
        {
            string input    = "1122";
            int    solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 3);
        }
Пример #16
0
        public void TestMethod7()
        {
            int input    = 43;
            int solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 6);
        }
Пример #17
0
        public void TestMethod6()
        {
            int input    = 36;
            int solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 5);
        }
Пример #18
0
        public void TestMethod5()
        {
            string input    = @"oiii ioii iioi iiio";
            int    solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 0);
        }
Пример #19
0
        public void TestMethod2()
        {
            string input    = "abcde xyz ecdab";
            int    solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 0);
        }
Пример #20
0
        public void TestMethod3()
        {
            string input    = "a ab abc abd abf abj";
            int    solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 1);
        }
Пример #21
0
        public void Solve_WithMegaPuzzle_Solves()
        {
            var puzzle = new Puzzle(new int?[, ] {
                { null, null, null, null, 10, 1, null, 8, null, 15, 3, 11, null, 2, 16, null },
                { 14, null, 2, null, null, 4, 3, null, null, 13, 8, null, null, 12, null, null },
                { null, null, null, 12, null, null, null, 15, null, null, null, 7, null, null, 9, 10 },
                { 1, 10, 15, null, 6, null, null, null, null, 14, null, null, null, null, null, 11 },
                { null, 11, 14, 6, null, null, null, 9, 13, 8, null, null, null, null, 2, 3 },
                { 12, null, null, null, 4, null, 7, 3, 11, 6, null, null, 16, null, 5, null },
                { 13, 16, null, 2, null, null, null, 1, null, null, 5, null, 10, 9, null, null },
                { null, 4, null, null, 13, null, 2, null, null, null, 16, 3, 11, null, null, null },
                { null, null, null, 10, 3, 6, null, null, null, 9, null, 12, null, null, 4, null },
                { null, null, 12, 15, null, 9, null, null, 7, null, null, null, 1, null, 3, 14 },
                { null, 1, null, 4, null, null, 5, 12, 3, 10, null, 8, null, null, null, 2 },
                { 3, 6, null, null, null, null, 15, 10, 4, null, null, null, 12, 5, 7, null },
                { 2, null, null, null, null, null, 4, null, null, null, null, 15, null, 16, 11, 9 },
                { 4, 14, null, null, 16, null, null, null, 2, null, null, null, 6, null, null, null },
                { null, null, 16, null, null, 7, 8, null, null, 4, 10, null, null, 14, null, 5 },
                { null, 3, 6, null, 9, 12, 14, null, 8, null, 13, 16, null, null, null, null }
            });
            var solver = new PuzzleSolver <Puzzle>(
                new List <IConstraint> {
                new RowUniquenessConstraint(), new ColumnUniquenessConstraint(), new BoxUniquenessConstraint(), new DiagonalUniquenessConstraint()
            });

            solver.Solve(puzzle);

            _AssertPuzzleSolved(puzzle);
        }
Пример #22
0
        public void TestMethod1()
        {
            string input    = "abcde fghij";
            int    solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 1);
        }
Пример #23
0
        public void Solve_WithMegaPuzzle_Solves()
        {
            var puzzle = new Puzzle(new int?[][] {
                new int?[] { null, null, null, null, 10, 1, null, 8, null, 15, 3, 11, null, 2, 16, null },
                new int?[] { 14, null, 2, null, null, 4, 3, null, null, 13, 8, null, null, 12, null, null },
                new int?[] { null, null, null, 12, null, null, null, 15, null, null, null, 7, null, null, 9, 10 },
                new int?[] { 1, 10, 15, null, 6, null, null, null, null, 14, null, null, null, null, null, 11 },
                new int?[] { null, 11, 14, 6, null, null, null, 9, 13, 8, null, null, null, null, 2, 3 },
                new int?[] { 12, null, null, null, 4, null, 7, 3, 11, 6, null, null, 16, null, 5, null },
                new int?[] { 13, 16, null, 2, null, null, null, 1, null, null, 5, null, 10, 9, null, null },
                new int?[] { null, 4, null, null, 13, null, 2, null, null, null, 16, 3, 11, null, null, null },
                new int?[] { null, null, null, 10, 3, 6, null, null, null, 9, null, 12, null, null, 4, null },
                new int?[] { null, null, 12, 15, null, 9, null, null, 7, null, null, null, 1, null, 3, 14 },
                new int?[] { null, 1, null, 4, null, null, 5, 12, 3, 10, null, 8, null, null, null, 2 },
                new int?[] { 3, 6, null, null, null, null, 15, 10, 4, null, null, null, 12, 5, 7, null },
                new int?[] { 2, null, null, null, null, null, 4, null, null, null, null, 15, null, 16, 11, 9 },
                new int?[] { 4, 14, null, null, 16, null, null, null, 2, null, null, null, 6, null, null, null },
                new int?[] { null, null, 16, null, null, 7, 8, null, null, 4, 10, null, null, 14, null, 5 },
                new int?[] { null, 3, 6, null, 9, 12, 14, null, 8, null, 13, 16, null, null, null, null }
            });
            var solver = new PuzzleSolver <Puzzle>(
                new IConstraint[] { new RowUniquenessConstraint(), new ColumnUniquenessConstraint(), new BoxUniquenessConstraint(), new DiagonalUniquenessConstraint() });

            var solved = solver.Solve(puzzle);

            PuzzleTestUtils.AssertStandardPuzzleSolved(solved);
        }
Пример #24
0
    public void PuzzleSolverTest3_TwoStamps()
    {
        Box box = new Box();

        box.RotateYLeft();
        box.RotateZLeft();
        box.Stamp();
        box.RotateYLeft();
        box.RotateYLeft();
        box.Stamp();

        PuzzleSolver       solver   = new PuzzleSolver();
        List <Box.Command> commands = solver.Solve(box);

        List <Box.Command> expectedCommands = new List <Box.Command> {
            Box.Command.Left90Y,
            Box.Command.Left90Z,
            Box.Command.Stamp,
            Box.Command.Left90Y,
            Box.Command.Left90Y,
            Box.Command.Stamp
        };

        Assert.True(ListsAreEqual(expectedCommands, commands));
    }
Пример #25
0
        public void SolvesCorrectly_WithDiagonals()
        {
            var puzzle = new Puzzle(new int?[][] {
                new int?[] { null, null, null, null, null, null, null, 1, 2 },
                new int?[] { null, 5, null, null, 9, null, null, null, null },
                new int?[] { null, null, null, null, null, 8, null, null, null },
                new int?[] { null, 3, null, null, null, null, 5, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, 9, null },
                new int?[] { null, null, null, null, 4, 3, 9, null, null },
                new int?[] { 6, null, 1, null, null, null, 7, null, null },
                new int?[] { 2, null, null, null, null, null, null, null, null },
            });
            var boxesToConstrain = new Box[] {
                new Box(new Coordinate(3, 3), 3),
            };
            var solver = new PuzzleSolver <Puzzle>(
                new IConstraint[] {
                new RowUniquenessConstraint(),
                new ColumnUniquenessConstraint(),
                new BoxUniquenessConstraint(),
                new MagicSquaresConstraint(
                    _CreateStandardPossibleValues(9),
                    boxesToConstrain, includeDiagonals: true)
            });

            var solution = solver.Solve(puzzle);

            PuzzleTestUtils.AssertStandardPuzzleSolved(solution);
            MagicSquareTests.AssertMagicSquaresSatisfied(solution, boxesToConstrain, 15, verifyDiagonals: true);
        }
Пример #26
0
        public void Solve_WithManySolutions_CanSolve()
        {
            var puzzle = new Puzzle(new int?[][] {
                new int?[] { null, null, 9, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, 3, 5, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
                new int?[] { null, null, null, null, null, null, null, null, null },
            });
            var boxesToConstrain = new Box[] {
                new Box(new Coordinate(0, 0), 3),
                new Box(new Coordinate(3, 3), 3),
                new Box(new Coordinate(6, 6), 3),
            };
            var constraint = new MagicSquaresConstraint(
                _CreateStandardPossibleValues(9),
                boxesToConstrain, includeDiagonals: false);
            var solver = new PuzzleSolver <Puzzle>(
                new IConstraint[] {
                new RowUniquenessConstraint(),
                constraint,
            });

            var solution = solver.Solve(puzzle);

            Assert.Equal(0, solution.NumEmptySquares);
            MagicSquareTests.AssertMagicSquaresSatisfied(
                solution, boxesToConstrain, expectedSum: 15, verifyDiagonals: false);
        }
Пример #27
0
        public void TestMethod3()
        {
            string input    = "aa bb cc dd aaa";
            int    solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 1);
        }
Пример #28
0
        public void TestMethod2()
        {
            string input    = @"1236	741
                             2134	106"    ;
            int    solution = PuzzleSolver.Solve(input);

            Assert.AreEqual(solution, 2523);
        }
Пример #29
0
        public void Solve_InvalidPuzzleWithRandomGuesses_Throws(int?[][] matrix)
        {
            var puzzle = new Puzzle(matrix);
            var solver = new PuzzleSolver <Puzzle>(
                new IConstraint[] { new RowUniquenessConstraint(), new ColumnUniquenessConstraint(), new BoxUniquenessConstraint() });

            Assert.Throws <ArgumentException>(() => solver.Solve(puzzle, randomizeGuesses: true));
        }
Пример #30
0
        public bool SudokuSpice(PuzzleSampleCollection sampleCollection)
        {
            var puzzle = new Puzzle(sampleCollection.Random().NullableMatrix);
            var solver = new PuzzleSolver(puzzle);

            solver.Solve();
            return(puzzle.NumEmptySquares == 0);
        }