Beispiel #1
0
        /// <summary>
        /// If two cells in one segment contains the same two candidates only,
        /// then these candidates in other cells can be removed.
        /// </summary>
        private void NakedPair(List <Cell> changes)
        {
            using (var cacheItem = _cache.Get <Candidates, int>())
            {
                var dic = cacheItem.Dictionary;
                foreach (var seg in Segments)
                {
                    if (seg.FreeCells < 3)
                    {
                        continue;
                    }

                    dic.Clear();
                    foreach (var cell in seg)
                    {
                        if (cell.HasValue)
                        {
                            continue;
                        }
                        if (dic.ContainsKey(cell.Candidates))
                        {
                            dic[cell.Candidates]++;
                        }
                        else
                        {
                            dic[cell.Candidates] = 1;
                        }
                    }

                    foreach (var pair in dic)
                    {
                        if (pair.Value < 2 || pair.Value >= seg.FreeCells)
                        {
                            continue;
                        }
                        if (CandidatesHelper.Count(pair.Key) != pair.Value)
                        {
                            continue;
                        }
                        foreach (var cell in seg)
                        {
                            if (cell.HasValue || cell.Candidates == pair.Key)
                            {
                                continue;
                            }
                            var old = cell.Candidates;
                            cell.RemoveCandidates(pair.Key);
                            if (cell.Candidates != old)
                            {
                                changes.Add(cell);
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
 public void RemoveCandidates(Candidates candidates)
 {
     if (HasValue)
     {
         return;
     }
     Candidates   &= ~candidates;
     Possibilities = CandidatesHelper.Count(Candidates);
     OnChanged();
 }
Beispiel #3
0
 public void SetCandidates(Candidates candidates)
 {
     if (HasValue)
     {
         throw new InvalidOperationException("Cell has already a value.");
     }
     if ((Candidates | candidates) != Candidates)
     {
         throw new InvalidOperationException("Cannot add new candidates.");
     }
     Candidates    = candidates;
     Possibilities = CandidatesHelper.Count(Candidates);
     OnChanged();
 }
Beispiel #4
0
 public void Reset()
 {
     if (HasValue)
     {
         foreach (var seg in Segments.Keys)
         {
             seg.FreeCells++;
             seg.Values &= ~CandidatesHelper.ToCandidate(Value);
         }
     }
     _value        = Cell.EmptyValue;
     IsFixed       = false;
     Candidates    = Cell.AllCandidates;
     Possibilities = CandidatesHelper.Count(Candidates);
     OnChanged();
 }