/// <summary> /// Finds errors in the grid. Returns a list of cells such that: /// - there is another cell with the same value in the row /// - there is another cell with the same value in the column /// - there is another cell with the same value in the 3x3 square /// where the cell resides /// </summary> public List<List<int>> FindErrors(Grid grid) { List<List<int>> errors = new List<List<int>>(); for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { int val = grid.Get(row, col); // The same cell in the row? bool valueInRow = grid.GetRow(row).Count((eachCell) => eachCell == val) > 1; // The same cell in the column? bool valueInCol = grid.GetColumn(col).Count((eachCell) => eachCell == val) > 1; // The same cell in the 3x3 square? bool valueInSquare = grid.GetSquareAbout(row, col).Count((eachCell) => eachCell == val) > 1; if (val != 0 && (valueInRow || valueInCol || valueInSquare)) { errors.Add(new List<int>(new int[] { row, col })); } } } return errors; }
/// <summary> /// Get the list of valid moves for the given cell in the given grid. /// Used by the Hints bar and Consider() /// </summary> /// <returns> /// A list of valid move choices. /// </returns> public List<int> GetHintsFor(Grid grid, int row, int col) { // believe it or not, Lists are faster than int[]s for some reason List<int> stuff = grid.GetColumn(col).Concat(grid.GetRow(row)).Concat(grid.GetSquareAbout(row, col)).ToList(); List<int> results = new List<int>(); for (int i = 1; i <= 9; i++) { if (!stuff.Contains(i)) { results.Add(i); } } return results; }