Example #1
0
        /* Solve methods */
        // Solves a copy of the current board
        private bool SolveBoard()
        {
            if (_solvedBoard != null)
                return true;

            // Copy the existing board and remove all non-predefined values
            AbstractBoard _copyOfBoard = (AbstractBoard)_board.Clone();
            _copyOfBoard.Clear();

            // Solve the copied board and assign to solved board
            Solver solver = new Solver(_copyOfBoard);
            if (solver.SolveBoard())
            {
                _solvedBoard = _copyOfBoard;
                return true;
            }
            else
                throw new UnsolvableBoardException();
        }
Example #2
0
        /// <summary>
        /// Brute Force
        /// </summary>
        private bool BruteForce()
        {
            Coordinate CellsWithMinimumPossibleValues = null;
            int minimumPossibleValues = int.MaxValue;
            for (int i = 0; i != _board.GetBoardSize(); i++)
            {
                for (int j = 0; j != _board.GetBoardSize(); j++)
                {
                    if ((possibleValues[i, j].Count < minimumPossibleValues) && (possibleValues[i, j].Count > 0))
                    {
                        minimumPossibleValues = possibleValues[i, j].Count;
                        CellsWithMinimumPossibleValues = new Coordinate(i, j);
                    }
                }
            }

            if (CellsWithMinimumPossibleValues == null)
                return false;

            foreach (int possibleValue in possibleValues[CellsWithMinimumPossibleValues.Row, CellsWithMinimumPossibleValues.Column])
            {
                AbstractBoard copyOfBoard = (AbstractBoard)_board.Clone();
                copyOfBoard.SetNumber(CellsWithMinimumPossibleValues.Row, CellsWithMinimumPossibleValues.Column, possibleValue);
                Solver newSolver = new Solver(copyOfBoard);
                if (newSolver.SolveBoard())
                {
                    for (int i = 0; i != _board.GetBoardSize(); i++)
                    {
                        for (int j = 0; j != _board.GetBoardSize(); j++)
                        {
                            _board.SetNumber(i, j, copyOfBoard.GetNumber(i, j));
                        }
                    }

                    return true;
                }
            }

            return false;
        }