Sudoque.Game.Engine.Rules.OnlyOnePotential.HelpWith C# (CSharp) Method

HelpWith() public method

public HelpWith ( IEnumerable cells ) : Hint
cells IEnumerable
return Hint
        public Hint HelpWith(IEnumerable<Cell> cells)
        {
            var actuals = cells.SelectMany(
                c => c.Actual.HasValue ?
                    new[] {c.Actual.Value} :
                    new int[0]);

            var copiedCandidates = cells.Select((c, i) => c.Copy()).ToList();
            copiedCandidates.RemoveAll(c => c.Actual.HasValue);

            foreach (var copiedCandidate in copiedCandidates)
            {
                if (!copiedCandidate.Potentials.Any())
                {
                    for (int i = 1; i < 10; i++)
                    {
                        copiedCandidate.RequestToggleNumber(i);
                    }
                }
            }

            foreach (var actual in actuals)
            {
                foreach(var cell in copiedCandidates)
                {
                    cell.RemovePotential(actual);
                    if (cell.Actual.HasValue)
                    {
                        return new Hint(string.Format("This cell can only be a {0}", cell.Actual.Value),
                            new[] {cell.Id});
                    }
                }
            }

            return Hint.None;
        }

Usage Example

        public void ShouldTellUsIfAnEmptySquareOnlyHasOnePotentialLeft()
        {
            // Given some cells, all of which are filled with actuals
            // except the third, which is missing "3"
            var cells = CreateCellsWithActuals(1, 2, 3, 4, 5, 6, 7, 8, 9);
            cells[2].RequestToggleNumber(3);

            // When we apply the rule to that group
            var rule = new OnlyOnePotential();
            var hint = rule.HelpWith(cells);

            // Then the rule should return a hint that the 3rd cell can only be a 3.
            Assert.AreEqual(hint.CellIds.First(), cells[2].Id);
            Assert.AreEqual("This cell can only be a 3", hint.Text);
        }
All Usage Examples Of Sudoque.Game.Engine.Rules.OnlyOnePotential::HelpWith
OnlyOnePotential