public void SetValue(SudokuSquare s, int value) { s.SetValue(value); foreach (var ss in AllRelated(s)) { ss.Forbid(value); } }
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 }; } } }
/// <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); }
/// <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); }
public bool IsRelatedTo(SudokuSquare s) => Row == s.Row || Column == s.Column || Block == s.Block;
public IEnumerable <SudokuSquare> AllRelated(SudokuSquare square) => Squares.Where(square.IsRelatedTo);