Ejemplo n.º 1
0
            public SolveResult Solve(IGridPartialSolution gridPartialPartialSolution)
            {
                if (gridPartialPartialSolution.IsSolved)
                {
                    return new SolveResult(true, gridPartialPartialSolution);
                }

                // Solves as many squares as possible without trying to guess values
                var result = this.possibilitiesSquareGrid.SolveSquares(gridPartialPartialSolution);

                if (result.NoValidSolution)
                {
                    return new SolveResult(false, null);
                }

                if (result.PartialSolution.IsSolved)
                {
                    return new SolveResult(true, result.PartialSolution);
                }

                var solution = result.PartialSolution.Copy();
                var squareToGuess = solution.GetNextUnsolvedSquare();

                solution.SetSquareBlack(squareToGuess);
                var blackGuessResult = this.Solve(solution);

                if (blackGuessResult.Solved)
                {
                    return blackGuessResult;
                }

                solution = result.PartialSolution.Copy();
                solution.SetSquareWhite(squareToGuess);
                return this.Solve(solution);
            }
        public SolveSquaresResult SolveSquares(IGridPartialSolution gridPartialSolution)
        {
            bool anySquaresSolved;
            IGridPartialSolution partialSolution;
            do
            {
                int rowId = 0;
                anySquaresSolved = false;
                var solvedRows = new List<IPartiallyCompleteGroup>();

                foreach (var row in this.rows)
                {
                    var result = row.SolveSquares(gridPartialSolution.GetRow(rowId));
                    anySquaresSolved = anySquaresSolved || result.AnyNewlySolvedSquares;

                    if (!result.StillValid)
                    {
                        return new SolveSquaresResult(gridPartialSolution, false);
                    }

                    solvedRows.Add(result.Solved);
                    
                    rowId++;
                }

                int columnId = 0;
                var solvedColumns = new List<IPartiallyCompleteGroup>();

                foreach (var column in this.columns)
                {
                    var result = column.SolveSquares(gridPartialSolution.GetColumn(columnId));
                    anySquaresSolved = anySquaresSolved || result.AnyNewlySolvedSquares;

                    if (!result.StillValid)
                    {
                        return new SolveSquaresResult(gridPartialSolution, true);
                    }

                    solvedColumns.Add(result.Solved);

                    columnId++;
                }
                
                partialSolution = new GridPartialSolution(solvedRows, solvedColumns);

                if (partialSolution.IsSolved)
                {
                    return new SolveSquaresResult(partialSolution, false);
                }

                gridPartialSolution = partialSolution.Copy();
            }
            while (anySquaresSolved);
            


            return new SolveSquaresResult(partialSolution, false);
        }
Ejemplo n.º 3
0
        public SolveSquaresResult(IGridPartialSolution partialSolution, bool noValidSolution, string name)
        {
            if (!noValidSolution && partialSolution == null)
            {
                throw new ArgumentException("If noValidSolution is false, then solution must not be null.");
            }

            this.NoValidSolution = noValidSolution;
            this.Name = name;
            this.PartialSolution = partialSolution;
        }
Ejemplo n.º 4
0
 public SolveSquaresResult(IGridPartialSolution partialSolution, bool noValidSolution) : this(partialSolution, noValidSolution, "abc")
 {
     
 }
Ejemplo n.º 5
0
 public SolveResult(bool solved, IGridPartialSolution gridPartialSolution)
 {
     this.GridPartialSolution = gridPartialSolution;
     this.Solved = solved;
 }