예제 #1
0
        public void SolveComplete()
        {
            var totalFailures  = 0;
            var totalSteps     = 0;
            var methodSteps    = 0;
            var validatorSteps = 0;
            var timer          = new Stopwatch();

            timer.Start();
            Console.WriteLine(CurrentBoard.SerialiseBoardToPuzzleString());
            Helpers.WriteBreak();
            while (!CurrentBoard.PuzzleSolved)
            {
                var solvableCells = CurrentBoard.BoardCells.Where(x => !x.SolvedCell).ToList();
                foreach (var cell in solvableCells)
                {
                    var validatorSuccess = false;
                    var methodSuccess    = false;
                    foreach (var validator in BoardValidators.OrderBy(x => x.ValidatorDifficulty))
                    {
                        validatorSuccess = validator.ValidatePotentialCellValues(cell, CurrentBoard, out _);
                        if (!validatorSuccess)
                        {
                            totalFailures++;
                        }

                        validatorSteps++;
                        totalSteps++;
                    }

                    if (!validatorSuccess)
                    {
                        foreach (var method in SolverMethods.OrderBy(x => x.MethodDifficulty))
                        {
                            methodSuccess = method.ApplyMethod(cell, CurrentBoard, out _);
                            if (!methodSuccess)
                            {
                                totalFailures++;
                            }
                            methodSteps++;
                            totalSteps++;
                        }
                    }
                }

                CurrentBoard.PuzzleSolved = !solvableCells.Any(x => !x.SolvedCell);
            }

            timer.Stop();
            CurrentBoard.PrintBoard();
            Helpers.WriteBreak();
            Console.WriteLine($"Completed Puzzle String");
            Console.WriteLine(CurrentBoard.SerialiseBoardToPuzzleString());
            Helpers.WriteBreak();
            Console.WriteLine($"Puzzle Solved in {timer.Elapsed}\nTotal Steps Taken (Validators & Solve Methods) {totalSteps}" +
                              $"\nTotal Validator Steps Taken {validatorSteps}" +
                              $"\nTotal Method Steps Taken {methodSteps}" +
                              $"\nTotal Actions Taken (Validators & Solve Methods w/ Legal Move Available) {totalSteps-totalFailures}");
        }
예제 #2
0
        public void Solve(PseudoBoard board)
        {
            var currentFailures = 0;
            var totalFailures   = 0;
            var totalSteps      = 0;
            var methodSteps     = 0;
            var validatorSteps  = 0;
            var timer           = new Stopwatch();

            timer.Start();
            while (!board.PuzzleSolved)
            {
                var solvableCells = board.BoardCells.Where(x => !x.SolvedCell).ToList();
                foreach (var cell in solvableCells)
                {
                    var validatorSuccess = false;
                    var methodSuccess    = false;
                    foreach (var validator in BoardValidators.OrderBy(x => x.ValidatorDifficulty))
                    {
                        validatorSuccess = validator.ValidatePotentialCellValues(cell, board);
                        if (!validatorSuccess)
                        {
                            currentFailures++;
                            totalFailures++;
                        }
                        else
                        {
                            currentFailures = 0;
                        }

                        validatorSteps++;
                        totalSteps++;
                    }

                    if (!validatorSuccess)
                    {
                        foreach (var method in SolverMethods.OrderBy(x => x.MethodDifficulty))
                        {
                            methodSuccess = method.ApplyMethod(cell, board);
                            if (!methodSuccess)
                            {
                                currentFailures++;
                                totalFailures++;
                            }
                            else
                            {
                                currentFailures = 0;
                            }
                            methodSteps++;
                            totalSteps++;
                        }
                    }
                }

                board.PuzzleSolved = solvableCells.All(x => x.SolvedCell);
            }

            timer.Stop();
            board.PrintBoard();
            Console.WriteLine();
            Console.WriteLine($"Puzzle Solved in {timer.Elapsed}\nTotal Steps Taken (Validators & Solve Methods) {totalSteps}" +
                              $"\nTotal Validator Steps Taken {validatorSteps}" +
                              $"\nTotal Method Steps Taken {methodSteps}" +
                              $"\nTotal Actions Taken (Validators & Solve Methods w/ Legal Move Available) {totalSteps-totalFailures}");
        }