public static byte[] ToBinary(SudokuGrid grid, SudokuConvertionAlgorithm algorithm) { var sb = new StringBuilder(grid.Metrics.CellsTotal); grid.IterateLinesXY((x, y) => sb.Append(grid.Cells[y, x].Number)); return(ToBinary(sb.ToString(), algorithm, grid.Constraints, grid.Metrics)); }
private SudokuGridCell FindFirstCellWithMinimumCandidates(SudokuGrid grid) { byte minCandidates = grid.Metrics.MaximumNumber; SudokuGridCell minCandidatesCell = null; grid.IterateLinesXY((x, y) => { var currCell = grid.Cells[y, x]; if (currCell.Candidates != null && currCell.Candidates.Count < minCandidates) { minCandidates = (byte)currCell.Candidates.Count; minCandidatesCell = currCell; } }); return(minCandidatesCell); }
private SudokuGridCell[,] MakeAssumptionInCell(ref SudokuGridPosition? minCandidatesCellPos) { if (!minCandidatesCellPos.HasValue) { // TODO: optimize: lookup that kind of cell repetitively when its number of candidates changes (decreases): compare against the minimum value from a variable (where to put it?) minCandidatesCellPos = FindFirstCellWithMinimumCandidates(Grid).Position; } var minCandidatesCell = Grid.Cells[minCandidatesCellPos.Value.Y, minCandidatesCellPos.Value.X]; byte assumedNumber = minCandidatesCell.Candidates.ToArray()[0]; assumptionsGrid = (SudokuGrid)Grid.GetType() .GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, CallingConventions.HasThis, new[] { typeof(SudokuGridConstraints) }, null) .Invoke(new [] { (object)Grid.Constraints }); // BUG: !!! as we use the standard grid here, which was not initially intended to be used anywhere except for decode-and-construct scenarios, by the time we reach this code the assumptionsGrid's cells are not initialized yet -- consider introducing some sort of temporary 'grid' (most probably, a kind of 'cells-only layer'). For that reason one of the ctor's code sections was moved into the Reset() method var assumptionsGridSolver = new SudokuSolvingIterationAssumptionTechnique(assumptionsGrid); // WI: cell-to-cell iteration is a very intensive process -- use cells array cloning instead with targeted minCandidatesCell... altering var minCandidatesCellPosCopy = minCandidatesCellPos.Value; assumptionsGrid.IterateLinesXY((x, y) => { SudokuGridCell currCell; var currPos = new SudokuGridPosition(x, y, false); if (currPos.Equals(minCandidatesCellPosCopy)) { currCell = new SudokuGridCell(assumptionsGrid, currPos, assumedNumber); assumptionsGridSolver.lastFilledPos = currPos; assumptionsGridSolver.currCheckingPos = currPos; assumptionsGridSolver.currCheckingPos.Shift(1, assumptionsGrid.Metrics); } else { currCell = new SudokuGridCell(assumptionsGrid, currPos, Grid.Cells[y, x].Number); if (currCell.Candidates != null) { // just to bring into accord with the latest value from the 'parent' grid currCell.Candidates.IntersectWith(Grid.Cells[y, x].Candidates); } } SudokuGrid.AssignNewCell(assumptionsGrid, currCell); //assumptionsGrid.Cells[y, x] = currCell; }); //List<SudokuGridCell[,]> assumptionGridSolutuons = assumptionGridSolver.SearchSolutions(); //if (assumptionGridSolutuons.Count > 0) // correct assumption was made //{ // return assumptionsGrid.Cells; //} //SudokuGridCell[,] solution; if (assumptionsGridSolver.SearchSolutionDebug()) // correct assumption was made { //return solution; return(assumptionsGrid.Cells); } // incorrect assumption was made (executes on contradiction only) minCandidatesCell.Candidates.Remove(assumedNumber); // if there were only two candidates, with assumedNumber being one of them, then the remaining one is a clue if (minCandidatesCell.IsClue) { minCandidatesCellPos = null; var newPos = new SudokuGridPosition(minCandidatesCell.Position.X, minCandidatesCell.Position.Y, false); lastFilledPos = newPos; currCheckingPos = newPos; currCheckingPos.Shift(1, Grid.Metrics); } return(null); }