Esempio n. 1
0
 private static void RaiseBreakerProgressEvent(SudokuPuzzle puzzle)
 {
     if (BreakerProgressEvent != null)
     {
         BreakerProgressEvent(new BreakerProgressEventArgs(puzzle));
     }
 }
Esempio n. 2
0
 private static void CopyPuzzle(SudokuPuzzle fromPuzzle, SudokuPuzzle toPuzzle)
 {
     foreach (SudokuCell cell in toPuzzle.Cells)
     {
         cell.SetPossibleValues(fromPuzzle.Cells[cell.X, cell.Y].PossibleValues);
     }
 }
Esempio n. 3
0
        public static bool TryBreak(SudokuPuzzle puzzle)
        {
            if (puzzle.IsSolved)
            {
                return(true);
            }
            if (puzzle.Failed)
            {
                return(false);
            }
            SudokuCell cellToTry = puzzle.GetFirstUnsolvedCell();

            foreach (int possible in cellToTry.PossibleValues)
            {
                SudokuPuzzle puzzleTotry = new SudokuPuzzle(puzzle.ToIntArray());
                SudokuCell   cell        = puzzleTotry.Cells[cellToTry.X, cellToTry.Y];

                cell.SolvedValue = possible;
                if (puzzleTotry.IsSolved)
                {
                    CopyPuzzle(puzzleTotry, puzzle);
                    return(true); // no progress needed, it will return puzzle
                }
                else
                {
                    if (puzzleTotry.Failed)
                    {
                        puzzle.Cells[cell.X, cell.Y].RemoveValue(possible);
                        RaiseBreakerProgressEvent(puzzle);
                        if (cell.IsSolved)
                        {
                            return(TryBreak(puzzle));
                        }
                    }
                    else
                    {
                        //try another cell
                        if (TryBreak(puzzleTotry))
                        {
                            CopyPuzzle(puzzleTotry, puzzle);
                            RaiseBreakerProgressEvent(puzzle);
                            return(true);
                        }
                        else
                        {
                            puzzle.Cells[cell.X, cell.Y].RemoveValue(possible);
                            CopyPuzzle(puzzle, puzzleTotry);
                            RaiseBreakerProgressEvent(puzzle);
                            if (cell.IsSolved)
                            {
                                return(TryBreak(puzzle));
                            }
                        }
                    }
                }
            }
            return(false);
        }
Esempio n. 4
0
 public BreakerProgressEventArgs(SudokuPuzzle puzzle)
 {
     this.Puzzle = puzzle;
 }
Esempio n. 5
0
        public static async Task <bool> TryBreakAsync(SudokuPuzzle puzzle)
        {
            bool t = await Task.Run(() => TryBreak(puzzle));

            return(t);
        }