public (List <SudokuCell>, List <int>, List <int>, List <int>) GrabSubgroup(int targetNumber)
        {
            List <SudokuCell> possibleCells       = XGrab(targetNumber);                               //the list of cells with targetNumber as possibility
            List <int>        retrievedRowIndices = SudokuCellExtensions.DeriveRowNums(possibleCells); //the rows of the cells with targetNumber as possibility
            List <int>        retrievedColIndices = SudokuCellExtensions.DeriveColNums(possibleCells); //the columns of the cells with targetNumber as possibility
            List <int>        retrievedSGIndices  = SudokuCellExtensions.DeriveSGNums(possibleCells);  //the subgrids of the cells with targetNumber as possibility

            return(possibleCells, retrievedRowIndices, retrievedColIndices, retrievedSGIndices);
        }
Exemplo n.º 2
0
        private static SudokuCell[,] LoadDataFromFile(string path)
        {
            using var streamReader = new StreamReader(path);
            var data     = streamReader.ReadToEnd();
            var byteData = Encoding.UTF8.GetBytes(data);

            try
            {
                return(SudokuCellExtensions.ConvertToSudokuCellArray(byteData));
            }
            catch
            {
                return(null);
            }
        }
        public void SubgroupExculsion(int targetNum)
        {
            (List <SudokuCell> candidateCells, List <int> rowInds, List <int> colInds, List <int> sgInds) = GrabSubgroup(targetNum);
            Dictionary <int, List <SudokuCell> > rowPossCs = new Dictionary <int, List <SudokuCell> >();
            Dictionary <int, List <SudokuCell> > colPossCs = new Dictionary <int, List <SudokuCell> >();
            Dictionary <int, List <SudokuCell> > sgPossCs  = new Dictionary <int, List <SudokuCell> >();
            Dictionary <SGUnitIntersectKey, List <SudokuCell> > sgIntersects = new Dictionary <SGUnitIntersectKey, List <SudokuCell> >(); //an empty dictionary to store the SGUnitIntersectKey, List<SudokuCell> pairs

            foreach (int rowInd in rowInds)                                                                                               //for each row in the returned list of row indices
            {
                var rQuery = candidateCells.Select(x => x).Where(y => y.rowNumber == rowInd);                                             //select all cells in candidateCells where the rowNumber matches the current row index
                rowPossCs[rowInd] = rQuery.ToList();                                                                                      //store the list of cells in the appropriate row number in dictionary.
            }
            foreach (int colInd in colInds)
            {
                var cQuery = candidateCells.Select(x => x).Where(y => y.colNumber == colInd);
                colPossCs[colInd] = cQuery.ToList();
            }
            foreach (int sgInd in sgInds)
            {
                var sgQuery = candidateCells.Select(x => x).Where(y => y.sgNumber == sgInd);
                sgPossCs[sgInd] = sgQuery.ToList();
                List <int> sgRs = SudokuCellExtensions.DeriveRowIndicesFromSG(sgInd);
                List <int> sgCs = SudokuCellExtensions.DeriveColIndicesFromSG(sgInd);
                foreach (int key in rowPossCs.Keys)
                {
                    if (sgRs.Contains(key))
                    {
                        SGUnitIntersectKey rca           = new SGUnitIntersectKey(sgInd, UnitType.row, key);
                        List <SudokuCell>  intersectVals = GetSGIntersect(sgPossCs[sgInd], rowPossCs[key]);
                        if (intersectVals != null)
                        {
                            if (intersectVals.Count > 1)
                            {
                                sgIntersects[rca] = intersectVals;
                            }
                        }
                    }
                }
                foreach (int key in colPossCs.Keys)
                {
                    if (sgCs.Contains(key))
                    {
                        SGUnitIntersectKey cca           = new SGUnitIntersectKey(sgInd, UnitType.column, key);
                        List <SudokuCell>  intersectVals = GetSGIntersect(sgPossCs[sgInd], colPossCs[key]);
                        if (intersectVals != null)
                        {
                            if (intersectVals.Count > 1)
                            {
                                sgIntersects[cca] = intersectVals;
                            }
                        }
                    }
                }
            }
            List <SGUnitIntersectKey> targetedSubgroupKeys = sgIntersects.Keys.ToList();

            foreach (SGUnitIntersectKey sgIK in targetedSubgroupKeys)
            {
                List <SudokuCell> openSGs = candidateCells.Select(x => x).Where(y => y.sgNumber == sgIK.sgValue).ToList();
                foreach (SudokuCell openSGCell in openSGs)
                {
                    if (!sgIntersects[sgIK].Contains(openSGCell))
                    {
                        sudoku.discardedValuesTable.AddDiscardedValue(openSGCell, targetNum);
                    }
                }
            }
        }