Example #1
0
 public void SetValue(SudokuSquare s, int value)
 {
     s.SetValue(value);
     foreach (var ss in AllRelated(s))
     {
         ss.Forbid(value);
     }
 }
Example #2
0
        public SudokuBoard()
        {
            board = new SudokuSquare[9, 9];

            foreach (var r in SudokuIterators.Indexes)
            {
                foreach (var c in SudokuIterators.Indexes)
                {
                    board[r, c] = new SudokuSquare {
                        Row = r, Column = c
                    };
                }
            }
        }
Example #3
0
        /// <summary>
        /// Set the values for the first square that has its options reduced to 1.
        /// </summary>
        public static bool SquareHasOneOption(this SudokuBoard board, out SudokuSquare newlySolved)
        {
            newlySolved = null;
            foreach (var s in board.Squares)
            {
                if (s.HasValue)
                {
                    continue;
                }

                if (s.NumOptions == 1)
                {
                    board.SetValue(s, s.Options.Single());
                    newlySolved = s;
                    return(true);
                }
            }
            return(false);
        }
Example #4
0
        /// <summary>
        /// If all squares in a group (row, column, block) but one exclude the same value, then the square without the
        /// exclusion must be that value.
        /// </summary>
        public static bool GroupHasOneSquareWithOption(this SudokuBoard board, out SudokuSquare newlyFound)
        {
            newlyFound = null;

            foreach (var enumerator in new[] { board.EnumerateRows(), board.EnumerateColumns(), board.EnumerateBlocks() })
            {
                foreach (var group in enumerator)
                {
                    var unsetSquares = group.Where(s => !s.HasValue).ToList();
                    var unsetValues  = group.UnfoundOptions();
                    foreach (var v in unsetValues)
                    {
                        if (unsetSquares.Count(e => e.Options.Contains(v)) == 1)
                        {
                            newlyFound = unsetSquares.Single(e => e.Options.Contains(v));
                            board.SetValue(newlyFound, v);
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Example #5
0
 public bool IsRelatedTo(SudokuSquare s) =>
 Row == s.Row ||
 Column == s.Column ||
 Block == s.Block;
Example #6
0
 public IEnumerable <SudokuSquare> AllRelated(SudokuSquare square) =>
 Squares.Where(square.IsRelatedTo);