Example #1
0
        /// <summary>
        /// check if value of <paramref name="cellWithValue"/> can be elsewhere in the board
        /// </summary>
        /// <param name="cellWithValue"></param>
        /// <returns></returns>
        private bool ValueCanBeElsewhere(SudokuCell cellWithValue)
        {
            if (cellWithValue.IsEmpty())
            {
                throw new ArgumentException($"Argument is not valid: {nameof(cellWithValue)} is an empty cell");
            }
            int row    = Board.GetRow(cellWithValue);
            int column = Board.GetColumn(cellWithValue);
            int value  = int.Parse(Board[row, column].Value);

            var emptyRowValues    = Board.GetNthRow(row).GetEmptyCells();
            var emptyColumnValues = Board.GetNthColumn(column).GetEmptyCells();
            var emptySquareValues = Board.GetSquareFromPosition(row, column).GetAllCells().GetEmptyCells();

            var allValues = new List <List <SudokuCell> >()
            {
                emptyRowValues, emptyColumnValues, emptySquareValues
            };

            // try to place value into other cells in the row, column and square
            foreach (var selectedValues in allValues)
            {
                bool isOnlyPlaceForValue = true;
                foreach (SudokuCell selectedCell in selectedValues)
                {
                    cellWithValue.ClearValue();
                    selectedCell.Value = value.ToString();
                    var cellsRow    = Board.GetNthRow(Board.GetRow(selectedCell));
                    var cellsCol    = Board.GetNthColumn(Board.GetColumn(selectedCell));
                    var cellsSquare = Board.GetSquareFromPosition(Board.GetRow(selectedCell), Board.GetColumn(selectedCell)).GetAllCells();

                    if (cellsRow.IsSudokuValid() && cellsCol.IsSudokuValid() && cellsSquare.IsSudokuValid())
                    {
                        isOnlyPlaceForValue = false;
                    }

                    selectedCell.ClearValue();
                    cellWithValue.Value          = value.ToString();
                    cellWithValue.IsDefaultValue = true;
                }
                if (isOnlyPlaceForValue)
                {
                    return(false);
                }
            }
            return(true);
        }
Example #2
0
        /// <summary>
        /// check if cell can have another value than <paramref name="value"/>
        /// </summary>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool CanCellContainAnotherValue(int row, int column, int value)
        {
            // check if here can be another value, if no -> remove the value
            var cellsRow    = Board.GetNthRow(row).GetValuesFromCells();
            var cellsCol    = Board.GetNthColumn(column).GetValuesFromCells();
            var cellsSquare = Board.GetSquareFromPosition(row, column).GetAllCells().GetValuesFromCells();

            var invalidValues = cellsRow.Union(cellsCol).Union(cellsSquare).ToList();

            invalidValues.Remove(value);

            var validValues = this.Board.AllSudokuValues.Except(invalidValues);

            if (validValues.Count() == 1 && validValues.First() == value)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }