public void AddDiscardedValues(SudokuCell keyCell, List <int> discardedValues)
 {
     if (discardedCellValues != null && discardedCellValues.ContainsKey(keyCell))
     {
         if (discardedValues.Contains(0))
         {
             discardedValues.RemoveAll(dv => dv == 0);
         }
         if (keyCell.isFilled && discardedValues.Contains(keyCell.FillValue))
         {
             discardedValues.RemoveAll(fv => fv == keyCell.FillValue);
         }
         if (discardedValues != null && discardedValues.Count > 0)
         {
             discardedCellValues[keyCell] = discardedCellValues[keyCell].Union(discardedValues).ToList();
         }
         if (keyCell.NeighborFills != null && discardedValues.Any(dv => keyCell.NeighborFills.Contains(dv)))
         {
             discardedValues = discardedValues.Except(keyCell.NeighborFills).ToList();
             if (discardedValues == null || discardedValues.Count == 0)
             {
                 return;
             }
             else
             {
                 discardedCellValues[keyCell] = discardedCellValues[keyCell].Union(discardedValues).Distinct().ToList();
             }
         }
     }
     else if (discardedCellValues != null && !discardedCellValues.ContainsKey(keyCell))
     {
         AddKey(keyCell, discardedValues);
     }
 }
 public void AddKey(SudokuCell keyCell, List <int> value = null)
 {
     if (discardedCellValues.Keys.Contains(keyCell))
     {
         Console.WriteLine("Key already exists in discarded values");
     }
     else
     {
         if (value == null)
         {
             discardedCellValues[keyCell] = new List <int>();
         }
         else
         {
             if (value.Contains(0))
             {
                 value.RemoveAll(ak => ak == 0);
             }
             if (keyCell.isFilled && value.Contains(keyCell.FillValue))
             {
                 value.RemoveAll(af => af == keyCell.FillValue);
             }
             if (keyCell.isFilled && keyCell.NeighborFills != null && value.Any(v => keyCell.NeighborFills.Contains(v)))
             {
                 value = value.Except(keyCell.NeighborFills).ToList();
                 if (value == null || value.Count == 0)
                 {
                     discardedCellValues[keyCell] = new List <int>();
                     return;
                 }
             }
             discardedCellValues[keyCell] = value;
         }
     }
 }
 public void AddDiscardedValue(SudokuCell keyCell, int discardedValue)
 {
     if (!discardedCellValues.ContainsKey(keyCell))
     {
         AddKey(keyCell, new List <int>()
         {
             discardedValue
         });
     }
     if (discardedCellValues[keyCell] != null && !discardedCellValues[keyCell].Contains(discardedValue))
     {
         if (!keyCell.isFilled || (keyCell.isFilled && discardedValue != keyCell.FillValue))
         {
             discardedCellValues[keyCell].Add(discardedValue);
             Console.WriteLine("{0} added to {1}'s list of discarded values", discardedValue, keyCell);
             Debug.WriteLine("{0} added to {1}'s list of discarded values", discardedValue, keyCell);
         }
         if (!keyCell.isFilled && keyCell.NeighborFills != null && keyCell.NeighborFills.Contains(discardedValue))
         {
             Console.WriteLine("Cannot add a neighboring value to discarded values table.");
             Debug.WriteLine("Cannot add a neighboring value to discarded values table.");
         }
         if (keyCell.isFilled && keyCell.FillValue == discardedValue)
         {
             Console.WriteLine("Cannot discard the fill value of a cell.");
             Debug.WriteLine("Cannot discard the fill value of a cell.");
         }
     }
     else
     {
         Console.WriteLine("The cell already has {0} inside its list of discarded values.", discardedValue);
         Debug.WriteLine("The cell already has {0} inside its list of discarded values.", discardedValue);
     }
 }
 public List <TwinNode> GetTwinNodesWithKey(SudokuCell targetCell)
 {
     if (!ConfirmTwinKey(targetCell))
     {
         return(null);
     }
     return(_twinNodes.Select(tnc => tnc).Where(tidx => tidx.keyA == targetCell || tidx.keyB == targetCell).ToList());
 }
Esempio n. 5
0
 //function to input a cell and return list not containing said cell (if cell is inside)
 public List <SudokuCell> GetNeighboringCells(SudokuCell inputCell)
 {
     if (_members.Contains(inputCell))
     {
         return(_members.Where(z => z != inputCell).ToList());
     }
     return(null);
 }
Esempio n. 6
0
        public static int getCellDistance(SudokuCell c1, SudokuCell c2)
        {
            int dx = (c2.rowNumber - c1.rowNumber);
            int dy = (c2.colNumber - c2.colNumber);

            double totalDistance = Math.Sqrt((dx ^ 2) + (dy ^ 2));

            return((int)totalDistance);
        }
 public List <int> this[SudokuCell key] {
     get { return(GetPairByKeyType(key).Value); } set { if (value == null)
                                                        {
                                                            throw new InvalidOperationException();
                                                        }
                                                        else
                                                        {
                                                            AddDiscardedValues(key, value);
                                                        } }
 }
 public bool Remove(SudokuCell key)
 {
     if (ContainsKey(key))
     {
         return(discardedCellValues.Remove(GetPairByKeyType(key).Key));
     }
     else
     {
         return(false);
     }
 }
