Example #1
0
        public static List <ISolutionStep> Solve(GameBoard board, out bool couldSolve)
        {
            List <Cell> unsolved = board
                                   .GetAllLines(Coord.BYROWS)
                                   .SelectMany(t => t)
                                   .Where(cell => cell.Value == 0)
                                   .ToList();

            var returnValue = new List <ISolutionStep>();
            var totalRules  = RuleCreator.Length;
            int i           = 0;

            couldSolve = true;
            while (i < totalRules && unsolved.Count > 0)
            {
                for (i = 0; i < totalRules; i++)
                {
                    ISolutionStep rule = RuleCreator[i]();
                    if (!rule.FindPattern(board))
                    {
                        continue;
                    }

                    returnValue.Add(rule);
                    rule.Apply();
                    if (i == 0)  // only rule which solves the cell
                    {
                        Cell solutionCell = rule.Solution.Actions
                                            .First(action => action.PencilmarkDataList
                                                   .FirstOrDefault(pmFinding => pmFinding.Role == PMRole.Solution) != null)
                                            .Cell;

                        unsolved.Remove(solutionCell);
                    }

                    break;
                }

                if (i == totalRules)
                {
                    couldSolve = false;
                    Console.WriteLine("Could not solve the puzzle.  Still {0} cells unsolved.", unsolved.Count);
                    break;
                }
            }

            return(returnValue);
        }
Example #2
0
        private void UpdateRuleButtons()
        {
            if (PuzzleSolution == null || PuzzleSolution.Count == 0)
            {
                return;
            }

            ButtonUndoAll.Enabled = ButtonStepBack.Enabled = (CurrentStep > 0);
            ButtonRedoAll.Enabled = ButtonStepForward.Enabled = (CurrentStep < PuzzleSolution.Count);
            if (CurrentStep < PuzzleSolution.Count)
            {
                ISolutionStep step = PuzzleSolution[CurrentStep];
                TextBoxRuleInformation.Text = step.Solution.Description;
                UIGameBoard.HighlightCells(step.Solution);
            }
            else
            {
                TextBoxRuleInformation.Text = string.Empty;
            }
        }