Exemplo n.º 1
0
        string DebugCandidateInfo(List <SudokuCell> candidatePair)
        {
            List <int> values       = candidatePair[0].Candidates.ToList();
            int        columnNumber = candidatePair[0].GridPosition.Column;
            int        rowNumber    = candidatePair[0].GridPosition.Row;
            int        blockNumber  = candidatePair[0].GridPosition.Block;

            return(string.Format("Values: {0} Column: {1} Row: {2} Block: {3}",
                                 StaticSudoku.ArrayToString(values, ", "),
                                 columnNumber, rowNumber, blockNumber));
        }
Exemplo n.º 2
0
        public override string ToString()
        {               // Key = # of occurances, Value = List of numbers that occured that many times.
            StringBuilder str = new StringBuilder();

            foreach (var kvp in _dictionary)
            {
                str.AppendFormat("[{0}(key) occurrences of value(s): ({1})]", kvp.Key, StaticSudoku.ArrayToString <V>(kvp.Value, ","));
            }
            return(str.ToString());
        }
Exemplo n.º 3
0
        int ExploreHiddenSubset(List <int> hiddenSubset, List <SudokuCell> block)
        {
            if (hiddenSubset.Count > 0)
            {
                DebugWrite("HiddenSubset: Candidate(s) ({0}) has only {1} entries in block {2}.",
                           StaticSudoku.ArrayToString(hiddenSubset, ", "), hiddenSubset.Count, block[0].Block);

                List <SudokuCell> subsetCells = GetCellsWithSubset_Any(block, hiddenSubset);

                if (subsetCells.Count == 0)
                {
                    return(0);
                }

                if (subsetCells.Count == 1 && hiddenSubset.Count == 1)
                {
                    SudokuCell cell = subsetCells[0];
                    int        val  = hiddenSubset[0];
                    DebugWrite("ELIMINATED NAKED Candidate \"{0}\": Block {1}, column {2}, row {3}.", val, cell.Block, cell.Column, cell.Row);
                    cell.Value = val;

                    return(RemoveCandidatesValues(hiddenSubset, _sudokuGrid.GetCellsInScope(cell).ToList()));
                }
                else if (subsetCells.Count == 2 && hiddenSubset.Count == 2)
                {
                    int  col         = subsetCells[0].GridPosition.Column;
                    int  row         = subsetCells[0].GridPosition.Row;
                    bool columnMatch = InSameColumn(subsetCells);
                    bool rowMatch    = InSameRow(subsetCells);

                    if (columnMatch)
                    {
                        // search columns
                        List <SudokuCell> scope = _sudokuGrid.GetColumnScope(col).Except(subsetCells).ToList();
                        DebugWrite("HIDDEN PAIR: Eliminated candidates ({0}) on column {1}.", StaticSudoku.ArrayToString(hiddenSubset, ","), col);
                        return(RemoveCandidatesValues(hiddenSubset, scope));
                    }
                    else if (rowMatch)
                    {
                        // search rows
                        List <SudokuCell> scope = _sudokuGrid.GetRowScope(row).Except(subsetCells).ToList();
                        DebugWrite("HIDDEN PAIR: Eliminated candidates ({0}) on row {1}.", StaticSudoku.ArrayToString(hiddenSubset, ","), row);
                        return(RemoveCandidatesValues(hiddenSubset, scope));
                    }
                }
                else if (subsetCells.Count == 3 && hiddenSubset.Count == 3)
                {
                    bool columnMatch = InSameColumn(subsetCells);
                    bool rowMatch    = InSameRow(subsetCells);

                    if (columnMatch)
                    {
                        int col = subsetCells[0].GridPosition.Column;
                        DebugWrite("_____HIDDEN TRIPLE: On column {0}", col);
                    }
                    else if (rowMatch)
                    {
                        int row = subsetCells[0].GridPosition.Row;
                        DebugWrite("_____HIDDEN TRIPLE: On row {0}", row);
                    }
                }
            }
            return(0);
        }
Exemplo n.º 4
0
        int FoundNakedMatchingCandidates(List <SudokuCell> matchingGroup, SudokuRegion region, int regionIndex)
        {
            List <int> groupValues = matchingGroup[0].Candidates.ToList();

            DebugWrite("Found: Naked Matching Candidates! GroupSize=\"{0}\", Region=\"{1} {2}\", Candidates=\"{3}\".",
                       matchingGroup.Count, Enum.GetName(typeof(SudokuRegion), region), regionIndex, StaticSudoku.ArrayToString(groupValues, ","));


            SudokuCell cell = matchingGroup[0];

            foreach (SudokuCell seen in matchingGroup)
            {
                visitedGroups.Add(seen.GridPosition.ToString(), seen.FormatCandidatesString_Compact());
            }

            int totalEliminated = 0;

            if (InSameRow(matchingGroup))
            {
                List <SudokuCell> rowScope = _sudokuGrid.GetRowScope(cell.Row).Except(matchingGroup).ToList();
                totalEliminated += RemoveCandidatesValues(groupValues, rowScope);
            }
            else if (InSameColumn(matchingGroup))
            {
                List <SudokuCell> columnScope = _sudokuGrid.GetColumnScope(cell.Column).Except(matchingGroup).ToList();
                totalEliminated += RemoveCandidatesValues(groupValues, columnScope);
            }

            if (InSameBlock(matchingGroup))
            {
                List <SudokuCell> blockScope = _sudokuGrid.GetBlockScope(cell.Block).Except(matchingGroup).ToList();
                totalEliminated += RemoveCandidatesValues(groupValues, blockScope);
            }

            return(totalEliminated);
        }