Ejemplo n.º 1
0
        /* Parsers */

        public static AbstractBoard ParseDataIntoBoard(string boardData)
        {
            // Split file into lines and create a board that matches
            string[] boardLines = boardData.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            // Create a suitable board
            AbstractBoard board = BoardFactory.CreateBoard(boardLines.Length);

            for (int i = 0; i < boardLines.Length; i++)
            {
                string   currentLine = boardLines[i];
                string[] cells       = currentLine.Split(new string[] { "," }, StringSplitOptions.None);

                for (int j = 0; j < cells.Length; j++)
                {
                    string currentCell = cells[j];
                    if (currentCell.Length >= 2)
                    {
                        // Read values from current cell
                        int  value      = Convert.ToInt32(currentCell.Substring(0, currentCell.Length - 1));
                        bool predefined = (currentCell[currentCell.Length - 1] == 'T') ? true : false;

                        // Update cells in board
                        board.SetNumber(i, j, value, predefined);
                    }
                }
            }

            return(board);
        }
Ejemplo n.º 2
0
 public void SetNumber(int row, int column, int value)
 {
     if (_board != null)
     {
         _board.SetNumber(row, column, value);
     }
     else
     {
         throw new GameNotStartedException();
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Step 1: Only one valid value for a square
        ///
        /// SuDoku Solver checks to see if any of the squares have only one possible value.
        /// If any are found then the value is entered for this square as mentioned above and
        /// Step 1 is repeated until all remaining unsolved squares contain multiple possible
        /// values (or the SuDoku is completely solved!)
        /// </summary>
        private int SolverStep1()
        {
            int foundNumbers = 0;

            // Loop through algorithm until there is no more changes
            bool changes;

            do
            {
                changes = false;

                // Check each empty cell on the board
                for (int i = 0; i < _board.GetBoardSize(); i++)
                {
                    for (int j = 0; j < _board.GetBoardSize(); j++)
                    {
                        if (_board.IsNumberBlank(i, j))
                        {
                            // There may only be one possible value (because of step 3 and 4),
                            // which means that we don't need to run this function
                            if (possibleValues[i, j].Count > 1)
                            {
                                RemoveInvalidValuesFromCell(ref possibleValues[i, j], i, j);
                            }

                            // If there is only one possible number; insert into board,
                            // remove from possible values and update changes
                            if (possibleValues[i, j].Count == 1)
                            {
                                _board.SetNumber(i, j, possibleValues[i, j][0]);
                                possibleValues[i, j].RemoveAt(0);
                                changes = true;
                                foundNumbers++;
                            }
                        }
                    }
                }
            }while (changes == true);

            return(foundNumbers);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }