Example #1
0
        public bool ValidatePotentialCellValues(PseudoCell cell, PseudoBoard board, out string solveMessage)
        {
            solveMessage = "";
            var existingValues = board.BoardCells.Where(x => x.CellRow == cell.CellRow &&
                                                        x.SolvedCell &&
                                                        cell.PossibleValues.Contains(x.CurrentValue)).ToList();
            var startCount = cell.PossibleValues.Count;

            foreach (var eCell in existingValues)
            {
                solveMessage = $"{solveMessage}\nRemoved {eCell.CurrentValue} from R{cell.CellRow} C{cell.CellColumn} for conflict with R{eCell.CellRow} C{eCell.CellColumn} : Row Constraint";
                cell.PossibleValues.Remove(eCell.CurrentValue);
            }


            if (cell.PossibleValues.Count == 1)
            {
                cell.CurrentValue   = cell.PossibleValues.First(); //only 1 value remains.
                solveMessage        = $"{solveMessage}\nSolved for {cell.CurrentValue} in R{cell.CellRow} C{cell.CellColumn} : Naked Single Row Constraint";
                cell.PossibleValues = new List <int>();
                cell.SolvedCell     = true;
            }

            return(cell.PossibleValues.Count != startCount);
        }
Example #2
0
        public bool ApplyMethod(PseudoCell cell, PseudoBoard board)
        {
            var startCount = cell.PossibleValues.Count;

            foreach (var value in cell.PossibleValues)
            {
                var sharedBoxCount = 0;
                var sharedBoxCells = board.BoardCells.Where(x => x.CellBox == cell.CellBox && cell.PossibleValues.Contains(value)).ToList();
                sharedBoxCount = sharedBoxCells.Count;
                if (sharedBoxCells.Any() && sharedBoxCount <= 3)
                {
                    if (sharedBoxCells.Where(x => x.CellRow == cell.CellRow).Count() == sharedBoxCount)
                    {
                        foreach (var boardCell in board.BoardCells.Where(x => x.CellRow == cell.CellRow && x.CellBox != cell.CellBox))
                        {
                            boardCell.PossibleValues.Remove(value);
                        }
                    }

                    if (sharedBoxCells.Where(x => x.CellColumn == cell.CellColumn).Count() == sharedBoxCount)
                    {
                        foreach (var boardCell in board.BoardCells.Where(x => x.CellColumn == cell.CellColumn && x.CellBox != cell.CellBox))
                        {
                            boardCell.PossibleValues.Remove(value);
                        }
                    }
                }
            }
            return(cell.PossibleValues.Count != startCount);
        }
Example #3
0
        public bool ApplyMethod(PseudoCell cell, PseudoBoard board)
        {
            var startCount = cell.PossibleValues.Count;

            foreach (var value in cell.PossibleValues)
            {
                var rowCells = board.BoardCells.Where(x => x.CellRow == cell.CellRow).Where(x => x.PossibleValues.Contains(value)).ToList();
                var colCells = board.BoardCells.Where(x => x.CellColumn == cell.CellColumn).Where(x => x.PossibleValues.Contains(value)).ToList();
                var boxCells = board.BoardCells.Where(x => x.CellBox == cell.CellBox).Where(x => x.PossibleValues.Contains(value)).ToList();

                if (rowCells.Count == 1 || colCells.Count == 1 || boxCells.Count == 1)
                {
                    cell.CurrentValue   = value;
                    cell.PossibleValues = new List <int>();
                    cell.SolvedCell     = true;
                    return(true);
                }
            }
            return(cell.PossibleValues.Count != startCount);
        }
Example #4
0
        public bool ValidatePotentialCellValues(PseudoCell cell, PseudoBoard board)
        {
            var existingValues = board.BoardCells.Where(x => x.CellRow == cell.CellRow &&
                                                        x.SolvedCell &&
                                                        cell.PossibleValues.Contains(x.CurrentValue)).Select(x => x.CurrentValue).ToList();
            var startCount = cell.PossibleValues.Count;

            foreach (var value in existingValues)
            {
                cell.PossibleValues.Remove(value);
            }

            if (cell.PossibleValues.Count == 1)
            {
                cell.CurrentValue   = cell.PossibleValues.First(); //only 1 value remains.
                cell.PossibleValues = new List <int>();
                cell.SolvedCell     = true;
            }

            return(cell.PossibleValues.Count != startCount);
        }
Example #5
0
        public bool ApplyMethod(PseudoCell cell, PseudoBoard board, out string solveMessage)
        {
            var startCount = cell.PossibleValues.Count;

            solveMessage = "";
            foreach (var value in cell.PossibleValues)
            {
                var rowCells = board.BoardCells.Where(x => x.CellRow == cell.CellRow).Where(x => x.PossibleValues.Contains(value)).Count();
                var colCells = board.BoardCells.Where(x => x.CellColumn == cell.CellColumn).Where(x => x.PossibleValues.Contains(value)).Count();
                var boxCells = board.BoardCells.Where(x => x.CellBox == cell.CellBox).Where(x => x.PossibleValues.Contains(value)).Count();

                if (rowCells == 1 || colCells == 1 || boxCells == 1)
                {
                    cell.CurrentValue   = value;
                    solveMessage        = $"{solveMessage}\nSolved for {cell.CurrentValue} in R{cell.CellRow} C{cell.CellColumn} : Hidden Single";
                    cell.PossibleValues = new List <int>();
                    cell.SolvedCell     = true;
                    return(true);
                }
            }
            return(cell.PossibleValues.Count != startCount);
        }
