Example #1
0
 public bool Equals(SudokuGrid obj)
 {
     for (int row = 0; row < 9; row++)
     {
         for (int column = 0; column < 9; column++)
         {
             if (!this[row, column].Equals(obj[row, column]))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Example #2
0
        /// <summary>
        /// Checks whether a given SudokuGrid is valid
        /// </summary>
        /// <returns></returns>
        public bool IsValid()
        {
            SudokuSolver sudokuSolver = new SudokuSolver();
            SudokuGrid   mySudoku     = sudokuSolver.Copy(this);

            if (sudokuSolver.IsSolved(this))
            {
                return(true);
            }


            if (this.NumSolved() < 16)
            {
                return(false);
            }

            mySudoku.SolveForIsValid();
            //if simple solve, return is valid
            if (sudokuSolver.IsSolved(mySudoku))
            {
                return(true);
            }

            bool solvedOne = false;

            SudokuGrid copy1 = sudokuSolver.Copy(this);

            sudokuSolver.bruteForceSolver(ref copy1);

            if (!sudokuSolver.IsSolved(copy1))
            {
                return(false);
            }

            if (sudokuSolver.InvalidMove(this))
            {
                return(false);
            }

            SudokuGrid firstSolve = new SudokuGrid();

            firstSolve = sudokuSolver.Copy(copy1);

            //else, guess all possibles and brute force solve. if multiple solutions, return false
            for (int row = 0; row < 9; row++)
            {
                for (int column = 0; column < 9; column++)
                {
                    //if unsolved
                    if (!this[row, column].getSolved())
                    {
                        for (int i = 0; i < this[row, column].getPossibles().Count; i++)
                        {
                            SudokuGrid copy = sudokuSolver.Copy(this);
                            //solve to the index of the guess
                            copy[row, column].solve(copy[row, column].getPossibles()[i]);



                            bool solvedThisOne;



                            try
                            {
                                //brute force it
                                sudokuSolver.bruteForceSolver(ref copy);
                                solvedThisOne = sudokuSolver.IsSolved(copy);
                            }
                            catch
                            {
                                solvedThisOne = false;
                            }

                            //if this one and another different one were solved, invalid for too many solutions
                            if (solvedThisOne)
                            {
                                try
                                {
                                    if (!firstSolve.ToString().Equals(copy.ToString()))
                                    {
                                        //return false;
                                    }
                                    if (!sudokuSolver.Equals(firstSolve, copy))
                                    {
                                        return(false);
                                    }
                                }
                                catch
                                {
                                }
                            }
                            if (solvedThisOne)
                            {
                                firstSolve = sudokuSolver.Copy(copy);
                            }
                        }
                    }
                }
            }
            return(true);
        }