コード例 #1
0
ファイル: Board.cs プロジェクト: rbirkby/LinqSudokuSolver
        private Digit[,] DuplicateDigits()
        {
            // Return a copy of the _digits array
            var digits = new Digit[Size, Size];

            for (var y = 0; y < Size; y++)
            {
                for (var x = 0; x < Size; x++)
                {
                    digits[x, y] = new Digit(_digits[x, y]);
                }
            }
            return(digits);
        }
コード例 #2
0
ファイル: Board.cs プロジェクト: rbirkby/LinqSudokuSolver
 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;
     }
 }
コード例 #3
0
ファイル: Board.cs プロジェクト: rbirkby/LinqSudokuSolver
 private void Initialize(Board basis)
 {
     // Initialize each digit
     _digits = new Digit[Size, Size];
     for (var y = 0; y < Size; y++)
     {
         for (var x = 0; x < Size; x++)
         {
             _digits[x, y] = new Digit();
         }
     }
     if (null != basis)
     {
         // Copy the digits of the specified board
         for (var y = 0; y < Size; y++)
         {
             for (var x = 0; x < Size; x++)
             {
                 var digit = basis.GetDigit(x, y);
                 if (digit.ValueKnown)
                 {
                     SetValue(x, y, digit.KnownValue, digit.DigitKind);
                 }
             }
         }
     }
 }
コード例 #4
0
ファイル: Board.cs プロジェクト: rbirkby/LinqSudokuSolver
 private Digit[,] DuplicateDigits()
 {
     // Return a copy of the _digits array
     var digits = new Digit[Size, Size];
     for (var y = 0; y < Size; y++)
     {
         for (var x = 0; x < Size; x++)
         {
             digits[x, y] = new Digit(_digits[x, y]);
         }
     }
     return digits;
 }
コード例 #5
0
ファイル: Digit.cs プロジェクト: rbirkby/LinqSudokuSolver
 public Digit(Digit digit)
 {
     // Copy the specified digit
     _bits = digit._bits;
 }
コード例 #6
0
 public Digit(Digit digit)
 {
     // Copy the specified digit
     _bits = digit._bits;
 }
コード例 #7
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;
 }