示例#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());
        }
示例#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();
        }
示例#3
0
        public static bool ValidateSection(AbstractBoard board, IEnumerable<Coordinate> cells, bool completeCheck = false)
        {
            HashSet<int> previousValues = new HashSet<int>();
            foreach (Coordinate cell in cells)
            {
                // Get current value
                int value = board.GetNumber(cell.Row, cell.Column);

                // Check if the current value is blank
                if (board.IsNumberBlank(cell.Row, cell.Column))
                {
                    // For a complete check; none of the numbers are allowed to be blank
                    // Otherwise skip to next cell
                    if (completeCheck)
                        return false;
                    else
                        continue;
                }

                // Check if the current value is duplicated
                if (previousValues.Contains(value))
                    return false;

                // Store the current value
                previousValues.Add(value);
            }

            // No duplicates
            return true;
        }
示例#4
0
 private void RemoveInvalidValuesFromCellForSection(ref List <int> possibleValuesInCell, IEnumerable <Coordinate> cells)
 {
     foreach (Coordinate cell in cells)
     {
         int value = _board.GetNumber(cell.Row, cell.Column);
         possibleValuesInCell.Remove(value);
     }
 }
示例#5
0
 // Gets the correct number from the solved board
 public int GetCorrectNumber(int row, int column)
 {
     if (SolveBoard())
     {
         return(_solvedBoard.GetNumber(row, column));
     }
     else
     {
         throw new UnsolvableBoardException();
     }
 }
示例#6
0
 public int GetNumber(int row, int column)
 {
     if (_board != null)
     {
         return(_board.GetNumber(row, column));
     }
     else
     {
         throw new GameNotStartedException();
     }
 }
示例#7
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);
        }
示例#8
0
        public static bool ValidateSection(AbstractBoard board, IEnumerable <Coordinate> cells, bool completeCheck = false)
        {
            HashSet <int> previousValues = new HashSet <int>();

            foreach (Coordinate cell in cells)
            {
                // Get current value
                int value = board.GetNumber(cell.Row, cell.Column);

                // Check if the current value is blank
                if (board.IsNumberBlank(cell.Row, cell.Column))
                {
                    // For a complete check; none of the numbers are allowed to be blank
                    // Otherwise skip to next cell
                    if (completeCheck)
                    {
                        return(false);
                    }
                    else
                    {
                        continue;
                    }
                }

                // Check if the current value is duplicated
                if (previousValues.Contains(value))
                {
                    return(false);
                }

                // Store the current value
                previousValues.Add(value);
            }

            // No duplicates
            return(true);
        }