Esempio n. 9
0
 public void RemoveMember(SudokuCell target)
 {
     if (!_members.Contains(target))
     {
         Console.WriteLine("The desired cell is not in this group.");
     }
     else
     {
         _members.Remove(target);
     }
 }
 public bool Remove(SudokuCell key, int value)
 {
     if (ContainsKey(key))
     {
         if (this[key].Contains(value))
         {
             return(this[key].Remove(value));
         }
     }
     return(false);
 }
 public int RemoveValue(SudokuCell key, int value)
 {
     if (discardedCellValues != null && discardedCellValues.ContainsKey(key))
     {
         if (discardedCellValues[key].Contains(value))
         {
             discardedCellValues[key].Remove(value);
             return(value);
         }
     }
     return(0);
 }
 public bool TryGetValue(SudokuCell key, out List <int> value)
 {
     if (ContainsKey(key))
     {
         value = GetPairByKeyType(key).Value;
         return(true);
     }
     else
     {
         value = default(List <int>);
         return(false);
     }
 }
        private KeyValuePair <SudokuCell, List <int> > GetPairByKeyType(SudokuCell key)
        {
            KeyValuePair <SudokuCell, List <int> > pair = discardedCellValues.Where(u => u.GetType() == key.GetType()).FirstOrDefault();

            if (pair.Key == null)
            {
                throw new KeyNotFoundException();
            }
            else
            {
                return(pair);
            }
        }
        public bool ContainsKey(SudokuCell key)
        {
            KeyValuePair <SudokuCell, List <int> > pair = discardedCellValues.Where(u => u.Key.GetType() == key.GetType()).FirstOrDefault();

            if (pair.Key == null)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
 public List <int> RemoveValues(SudokuCell keyCell, List <int> values)
 {
     if (discardedCellValues != null && discardedCellValues.ContainsKey(keyCell))
     {
         foreach (int value in values)
         {
             if (discardedCellValues[keyCell].Contains(value))
             {
                 discardedCellValues[keyCell].Remove(value);
             }
         }
         return(values);
     }
     return(null);
 }
Esempio n. 16
0
        public void SetupBoard(string clueFile)
        {
            InitializeCellGroups();
            List <CellData> clueList = ClueSerializationUtility.DeserailizeClues(clueFile, out bool fileExists);

            if (clueList != null && clueList.Count > 0)
            {
                puzzleId = clueFile;
                for (int i = 0; i < 81; i++)
                {
                    int      rn       = i / 9;
                    int      cn       = i % 9;
                    CellData cellular = clueList.FirstOrDefault(a => a.rowNum == rn + 1 && a.columnNum == cn + 1);
                    if (cellular != null)
                    {
                        SudokuCell cellulos = new SudokuCell(cellular);
                        cellList.Add(cellulos);
                        rowList[rn].AddMember(cellulos);
                        columnList[cn].AddMember(cellulos);
                        subgridList[cellulos.sgNumber - 1].AddMember(cellulos);
                        cellulos.rowPointer     = rowList[rn];
                        cellulos.columnPointer  = columnList[cn];
                        cellulos.subgridPointer = subgridList[cellulos.sgNumber - 1];
                        fillFrequencyCounter.IncrementFor(cellulos.FillValue);
                        Debug.WriteLine(cellulos);
                    }
                    else
                    {
                        SudokuCell cellulos = new SudokuCell();
                        cellList.Add(cellulos);
                        cellulos.rowNumber = rn + 1;
                        cellulos.colNumber = cn + 1;
                        SudokuCellExtensions.AssignSubgridNumber(cellulos);
                        rowList[rn].AddMember(cellulos);
                        columnList[cn].AddMember(cellulos);
                        subgridList[cellulos.sgNumber - 1].AddMember(cellulos);
                        cellulos.rowPointer     = rowList[rn];
                        cellulos.columnPointer  = columnList[cn];
                        cellulos.subgridPointer = subgridList[cellulos.sgNumber - 1];
                    }
                }
            }
        }
Esempio n. 17
0
 public static void UnfillCell(this SudokuCell sudokuCell)
 {
     if (sudokuCell == null)
     {
         new NullReferenceException();
     }
     else
     {
         if (sudokuCell.isFilled)
         {
             int oldValue = sudokuCell.FillValue;
             sudokuCell.FillValue = 0;
             SudokuPuzzle.GetPuzzle().cellBoard.fillFrequencyCounter.DecrementFor(oldValue);
         }
         else
         {
             Console.WriteLine("{0} is already unfilled.", sudokuCell.ToString());
         }
     }
 }
Esempio n. 18
0
 public static void FillCell(this SudokuCell sudokuCell, int newFill)
 {
     if (sudokuCell == null)
     {
         new NullReferenceException();
     }
     else
     {
         if (sudokuCell.Possibilities.Contains(newFill))
         {
             sudokuCell.FillValue = newFill;
             Console.WriteLine("{0} now has fill value of {1}", sudokuCell.ToString(), newFill.ToString());
             Debug.WriteLine("{0} now has fill value of {1}", sudokuCell.ToString(), newFill.ToString());
             SudokuPuzzle.GetPuzzle().cellBoard.fillFrequencyCounter.IncrementFor(newFill);
         }
         else
         {
             Console.WriteLine("{0} not part of {1}'s possibilities", newFill.ToString(), sudokuCell.ToString());
         }
     }
 }
Esempio n. 19
0
        //function to return cell(s) that all belong to one different subgroup
        public List <SudokuCell> ReturnSharedNeighbors(SudokuCell sc)
        {
            if (!_members.Contains(sc))
            {
                return(null);
            }
            List <SudokuCell> sharedNeighbors = new List <SudokuCell>();

            foreach (SudokuCell icell in _members)
            {
                switch (_groupType)
                {
                case UnitType.row:
                    if (icell.colNumber == sc.colNumber || icell.sgNumber == sc.sgNumber)
                    {
                        sharedNeighbors.Add(icell);
                    }
                    break;

                case UnitType.column:
                    if (icell.rowNumber == sc.rowNumber || icell.sgNumber == sc.sgNumber)
                    {
                        sharedNeighbors.Add(icell);
                    }
                    break;

                case UnitType.subgrid:
                    if (icell.rowNumber == sc.rowNumber || icell.colNumber == sc.colNumber)
                    {
                        sharedNeighbors.Add(icell);
                    }
                    break;

                default:
                    break;
                }
            }
            return(sharedNeighbors);
        }
Esempio n. 20
0
 public CellAttempts(SudokuCell s_cell, List <int> attemptedVals) //constructor with multiple attempted values
 {
     keyCell         = s_cell;
     attemptedValues = attemptedVals;
 }
Esempio n. 21
0
        public SudokuCell getFewestPossibilitiesNeighbor()
        {
            SudokuCell fewestCell = neighbors.OrderBy(y => y.Possibilities.Count()).FirstOrDefault();

            return(fewestCell);
        }
Esempio n. 22
0
        public static void AssignSubgridNumber(SudokuCell inputCell)
        {
            bool canBeFilled = !inputCell.isClue || inputCell.rowNumber == 0 || inputCell.colNumber == 0;

            if (canBeFilled)
            {
                if (inputCell.rowNumber <= 3)
                {
                    if (inputCell.colNumber <= 3)
                    {
                        inputCell.sgNumber = 1;
                    }
                    else if (inputCell.colNumber <= 6)
                    {
                        inputCell.sgNumber = 2;
                    }
                    else
                    {
                        inputCell.sgNumber = 3;
                    }
                }
                else if (inputCell.rowNumber <= 6)
                {
                    if (inputCell.colNumber <= 3)
                    {
                        inputCell.sgNumber = 4;
                    }
                    else if (inputCell.colNumber <= 6)
                    {
                        inputCell.sgNumber = 5;
                    }
                    else
                    {
                        inputCell.sgNumber = 6;
                    }
                }
                else
                {
                    if (inputCell.colNumber <= 3)
                    {
                        inputCell.sgNumber = 7;
                    }
                    else if (inputCell.colNumber >= 4 && inputCell.colNumber <= 6)
                    {
                        inputCell.sgNumber = 8;
                    }
                    else
                    {
                        inputCell.sgNumber = 9;
                    }
                }
            }
            else
            {
                if (inputCell.isClue)
                {
                    Console.WriteLine("Cannot change the subgrid number of a clue");
                }
                else
                {
                    Console.WriteLine("Assign the row and column numbers of the cell a value ranging from 1-9");
                }
            }
        }
 public bool ConfirmTwinKey(SudokuCell inquiry)
 {
     return(_keyNodes.Contains(inquiry));
 }
Esempio n. 24
0
 public void AddMember(SudokuCell newMember)
 {
     _members.Add(newMember);
 }
Esempio n. 25
0
 public TwinNode(SudokuCell a, SudokuCell b, HashSet <int> sharedValues, TwinEnum tEnum)
 {
     twinCells = new Pair <SudokuCell, SudokuCell>(a, b);
     possSet   = sharedValues;
     twinEnum  = tEnum;
 }
 public List <int> GetCellDiscardedValues(SudokuCell keyCell)
 {
     return(discardedCellValues[keyCell]);
 }
 public void ClearKey(SudokuCell key)
 {
     discardedCellValues[key].Clear();
 }
 public void Add(SudokuCell key, List <int> value)
 {
     (this as ICollection <KeyValuePair <SudokuCell, List <int> > >).Add(new KeyValuePair <SudokuCell, List <int> >(key, value));
 }
Esempio n. 29
0
 public CellAttempts(SudokuCell s_cell, int singularVal) //constructor with only a single attempted value
 {
     keyCell         = s_cell;
     attemptedValues = new List <int>();
     attemptedValues.Add(singularVal);
 }