예제 #1
0
        public void SetValue(int x, int y, int value, Digit.Kind kind)
        {
            // Save the current digits in case something goes wrong
            var savedDigits = DuplicateDigits();

            try
            {
                try
                {
                    // Attempt to set the specified value
                    _digits[x, y].SetValue(value, kind);
                }
                catch (InvalidDigitException)
                {
                    // Digit was invalid, so board would have become invalid
                    throw new InvalidBoardException(x, y);
                }
                // Exclude the value from the rest of the row and column
                for (var i = 0; i < Size; i++)
                {
                    if (i != x)
                    {
                        Exclude(i, y, value);
                    }
                    if (i != y)
                    {
                        Exclude(x, i, value);
                    }
                }
                // Exclude the value from the rest of the block
                for (var j = (y / 3) * 3; j < ((y / 3) * 3) + 3; j++)
                {
                    for (var i = (x / 3) * 3; i < ((x / 3) * 3) + 3; i++)
                    {
                        if ((i != x) || (j != y))
                        {
                            Exclude(i, j, value);
                        }
                    }
                }
            }
            catch (InvalidBoardException)
            {
                // Recover from invalid board and re-throw
                _digits = savedDigits;
                throw;
            }
        }
예제 #2
0
        public bool ChangeSelectedValue(int value, Digit.Kind kind)
        {
            var changed = false;

            // Get the current position
            _conflictPosition = null;
            var x = (int)_markerPosition.X;
            var y = (int)_markerPosition.Y;

            if (Digit.Unknown == value)
            {
                // Clear the digit's value if set
                if (_board.GetDigit(x, y).ValueKnown)
                {
                    _board.ClearValue(x, y);
                    changed = true;
                }
            }
            else
            {
                try
                {
                    // Set the cell's value
                    _board.SetValue(x, y, value, kind);
                    changed = true;
                }
                catch (InvalidBoardException e)
                {
                    // Would have created an invalid board; identify the location of the conflict
                    _conflictPosition = new Point(e.X, e.Y);
                }
            }
            // Update the display
            UpdateDisplay(_board);
            return(changed);
        }