Example #1
0
        /// <summary>
        /// Guess a value by forking new puzzle instances.
        /// </summary>
        private bool Guess()
        {
            int leastPossibilities = 10;
            int bestCandidate = -1;

            for (int ci=0; ci<81; ci++) {
                Cell cell = Grid.Cells[ci];
                if (cell.Value == 0 && cell.Possibilities.Count < leastPossibilities) {
                    leastPossibilities = cell.Possibilities.Count;
                    bestCandidate = ci;
                }
            }

            if (bestCandidate > -1) {
                foreach(int p in Grid.Cells[bestCandidate].Possibilities) {
                    Grid newGrid = new Grid();
                    newGrid.LoadState(Grid.SaveState());

                    newGrid.Cells[bestCandidate].Value = p;

                    Puzzle puzzle = new Puzzle(Name, newGrid);
                    puzzle.SaveStateHistory = SaveStateHistory;

                    puzzle.Solve("Guess");

                    if(puzzle.Solved) {
                        Grid.LoadState(newGrid.SaveState());
                        State.Concat(puzzle.State);
                        Solved = true;
                        return true;
                    }
                }
            }

            return false;
        }