Example #6
0
        public bool ApplyMethod(PseudoCell cell, PseudoBoard board, out string solveMessage)
        {
            var startCount = cell.PossibleValues.Count;

            solveMessage = "";
            var valueRemoved = false;

            switch (startCount)
            {
            case 2:
                var matchedBox = board.BoardCells
                                 .Where(x => x.CellBox == cell.CellBox && x.CellRow != cell.CellRow && x.CellColumn != cell.CellColumn)
                                 .Where(x => x.PossibleValues.Intersect(cell.PossibleValues).Count() == x.PossibleValues.Count() &&
                                        x.PossibleValues.Intersect(cell.PossibleValues).Count() == cell.PossibleValues.Count()).ToList();
                var matchedRow = board.BoardCells
                                 .Where(x => x.CellRow == cell.CellRow && x.CellColumn != cell.CellColumn)
                                 .Where(x => x.PossibleValues.Intersect(cell.PossibleValues).Count() == x.PossibleValues.Count() &&
                                        x.PossibleValues.Intersect(cell.PossibleValues).Count() == cell.PossibleValues.Count()).ToList();
                var matchedCol = board.BoardCells
                                 .Where(x => x.CellColumn == cell.CellColumn && x.CellRow != cell.CellRow)
                                 .Where(x => x.PossibleValues.Intersect(cell.PossibleValues).Count() == x.PossibleValues.Count() &&
                                        x.PossibleValues.Intersect(cell.PossibleValues).Count() == cell.PossibleValues.Count()).ToList();
                if (matchedBox.Any())
                {
                    var nonPairs = board.BoardCells
                                   .Where(x => x.CellBox == cell.CellBox)
                                   .Where(x => x.CellColumn != cell.CellColumn && x.CellRow != cell.CellRow)
                                   .Where(x => x.PossibleValues.Intersect(cell.PossibleValues).Count() > 0)
                                   .Except(matchedBox).ToList();
                    if (nonPairs.Any())
                    {
                        foreach (var nonPair in nonPairs)
                        {
                            foreach (var value in cell.PossibleValues)
                            {
                                solveMessage = $"{solveMessage}\nRemoved {value} from R{nonPair.CellRow} C{nonPair.CellColumn} due to conflict with R{cell.CellRow} C{cell.CellColumn} | R{matchedBox.First().CellRow} C{matchedBox.First().CellColumn}: Conjugate Pair";
                                nonPair.PossibleValues.Remove(value);
                                valueRemoved = true;
                            }
                        }
                    }
                }
                if (matchedRow.Any())
                {
                    var nonPairs = board.BoardCells
                                   .Where(x => x.CellRow == cell.CellRow)
                                   .Where(x => x.CellColumn != cell.CellColumn && x.CellBox != cell.CellBox)
                                   .Where(x => x.PossibleValues.Intersect(cell.PossibleValues).Count() > 0)
                                   .Except(matchedRow).ToList();
                    if (nonPairs.Any())
                    {
                        foreach (var nonPair in nonPairs)
                        {
                            foreach (var value in cell.PossibleValues)
                            {
                                solveMessage = $"{solveMessage}\nRemoved {value} from R{nonPair.CellRow} C{nonPair.CellColumn} due to conflict with R{cell.CellRow} C{cell.CellColumn} | R{matchedRow.First().CellRow} C{matchedRow.First().CellColumn}: Conjugate Pair";
                                nonPair.PossibleValues.Remove(value);
                                valueRemoved = true;
                            }
                        }
                    }
                }
                if (matchedCol.Any())
                {
                    var nonPairs = board.BoardCells
                                   .Where(x => x.CellColumn == cell.CellColumn)
                                   .Where(x => x.CellRow != cell.CellRow && x.CellBox != cell.CellBox)
                                   .Where(x => x.PossibleValues.Intersect(cell.PossibleValues).Count() > 0)
                                   .Except(matchedCol).ToList();
                    if (nonPairs.Any())
                    {
                        foreach (var nonPair in nonPairs)
                        {
                            foreach (var value in cell.PossibleValues)
                            {
                                solveMessage = $"{solveMessage}\nRemoved {value} from R{nonPair.CellRow} C{nonPair.CellColumn} due to conflict with R{cell.CellRow} C{cell.CellColumn} | R{matchedCol.First().CellRow} C{matchedCol.First().CellColumn}: Conjugate Pair";
                                nonPair.PossibleValues.Remove(value);
                                valueRemoved = true;
                            }
                        }
                    }
                }
                break;

            case 3:
                break;

            case 4:
                break;

            default:
                return(false);
            }

            return(valueRemoved);
        }