Exemple #1
0
        /* Parsers */
        public static string ParseDataFromBoard(AbstractBoard board)
        {
            StringBuilder text = new StringBuilder();

            for (int i = 0; i != board.GetBoardSize(); i++)
            {
                // Add newline separators after the first line
                if (i > 0)
                    text.Append(Environment.NewLine);

                for (int j = 0; j != board.GetBoardSize(); j++)
                {
                    // Add comma separators after the first number
                    if (j > 0)
                        text.Append(",");

                    // Add non-blank numbers
                    if (!board.IsNumberBlank(i, j))
                    {
                        text.Append(board.GetNumber(i, j));
                        text.Append(board.IsNumberPredefined(i, j) ? "T" : "F");
                    }
                }

            }

            return text.ToString();
        }
Exemple #2
0
        /* Parsers */

        public static string ParseDataFromBoard(AbstractBoard board)
        {
            StringBuilder text = new StringBuilder();

            for (int i = 0; i != board.GetBoardSize(); i++)
            {
                // Add newline separators after the first line
                if (i > 0)
                {
                    text.Append(Environment.NewLine);
                }

                for (int j = 0; j != board.GetBoardSize(); j++)
                {
                    // Add comma separators after the first number
                    if (j > 0)
                    {
                        text.Append(",");
                    }

                    // Add non-blank numbers
                    if (!board.IsNumberBlank(i, j))
                    {
                        text.Append(board.GetNumber(i, j));
                        text.Append(board.IsNumberPredefined(i, j) ? "T" : "F");
                    }
                }
            }

            return(text.ToString());
        }
Exemple #3
0
        private void fillBoard(int row, int column, ref List <int>[,] triedNumbers)
        {
            List <int> possibleValuesInCell;

            do
            {
                if (Backtracking > 40000)//max times to run fillboard, jumps out of method
                {
                    return;
                }

                _board.ClearNumber(row, column);
                possibleValuesInCell = new List <int>().GenerateAllPossibleValues(_board.GetBoardSize()); // list numbers depending on boardsize ((1-4),(1-6),(1-9),(1-12),(1-16))
                RemoveInvalidValuesFromCell(ref possibleValuesInCell, row, column);                       //  remove invalid numbers for the cell
                foreach (int triedNumber in triedNumbers[row, column])                                    //remove tried numbers in cell from possibleValuesInCell
                {
                    possibleValuesInCell.Remove(triedNumber);
                }


                if (possibleValuesInCell.Count == 0)
                {
                    triedNumbers[row, column].Clear();
                    int prevRow;
                    int prevColumn;
                    if (column == 0)
                    {
                        prevRow    = row - 1;
                        prevColumn = _board.GetBoardSize() - 1;
                    }
                    else
                    {
                        prevRow    = row;
                        prevColumn = column - 1;
                    }
                    fillBoard(prevRow, prevColumn, ref triedNumbers);
                }

                else
                {
                    int randomIndex  = RandomNumberGen(0, possibleValuesInCell.Count);    // Choose random index
                    int randomNumber = possibleValuesInCell.ElementAt <int>(randomIndex); // Get number in index

                    _board.SetNumber(row, column, randomNumber, false);
                    triedNumbers[row, column].Add(randomNumber);
                }

                Backtracking = Backtracking + 1;
            } while (possibleValuesInCell.Count == 0);
        }
Exemple #4
0
        /* Proxy to AbstractBoard */

        public int GetBoardSize()
        {
            if (_board != null)
            {
                return(_board.GetBoardSize());
            }
            else
            {
                throw new GameNotStartedException();
            }
        }
Exemple #5
0
        /* Validate methods */

        public static bool ValidateBoard(AbstractBoard board, bool completeCheck = false)
        {
            bool validation = true;

            for (int i = 0; i != board.GetBoardSize(); i++)
            {
                // Validate row, column and block
                validation &= ValidateSection(board, board.GetRowCells(i), completeCheck);
                validation &= ValidateSection(board, board.GetColumnCells(i), completeCheck);
                validation &= ValidateSection(board, board.GetBlockCells(i), completeCheck);
            }

            return(validation);
        }
Exemple #6
0
        /* Validate methods */
        public static bool ValidateBoard(AbstractBoard board, bool completeCheck = false)
        {
            bool validation = true;

            for (int i = 0; i != board.GetBoardSize(); i++)
            {
                // Validate row, column and block
                validation &= ValidateSection(board, board.GetRowCells(i), completeCheck);
                validation &= ValidateSection(board, board.GetColumnCells(i), completeCheck);
                validation &= ValidateSection(board, board.GetBlockCells(i), completeCheck);
            }

            return validation;
        }
Exemple #7
0
        /* Solver */

        /// <summary>
        /// The SuDoku Solver's algorithm only uses logical arguments to solve the puzzles.
        /// It does not use 'trial and error' methods. For anybody interested in an explanation
        /// of the algorithm it employs to solve your SuDoku then please read on. The Solver
        /// stores two grids (as arrays), one is the actual SuDoku grid itself, and the second
        /// contains all the 'possible values' for each square. Initially all values (1-9) would
        /// be possible for all squares of course. The algorithm then tries a number of steps to
        /// solve the puzzle, stopping when it either solves the SuDoku or all the following steps
        /// fail to find any new values.
        /// </summary>
        public bool SolveBoard()
        {
            // Initialize fields
            possibleValues = new List <int> [_board.GetBoardSize(), _board.GetBoardSize()];

            // Create a list of all possible values
            for (int i = 0; i < _board.GetBoardSize(); i++)
            {
                for (int j = 0; j < _board.GetBoardSize(); j++)
                {
                    if (_board.IsNumberBlank(i, j))
                    {
                        possibleValues[i, j] = new List <int>().GenerateAllPossibleValues(_board.GetBoardSize());
                    }
                    else
                    {
                        possibleValues[i, j] = new List <int>();
                    }
                }
            }


            // Find new numbers until the board is solved
            while (true)
            {
                // Perform step 1, noting any new numbers
                int foundNumbers = SolverStep1();
                if (Validator.ValidateBoard(_board, true))
                {
                    return(true);
                }

                // Perform step 2, noting any new numbers
                foundNumbers += SolverStep2();
                if (Validator.ValidateBoard(_board, true))
                {
                    return(true);
                }

                // If neither of step 1 nor 2 finds a number, move to step 3 and 4
                if (foundNumbers == 0)
                {
                    // Perform step 3, noting any changes to possible values
                    bool foundInvalidValues = SolverStep3();

                    // Perform step 4, noting any changes to possible values
                    foundInvalidValues |= SolverStep4();

                    // If no new information is found, try brute force
                    if (!foundInvalidValues)
                    {
                        return(BruteForce());
                    }
                }
            }
        }