Beispiel #1
0
        public PseudoSolver(List <PuzzleConstraint> constraints, PseudoBoard board)
        {
            CurrentStep = new SolverStep
            {
                SolverStepId = 1,
                StepComment  = "Starting Board State",
                BoardState   = new SolverState
                {
                    BoardCells  = board.BoardCells,
                    SolvedState = false
                }
            };
            CurrentBoard = board;

            SolverMethods.Add(new HiddenSingle());
            SolverMethods.Add(new ConjugatePair());


            foreach (var constraint in constraints)
            {
                switch (constraint)
                {
                case PuzzleConstraint.BoxUnique:
                    BoardValidators.Add(new BoxUnique());
                    break;

                case PuzzleConstraint.ColumnUnique:
                    BoardValidators.Add(new ColumnUnique());
                    break;

                case PuzzleConstraint.RowUnique:
                    BoardValidators.Add(new RowUnique());
                    break;

                case PuzzleConstraint.KnightUnique:
                    BoardValidators.Add(new KnightUnique());
                    break;

                case PuzzleConstraint.KingUnique:
                    BoardValidators.Add(new KingUnique());
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
Beispiel #2
0
        public SolveStatus StepSolve()
        {
            var boardStateChanged = false;
            var solvableCells     = CurrentBoard.BoardCells.Where(x => !x.SolvedCell).ToList();
            var noChangeMade      = 0;
            var solveMessage      = "";
            var stepType          = SolverStepType.FailStep;

            while (!boardStateChanged && !CurrentBoard.PuzzleSolved)
            {
                var baseDifficulty   = 1;
                var validatorSuccess = false;

                foreach (var validator in BoardValidators.Where(x => x.ValidatorDifficulty <= baseDifficulty).ToList())
                {
                    if (validatorSuccess)
                    {
                        break;
                    }
                    foreach (var cell in solvableCells)
                    {
                        if (validator.ValidatePotentialCellValues(cell, CurrentBoard, out solveMessage))
                        {
                            validatorSuccess = true;
                            stepType         = SolverStepType.ValidatorStep;
                            break;
                        }
                    }
                }

                if (validatorSuccess)
                {
                    boardStateChanged = true;
                    noChangeMade      = 0;
                    continue;
                }

                var methodSuccess = false;
                foreach (var method in SolverMethods.Where(x => x.MethodDifficulty <= baseDifficulty).ToList())
                {
                    if (methodSuccess)
                    {
                        break;
                    }
                    foreach (var cell in solvableCells)
                    {
                        if (method.ApplyMethod(cell, CurrentBoard, out solveMessage))
                        {
                            methodSuccess = true;
                            stepType      = SolverStepType.MethodStep;
                            break;
                        }
                    }
                }

                if (methodSuccess)
                {
                    boardStateChanged = true;
                    noChangeMade      = 0;
                    continue;
                }

                baseDifficulty++;
                noChangeMade++;

                if (noChangeMade >= 100)
                {
                    Console.WriteLine("No Board State change for 100 iterations. Solve could not complete.");
                    Console.WriteLine($"Total Steps = {CurrentStep.SolverStepId}");
                    return(SolveStatus.NoStateChange);
                }
            }

            SolverSteps.Add(CurrentStep);
            var nextStep = new SolverStep
            {
                SolverStepId = CurrentStep.SolverStepId + 1,
                StepComment  = solveMessage,
                StepType     = stepType,
                BoardState   = new SolverState
                {
                    BoardCells  = CurrentBoard.BoardCells.ToList(),
                    SolvedState = false
                }
            };

            CurrentStep  = nextStep;
            CurrentBoard = new PseudoBoard(CurrentBoard.BoardCells.ToList());
            CurrentBoard.PrintBoard();

            if (!string.IsNullOrEmpty(solveMessage))
            {
                Console.WriteLine(solveMessage);
            }
            Console.WriteLine($"Total Steps = {CurrentStep.SolverStepId}");

            if (CurrentBoard.PuzzleSolved)
            {
                return(SolveStatus.BoardSolved);
            }

            return(SolveStatus.StateChange);
        }